Populating Arrays via code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adriann
    New Member
    • Aug 2006
    • 25

    Populating Arrays via code

    Hi,
    How do I populate a flat array through a loop type sequence.
    So far I have a basic query resulting in a few rows of data.

    $sSql = 'SELECT suburb'
    . ' FROM locations'
    . ' WHERE locations.postc ode = '.$postcode
    . ' LIMIT 0 , 40;';

    $result = mysql_query($sS ql) or die('Query failed: ' . mysql_error());
    $numberOfRows = mysql_num_rows( $result);
    $i = 0;

    $SuburbArray = array(); // define the array
    if ($numberOfRows> 0) {
    while ($i<$numberOfRo ws) {
    echo mysql_result($r esult,$i,"subur b");
    $thisSuburb = mysql_result($r esult,$i,"subur b");
    // I need to add each value to array here.
    $i++;
    }
    }


    thank you.
  • somaskarthic
    New Member
    • Aug 2006
    • 60

    #2
    Hi

    Try this,

    $result = mysql_query($sS ql) or die('Query failed: ' . mysql_error());
    $numberOfRows = mysql_num_rows( $result);
    $i = 0;

    $SuburbArray = array(); // define the array
    if ($numberOfRows> 0) {
    while ($i<$numberOfRo ws) {
    echo mysql_result($r esult,$i,"subur b");
    $thisSuburb = mysql_result($r esult,$i,"subur b");
    // I need to add each value to array here.

    /*************** *************** ***************/
    $SuburbArray[i] = $thisSuburb;

    /*************** *************** *************** */



    $i++;
    }
    }



    You can check this , by printing the array values
    ...
    $total_count = count($SuburbAr ray);
    for($index=0;$i ndex<$total_cou nt;$index++)
    {
    echo $SuburbArray[$index]."<br>";
    }
    ...

    Try this and reply.

    -somaskarthic


    Originally posted by adriann
    Hi,
    How do I populate a flat array through a loop type sequence.
    So far I have a basic query resulting in a few rows of data.

    $sSql = 'SELECT suburb'
    . ' FROM locations'
    . ' WHERE locations.postc ode = '.$postcode
    . ' LIMIT 0 , 40;';

    $result = mysql_query($sS ql) or die('Query failed: ' . mysql_error());
    $numberOfRows = mysql_num_rows( $result);
    $i = 0;

    $SuburbArray = array(); // define the array
    if ($numberOfRows> 0) {
    while ($i<$numberOfRo ws) {
    echo mysql_result($r esult,$i,"subur b");
    $thisSuburb = mysql_result($r esult,$i,"subur b");
    // I need to add each value to array here.
    $i++;
    }
    }


    thank you.

    Comment

    • adriann
      New Member
      • Aug 2006
      • 25

      #3
      Hey, thanks for that. That works!
      I also found another solution.

      At the section of code where each new item is to be added into the array...
      array_push($Sub urbsArray, $NewItem)



      regards,
      Adrian

      Comment

      Working...