ibinod Posted August 13, 2009 Share Posted August 13, 2009 Hi, I am building a gallery and i am stuck with this please help me with this ok lets assume that i have 100 entries in my database cats table each with id, pic, thumb feild now in my php i want to do like this i want to select 70 entries and while looping i want to loop through the first 12 records and break it there and again from the same loop i want it to continue what i want is i want to show thumbs for the first 12 records and only text for the rest records, so how do i do this Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/170102-simple-looping-question/ Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2009 Share Posted August 13, 2009 Ammend to fit your database <?php // get last 70 records $result = mysql_query("SELECT name, image FROM tablename ORDER BY id DESC LIMIT 70"); $counter = 1; while($row = mysql_fetch_assoc($result)) { // if counter < 12 display image else print name print (($counter < 12) ? "<img src='".$row['image']."' /><br />" : $row['name']."<br />"); $counter++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/170102-simple-looping-question/#findComment-897330 Share on other sites More sharing options...
ibinod Posted August 13, 2009 Author Share Posted August 13, 2009 Hi neil thanks for helping but it's not working as i want , i have done something like this but still i am having problem could you pls helpme i have come with this but still i am having some problem //$cats is an array containing database records <ul class="pics"> foreach($cats as $key => $cat) { if($key <12) { echo '<li>records with images'.$key.'</li>'; } else { echo '<li>records without images'.$key.'</li>'; } } </ul> this is it but i would like the second li with different ul class so how do i do it if i place ul after foreach it will loop <ul> and i don't want it i just want to change the class Quote Link to comment https://forums.phpfreaks.com/topic/170102-simple-looping-question/#findComment-897395 Share on other sites More sharing options...
JonnoTheDev Posted August 13, 2009 Share Posted August 13, 2009 Thats not really what you asked for what i want is i want to show thumbs for the first 12 records and only text for the rest records However you will need 2 loops. One for the first 12 and another for the rest <ul class="pics"> for($x = 0; $x < 12; $x++) { } </ul> <ul class="pics2"> for($x = 12; $x <= (count($cats)-12); $x++) { } </ul> Quote Link to comment https://forums.phpfreaks.com/topic/170102-simple-looping-question/#findComment-897407 Share on other sites More sharing options...
ibinod Posted August 13, 2009 Author Share Posted August 13, 2009 Hi, thanx again but $cats is an associative array so how do print it's contents on the first loop i want this to output like this <ul class="pics"> <li>this li for 12 times</li> </ul> <ul class="pics2"> <li>and this for the remaining time</li> </ul> i want to do this without breaking it Quote Link to comment https://forums.phpfreaks.com/topic/170102-simple-looping-question/#findComment-897438 Share on other sites More sharing options...
ibinod Posted August 13, 2009 Author Share Posted August 13, 2009 thanx a lot neil i finally figured it out Quote Link to comment https://forums.phpfreaks.com/topic/170102-simple-looping-question/#findComment-897443 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.