Jump to content

simple looping question


ibinod

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/170102-simple-looping-question/
Share on other sites

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++;
}
?>

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

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>

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.