While loop and mysql_fetch_assoc on one query?

12,619

Solution 1

If I understand you need to use this function

http://php.net/manual/en/function.mysql-data-seek.php

to move your pointer

Solution 2

The only proper way as follows:
first, collect your data into array

$data = array();
while($row = mysql_fetch_array($query){
   $data[] = $row;
}

then, call a template
and use your data as many times as you wish:

<table>
<? foreach ($data as $row): ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['name']?></td>
<? endforeach ?>
</table?>
Share:
12,619
PHPLOVER
Author by

PHPLOVER

Updated on June 04, 2022

Comments

  • PHPLOVER
    PHPLOVER almost 2 years

    I have a webpage that does a MySQL query on a table. I have it to echo it out in a table and that works as it should, example:

    $query = mysql_query("SELECT * FROM table");
    
    while($row = mysql_fetch_array($query){
       // my table and db stuff echos out here
    }
    

    Now using the same mysql query $query I am trying to echo it out again on the same page using the same query underneath but my problem is it does not seem to work.

    Now you maybe thinking what I'm doing is odd, but the reason why is the first code above echoes data out in a table, but also checkboxes next to the stuff echoed out as it is for a form. All that works fine but it seems I cannot do another while loop above on the same query. The second one is exactly the same as above; only difference is it's not a form.

    Can I only do a while() and mysql_fetch_assoc once on a single query?

    UPDATE:

    I'm sorry I still don't understand properly.

    Here's my code; could anyone edit it for me?

    (I could not put php tags in the code to split up the HTML from the PHP code. Sorry for any inconvenience).

    $q = mysql_query("SELECT * FROM table");
    
    <h1> Vote for your favourite extension </h1>
    
    <form method="post" action="<?php echo basename(__file__); ?>">
    <table>
    <tbody>
        <tr class="odd">
            <td colspan="3" class="cellfeat" style="text-align: center;">Vote for your favourite extension</td>
        </tr>
            <?php
            if(!$q){
                // query failed etc
    
            } else { // query ok so display form
                while($row = mysql_fetch_array($q)){
                    echo '<tr class="odd">';
                    echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
                    echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
                    echo '<td class="cellfeat"><input type="checkbox" name="'.$row['id'].'" value="'.$row['id'].'" /></td>';
                    echo '</tr>';
                }
            }
            ?>
    </tbody>
    </table>
    <input type="submit" class="submitcontact" value="Vote" />
    </form>
    
    <h1>Extension Statistics</h1>
    
    <table>
        <tbody>
        <tr class="odd">
            <td colspan="3" class="cellfeat" style="text-align: center;">Voting Statistics</td>
        </tr>
                while($row = mysql_fetch_array($q)){
                    echo '<tr class="odd">';
                    echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
                    echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
                    echo '<td class="cellfeat">'.$row['count'].'</td>';
                    echo '</tr>';
                }
            ?>
        </tbody>
    </table>