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 } ?> Quote 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>'; } Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/163633-loop/#findComment-863388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.