Page 1 of 1

stopping an array from clearing data after function call

Posted: Fri Apr 24, 2009 3:28 pm
by kaiser0427
i'm getting data from a table and I'm running it through a function to extract all the rows from each field. Here is the function:

Code: Select all

 
function mysql_fetch_all($result, $number) {
   while($row=mysql_fetch_array($result)) {
     echo $row[$number]."<br>";
      $return[] = $row;
   }
   return $return;
}
 
I'm calling this function from here:

Code: Select all

 
function showTable($table_name, $primary_key, $index, $secondary_column, $div_header, $div_header2, $divID, $divID2){
    $show_all = mysql_query("SELECT * FROM $table_name WHERE $primary_key < 100");
    echo "<div id='$divID'><h4>".$div_header."</h4><p>";
    mysql_fetch_all($show_all, 1);
    echo "</p></div>";
    echo "<div id='$divID2'><h4>". $div_header2."</h4><p>";
//  $show_all = mysql_query("SELECT * FROM $table_name WHERE $primary_key < 100");
    mysql_fetch_all($show_all, 2);
    echo "</p></div>";
}
 
I think when i make the first call it is erasing the contents of the array from my $show_all SELECT statement. I have to uncomment the second $show_all in order for the function mysql_fetch_all to return the #2 data.

I don't want to have to make another query to call the function again (unless there isn't another way). Thanks in advance...

Re: stopping an array from clearing data after function call

Posted: Fri Apr 24, 2009 3:45 pm
by John Cartwright
If you want to repeadetly loop a result set you must reset the internal pointer after your finished looping the entire dataset. I.e.

Code: Select all

function mysql_fetch_all($result) { //removed weird $number param
   $return = array();
   while($row=mysql_fetch_array($result)) {
      $return[] = $row;
   }
   mysql_data_seek($result, 0); //reset the internal pointer
   return $return;
}
 
However, I'm confused on what you are doing with the row number. Really, you should only call this function once, collect the returned data, then you can do whatever you want with the array from there.

Code: Select all

function showTable($table_name, $primary_key, $index, $secondary_column, $div_header, $div_header2, $divID, $divID2){
    $show_all = mysql_query("SELECT * FROM $table_name WHERE $primary_key < 100");
    $rows = mysql_fetch_all($show_all);
    
    echo "<div id='$divID'><h4>".$div_header."</h4></div>";
    echo "<div id='$divID2'><h4>". $div_header2."</h4><p>";
     
    foreach ($rows as $row) {
        echo $row['your_column_name'] .'<br>';
    }
 
   echo '</p></div>';
 
}

Re: stopping an array from clearing data after function call

Posted: Fri Apr 24, 2009 4:08 pm
by kaiser0427
"However, I'm confused on what you are doing with the row number. Really, you should only call this function once, collect the returned data, then you can do whatever you want with the array from there."
with the row number 1($index) i'm accessing the names of vendors and with number 2 i'm accessing what shipping option ($secondary_column).

I have a form where a client inputs the information. This function is getting the contents of the database and displaying them below the form where the client knows what he's already entered.

Re: stopping an array from clearing data after function call

Posted: Fri Apr 24, 2009 8:04 pm
by kaiser0427
added this line to the beginning of the fetch_all function and it's working perfectly

Code: Select all

 mysql_data_seek($result, 0);