Topic: php loop through database and limit number of entries? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=30920" title="Pages that link to Topic: php loop through database and limit number of entries? (Page 1 of 1)" rel="nofollow" >Topic: php loop through database and limit number of entries? <span class="small">(Page 1 of 1)</span>\

 
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 03-19-2009 01:53 Edit Quote

Going to try to explain this as best I can. I have a bunch of stuff in a database and I need to display this info within a table. This isn't a problem at all but, I need it to only display 3 td tags per line and then move on to the row.

so i need it to do :

code:
<table>
<tr>
<td>My First Row</td>
<td>My First Row 2</td>
<td>My First Row 3</td>
</tr>

<tr>
<td>My Second Row</td>
<td>My Second Row 2</td>
<td>My Second Row 3</td>
</tr>
</table>



and continue to do this through every entry in the database table.

so I have something like this that will display the info:

code:
for ($i = 1; $i <= $num; $i++){
	
		$chairArray = mysql_fetch_array($chairResult);
		$chairName = $chairArray['name'];
		$chairDescription = $chairArray['description'];
		$chairImage = $chairArray['image'];
		
		echo "<tr>";
		echo "<td>";
		echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";
		echo "<br />";
		echo $chairName;
		echo "</td>";
		echo "</tr>";
		
		
	}



Rough example

So how can I make it do something like what I have and only display 3 per line?

Thanks in advance!!!

Later,

C:\

(Edited by CPrompt on 03-19-2009 02:02)

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

IP logged posted posted 03-19-2009 10:19 Edit Quote
code:
?> <tr><td>header</td><td>header</td><td>header3</td><?php //no </tr>...
for ($i = 1; $i <= $num; $i++){
	
		$chairArray = mysql_fetch_array($chairResult);
		$chairName = $chairArray['name'];
		$chairDescription = $chairArray['description'];
		$chairImage = $chairArray['image'];
		if ($i % 3 == 0)
{
     		        echo "</tr>";
                 		echo "<tr>";
}
		echo "<td>";
		echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";
		echo "<br />";
		echo $chairName;
		echo "</td>";
		
	}
?></tr><?php



Might have to adjust it a little bit since you foolishly start counting at 1, but you get the general idea...

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 03-19-2009 12:14 Edit Quote

thanks.

quote:

Tyberius Prime said:

since you foolishly start counting at 1



I said it was a rough sketch ;-)

Later,

C:\

Karl
Bipolar (III) Inmate

From: Phoenix
Insane since: Jul 2001

IP logged posted posted 03-23-2009 23:18 Edit Quote

When the loop is done, do not neglect to deal with the remainder of 3, so if you looped once through the last mod 3 test, you'd have a remainder of 2, you'd need to either:

a) colspan last td by remainder + 1 (well, depending on how you are counting)
b) create each additional td giving it some empty value

Karl..

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

IP logged posted posted 03-24-2009 20:35 Edit Quote

or just do

code:
for ($i = 0; $i <= $num -1; $i++){



Later,

C:\



Post Reply
 
Your User Name:
Your Password:
Login Options: Remember Me On This Computer
 
Your Text:
Loading...
Options: Show Signature
Enable Slimies
Enable Linkwords

« BackwardsOnwards »

Show Forum Drop Down Menu