stopping an array from clearing data after function call

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kaiser0427
Forum Newbie
Posts: 9
Joined: Thu Apr 23, 2009 3:38 pm

stopping an array from clearing data after function call

Post 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...
Last edited by Benjamin on Fri Apr 24, 2009 8:18 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: stopping an array from clearing data after function call

Post 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>';
 
}
kaiser0427
Forum Newbie
Posts: 9
Joined: Thu Apr 23, 2009 3:38 pm

Re: stopping an array from clearing data after function call

Post 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.
kaiser0427
Forum Newbie
Posts: 9
Joined: Thu Apr 23, 2009 3:38 pm

Re: stopping an array from clearing data after function call

Post 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);
 
Post Reply