swamp Posted June 25, 2009 Share Posted June 25, 2009 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 More sharing options...
Alex Posted June 25, 2009 Share Posted June 25, 2009 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 More sharing options...
KevinM1 Posted June 25, 2009 Share Posted June 25, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.