johnmcg0000 Posted January 4, 2012 Share Posted January 4, 2012 How do you make php code repeat for a certain amount of times. I am only new to php and I am trying to make the code below repeat 6 times instead of repeating all the matching elements in the database. From my other coding experience I would use a for loop but as I am new to php I am not really sure how to implement the for and while loops together. I was trying to add an integer that incremented until it hit 6 and then would exit the while loop but I couldn't get it to work. This is probably a simple problem and any help would be appricated. Thanks. <?php $subject_set = mysql_query("Select * FROM movies WHERE media = 'Movies' ORDER BY dateadded DESC", $connection); if (!$subject_set){ die("Database connection failed: " . mysql_error()); } while ($subject = mysql_fetch_array($subject_set)){?> <div class="par_element"> <div class="repeat_horizontal"> <div class="repeat_hor_picture"> <a href="<?=$subject['link']?>"> <img src=" <?=$subject['picture']?>" /></a> </div> <a href=" <?=$subject['link']?>"><?= $subject['title']?>.</a> </div> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/254325-how-do-you-repeat-php-code-for-a-certain-number-of-times/ Share on other sites More sharing options...
trq Posted January 4, 2012 Share Posted January 4, 2012 If you only want 6 records, limit your query to 6 results. See http://dev.mysql.com/doc/refman/5.5/en/select.html, in particular the section an the LIMIT clause. Link to comment https://forums.phpfreaks.com/topic/254325-how-do-you-repeat-php-code-for-a-certain-number-of-times/#findComment-1304074 Share on other sites More sharing options...
johnmcg0000 Posted January 4, 2012 Author Share Posted January 4, 2012 I knew it was something easy like that. Thanks that worked perfectly. Looks like I have to study harder at this php. Link to comment https://forums.phpfreaks.com/topic/254325-how-do-you-repeat-php-code-for-a-certain-number-of-times/#findComment-1304078 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.