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:)

Upvotes: 0

Views: 2142

Answers (5)

@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 } ?>

Upvotes: 0

Muhammad Nouman
Muhammad Nouman

Reputation: 190

<?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 } ?>

Upvotes: 1

Peter van den Broek
Peter van den Broek

Reputation: 191

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.

Upvotes: 0

nerdlyist
nerdlyist

Reputation: 2847

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.

Upvotes: 0

Ahmad ghoneim
Ahmad ghoneim

Reputation: 929

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 } ?>

Upvotes: 0

Related Questions