简体   繁体   中英

Foreach loop are displaying same rows multiple times

I have a problem with my first attempt at making a foreach loop.

My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table.

My code looks like this:

$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);

<?php foreach ($assoc_query as $value) { ?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr>
<?php } ?>

It's being displayed like so on the page:

/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK

Hope you can help me:)

<?php 
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);

$counter=0;
while($row= mysql_fetch_assoc($result))
{
    $assoc_query[$counter] = $row;
    $counter++;
}

foreach ($assoc_query as $value) { ?>
    <tr>
        <td>
            <div id='pageimg'><img src= <?php echo $value['pic'];?>  ></div>
        </td>
        <td>
            <div id="pagename"><?php echo $value['name']; ?> </div>
        </td>
    </tr>
<?php } ?>

this can't be done with foreach loop unless you gonna show 1 user's information only using limit Clause or Where Clause otherwise that it's useless

instead you need a while loop like this

<?php while ($assoc_query = mysql_fetch_assoc($result)) { ?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr>
<?php } ?>

Instead of foreach you will want a while. What you are doing here is looping the elements of array $assoc_query.

A while loop will get the next result set to be used.

while($assoc_query = mysql_fetch_assoc($result)){
    //Do your thing
}

Also please consider updating from mysql_ to mysqli_ or PDO. What you are using is depreciated and there is added security with the newer ones.

You should do it like this:

$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr>
<?php } ?>

That should work. But as said before you shouldn't code like this. Use mysqli or PDO like nerdlyist says.

@Nerdlyist

$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);


<?php while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
   <td>
     <div id='pageimg'><img src= <?php echo $assoc_query['pic'];?>  ></div>
   </td>
   <td>
     <div id="pagename"><?php echo $assoc_query['name']; ?> </div>
   </td>
</tr> 
<?php } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM