Home » Php » html – WHILE loops can't be used twice? PHP

html – WHILE loops can't be used twice? PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am new to PHP , I hope someone can help me. I have a table which contains “id” , “img” , “link” and “desc” in mysql database . I want it to echo all out into something like this :

    <div id="featured">
    <a target='_blank' href='link'><img src='image link1' title='description'/></a>
    <a target='_blank' href='link'><img src='image link2' title='description'/></a>
    <a target='_blank' href='link'><img src='image link3' title='description'/></a>
    <a target='_blank' href='link'><img src='image link4' title='description'/></a>
    </div>
    <div id="cap">
    <span class='desc' id='idnumber'>Description1</span>
    <span class='desc' id='idnumber'>Description2</span>
    <span class='desc' id='idnumber'>Description3</span>
    <span class='desc' id='idnumber'>Description4</span>
    </div>

PHP CODE:

<?php
  require_once "./dbconfig.php";
  opendb();
  $sql = "SELECT * FROM images ORDER BY id DESC"; 
  $query = querydb($sql);
?>
<div id="featured"> 
<?php
  while($row = mysql_fetch_array( $query )){
    echo "<a target='_blank' href='$row[link]'><img src='$row[img]' title='$row[desc]'/></a>";
  }
?>
</div>
<div id="cap">
<?php
  while($row = mysql_fetch_array( $query )){
    echo "<span class='desc' id='$row[id]'>$row[desc]</span>";
  }
closedb();
?>
</div>

Can WHILE being used twice or i am wrong? when i run this code , the second while loop is not working , the spans are not showing at all.

Please help.

How to&Answers:

The first loop consumes all data from mysql already, so there is nothing left in the second loop. You can store the rows data in the meanwhile however and then re-use that store.

<div id="featured"> 
<?php
$rows = array();
while($row = mysql_fetch_array( $query )){
  $rows[] = $row;
  echo "<a target='_blank' href='$row[link]'><img src='$row[img]' title='$row[desc]'/></a>";
}
?>
</div>
<div id="cap">
<?php
foreach($rows as $row){
  echo "<span class='desc' id='$row[id]'>$row[desc]</span>";
}
closedb();
?>
</div>

Answer:

Use this before your second while loop

mysql_data_seek($query,0);

Answer:

The DB reference ($query) acts as a pointer to the next unread record; when you call mysql_fetch_array(), it gets the record that is being pointed to, and moves the pointer to the next position.

Therefore, after looping through them all, the pointer will be pointing at the end of the record set, hence it returns false when you ask for the next record. Finishing the loop does not do anything else to the pointer; it remains pointing to the end of the record set.

So what you need to do is reset the pointer to the start of the data set before you can loop through it a second time.

The function to do this is mysql_data_seek();

Therefore you’d need the following line of code immediatly before your second loop:

mysql_data_seek($query, 0);

Hope that helps.

Answer:

Fromt the docs for mysql_fetch_array

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

The internal data point is still at the end when you try to use your second while loop. You have done nothing to reset it.

You can move it back to the start with mysql_data_seek

Answer:

You can use while twice, but see what it does:

It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.

Once mysql_fetch_array has no more rows to fetch, it will return false on every subsequent call – so your first while loop will stop looping, as the expression mysql_fetch_array( $query ) is false; and since it’s the same expression in the second while, that loop will never execute (as the expression evaluates to false).

What to do: If you want to loop over the results multiple times, I’d suggest to store the result rows into an array first, then loop over them. Simplified example:

$results = Array();
while ($row = mysql_fetch_rows($query)) {
  $results[] = $row;
}

foreach ($results as $row) {
  echo $row['something'];
}

Answer:

mysql_fetch_array will be empty because you have already fetched all of the rows from the database. You will need to reset the pointer using mysql_data_seek.

<?php
mysql_data_seek( $query, 0 );
while($row = mysql_fetch_array( $query )){
echo "<span class='desc' id='$row[id]'>$row[desc]</span>";
}
... rest of code

Answer:

With mysql_fetch_array() you are going through the results step by step, till you are at the end. Or as the PHP docs say:

Returns an array that corresponds to
the fetched row and moves the internal
data pointer ahead.

This means, you need to start from new. So, use this before your second while:

mysql_data_seek($query,0);

Answer:

try this

        <?php
         require_once "./dbconfig.php";
          opendb();
          $sql = "SELECT * FROM images ORDER BY id DESC"; 
          $query = querydb($sql);
           $query2 = querydb($sql);
         ?>
         <div id="featured"> 
         <?php
         while($row = mysql_fetch_array( $query )){
          echo "<a target='_blank' href='$row[link]'><img src='$row[img]'
 title='$row[desc]'/>     </a>";
          }
          ?>
       </div>
        <div id="cap">
         <?php
         while($row2 = mysql_fetch_array( $query2 )){
         echo "<span class='desc' id='$row2[id]'>$row2[desc]</span>";
         }
         closedb(); 
         ?>

Answer:

$str_res1 =”;
$str_res2 =”;
while($row = mysql_fetch_array( $query )){
$str_res1 .= ” a target=’_blank’ href=’$row[link]’ img src=’$row[img]’ title=’$row[desc]’ a”;
$str_res2 .= “span class=’desc’ id=’$row[id]’> $row[desc] span>”;
}

sorry i couldn’t complete html tags in my answer as it disappears from post if i write “<” or “/>”

by this you can take all values in php strings and you can echo it any where you want to .

Answer:

$stuff = mysql_query("SELECT * FROM tbl");

while($s = mysql_fetch_array($stuff)){
//ur code
}
// add this line
mysql_data_seek( $stuff, 0 );

while($r = mysql_fetch_array($stuff)){
//ur code
}