user3187898
user3187898

Reputation: 11

PHP - How to display multiple rows in two different places?

I'm new to PHP and I wanted to know if this is done correctrly. I want to display multiple rows from a table in two different places from the website:

<ul>
    <li><input type="text" name="first" id="first" />
        <ul>
            <?php 
                while($row = mysql_fetch_assoc($sql)){
                    $name = $row["name"];
                    echo <li>$name</li>;
                }

            ?>
        </ul>
    </li>
    <li>Vs.</li>
    <li><input type="text" name="second" id="second" />
        <ul>
            <?php 
              while($row = mysql_fetch_assoc($sql)){
                  $name = $row["name"];
                  echo "<li>$name</li>";
              }                                 
            ?>
        </ul>
    </li>
    <li><input type="submit" name="submit" id="submit" value="Compare!"/></li>
</ul>

The first while works perfectly, but the second one does not display anything.

Upvotes: 0

Views: 144

Answers (4)

hellcode
hellcode

Reputation: 2698

You could solve it in this way (and replace your mysql_* functions after that).
Instead of echoing the results directly put them in a variable and echo it twice (or more often if you like):

<ul>
    <li><input type="text" name="first" id="first" />
        <ul>
            <?php
                $list = "";
                while($row = mysql_fetch_assoc($sql)){
                    $name = $row["name"];
                    $list.= "<li>$name</li>";
                }
                echo $list;
            ?>
        </ul>
    </li>
    <li>Vs.</li>
    <li><input type="text" name="second" id="second" />
        <ul>
            <?php 
              echo $list;                                
            ?>
        </ul>
    </li>
    <li><input type="submit" name="submit" id="submit" value="Compare!"/></li>
</ul>

Upvotes: 1

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40899

In your first loop you're taking all the rows from the buffer that is used by mysql_fetch_assoc() - that's why that loop ends in the first place. Whey you run the second loop there are no more records in the buffer.

The easiest solution would be to first iterate through the buffer and save the data to an array, and then use that array in the 2 places you need it:

$rows = array();
while($row = mysql_fetch_assoc($sql)) $rows[] = $row;

And then use it in both places where you need that data:

foreach ($rows as $row) {
    $name = $row["name"];
    echo "<li>$name</li>";
}   

Keep in mind that mysql_ functions have been deprecated as of PHP 5.5.0 and you should use mysqli_ functions or PDO class instead.

Upvotes: 1

Marc B
Marc B

Reputation: 360572

Once you finish "consuming" a result set, it's done. Your second loop will always fail, as the first loop has used up all the rows. You should 'cache' the results, e.g.

while(... fetch from db ...) {
   $data = ...; // build whatever you need
}


echo $data; // first output spot
...
echo $data; // second output spot

Upvotes: 1

Jay Blanchard
Jay Blanchard

Reputation: 34406

The first while gets to the end of the array so you have to reset the pointer using mysql_data_seek.

As mentioned though, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's really not hard.

If you use PDO it is easier to place all of the results in an array that can be easily navigated and/or reset and re-used when needed.

Upvotes: 0

Related Questions