Code Problem: Limit results in a more effective way?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dawho9
Forum Newbie
Posts: 4
Joined: Mon Jun 17, 2002 5:55 pm

Code Problem: Limit results in a more effective way?

Post by dawho9 »

Hi,

I am fairly new to PHP, but picking up some ideas fairly quickly. I have a site that is completely PHP driven and works real well, but now I need some help optimizing one area.

In one section, I am search for results and putting thumbnails into table. I want to have four thumbnails per table row. Below is how I did it, very ugly. Kinda works. Is there a better way?

You can see the page in work at:

http://baby.brynteson.info/pics.php

Thanks and if you need more source code, I'll send the entire file.

Thanks again,

Richard

Code: Select all

$result = mysql_query("SELECT * FROM pics where pictype = '$pictype' order by id LIMIT 0, 4",$db); 
$result2 = mysql_query("SELECT * FROM pics where pictype = '$pictype' order by id LIMIT 4, 4",$db); 
$result3 = mysql_query("SELECT * FROM pics where pictype = '$pictype' order by id LIMIT 8, 4",$db); 
printf("<div style='POSITION: absolute; TOP: 125px; LEFT: 75px; Z-INDEX: 25; WIDTH: 500px'>\n"); 

printf("<TABLE border='1' cellspacing='2' cellpadding='2'> 
<TR> 
<TD colspan='9' bgcolor='#d62261'> 
Pictures 
</TD> 
</TR> 
<tr> 
\n"); 

while ($myrow = mysql_fetch_array($result)) &#123; 

printf(" 
<td BGCOLOR='#ffffff'> 
<center> 
<a href="pics.php?id=%s"><img src='images/%s'><br> 
%s 
</center> 
</a> 
</td> 

\n", $myrow&#1111;"id"], $myrow&#1111;"thumbname"], $myrow&#1111;"short"]); 

&#125; 

printf("</tr><tr>\n"); 

while ($myrow2 = mysql_fetch_array($result2)) &#123; 

printf(" 
<td BGCOLOR='#ffffff'> 
<center> 
<a href="pics.php?id=%s"><img src='images/%s'><br> 
%s 
</center> 
</a> 
</td> 

\n", $myrow2&#1111;"id"], $myrow2&#1111;"thumbname"], $myrow2&#1111;"short"]); 

&#125; 

printf("</tr><tr>\n"); 

while ($myrow3 = mysql_fetch_array($result3)) &#123; 

printf(" 
<td BGCOLOR='#ffffff'> 
<center> 
<a href="pics.php?id=%s"><img src='images/%s'><br> 
%s 
</center> 
</a> 
</td> 

\n", $myrow3&#1111;"id"], $myrow3&#1111;"thumbname"], $myrow3&#1111;"short"]); 

&#125;
Peter
Forum Commoner
Posts: 28
Joined: Mon Jun 10, 2002 12:40 am
Location: Brisbane, Australia

Post by Peter »

You could select all the images where pictype='$pictype'

Then when you have the multidimensional array of data you define $counter...

$counter = 0;

then use this code...

for($i=0;$i<count($datafromdatabase);$i++,$counter++){

this make a new row everytime there is a fourth cell...
if($counter % 4 == 0){
echo "</tr><tr>";
}

echo "<td><picture url or whatever></td>";
}


Hopefully thats understandable and what you want. :)
Post Reply