Jump to content

Recommended Posts

Can someone help me with this loop?

 

Its just printing out the first item 10 times instead of looping through.

 

Thanks

 

$res =& $db->query('SELECT id, title FROM table');

 

$res->fetchInto($row, DB_FETCHMODE_ASSOC);

 

for ($i=0; $i < 10; $i++) {

 

?>

  <p><a href="/page/?item=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a></p>

 

<?php } ?>

Link to comment
https://forums.phpfreaks.com/topic/163633-loop/
Share on other sites

Edit: Is it really necessary to use those classes? If not you can just use:

 

$result = mysql_query('SELECT id, title FROM table LIMIT 10');
while($row = mysql_fetch_assoc($result))
{
     echo '<p><a href="/page/?item=' . $row['id'] . '">' . $row['title'] . '</a></p>';
}

Link to comment
https://forums.phpfreaks.com/topic/163633-loop/#findComment-863386
Share on other sites

Can someone help me with this loop?

 

Its just printing out the first item 10 times instead of looping through.

 

Thanks

 

$res =& $db->query('SELECT id, title FROM table');

 

$res->fetchInto($row, DB_FETCHMODE_ASSOC);

 

for ($i=0; $i < 10; $i++) {

 

?>

  <p><a href="/page/?item=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a></p>

 

<?php } ?>

 

Your fetch function likely only fetches one row at a time.  You'll need to call that function 10 times to get 10 different rows of data.

Link to comment
https://forums.phpfreaks.com/topic/163633-loop/#findComment-863388
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.