Table loop help

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
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Table loop help

Post by paqman »

I have my page get all the info from a database, and I would like to display it in one table for each piece of data. The problem is the tables keep going vertically for the whole page. What loop would I use to make the page display 3 tables, then add a <br>? Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's "each piece of data" here? How is the data returned? Maybe you should post some code... If I understand correctly, all the data is returned in a single query. If so, you'll either have to analyze the data to find when the pieces change or know in some fashion where the data changes...
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

Code: Select all

mysql_connect("localhost","------------","-----------"); 
mysql_select_db("--------------"); 
$result = mysql_query("select * from usedtrucks ORDER BY make DESC");
while($r=mysql_fetch_array($result))
{	
	$id=$r["id"];
	$make =$r["make"];
	$model =$r["model"];
	$year = $r["year"];
	$engine = $r["engine"];
	$transmission = $r["transmission"];
	$frontaxle = $r["frontaxle"];
	$rearaxle = $r["rearaxle"];
	$cabin = $r["cabin"];
	$misc = $r["misc"];


}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how are they supposed to break into 3 tables?
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

I know what I want it to do, just not how to use a loop to achieve it. Basically have it go:

echo "<table><a href=\"popUp('file.php$id=$id')\"><tr><td><img src=\"$id.$ext\"></td></tr>
<tr><td>$make</td></tr>
<tr><td>$model</td></tr>
<tr><td>$model</td></tr></a></table>";

Have that for each one, but on every 3rd add a <br> after so it makes rows of 3. Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fyi, that's not 3 tables, but 3 columns ;)

PHP & MySQL formatting problem is from the useful posts thread.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$i=1;
while($array=mysql_fetch_assoc($result))
{
   if($i=3)
   {
       echo "<BR /><BR />";
       $i=0;
   }
   echo "your table here";
   $i++;
}
If I understood correctly :P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

okay that makes perfect sense, but now I have another challenge. This is wrong, but I'll use it to explain what I am trying to make it do:

Code: Select all

while($r=mysql_fetch_array($result))
{	
	$id=$r["id"];
	$make =$r["make"];
	$model =$r["model"];
	$year = $r["year"];
	$engine = $r["engine"];
	$transmission = $r["transmission"];
	$frontaxle = $r["frontaxle"];
	$rearaxle = $r["rearaxle"];
	$cabin = $r["cabin"];
	$misc = $r["misc"];
	$ext = $r["ext"];

// put 3 images in a row, then add a </tr><tr>
echo "<td><a href=\"javascript:popUp('pop_usedtrucks.php?ind=$id');\"><img src=\"image/usedtrucks/thumb/$id.$ext\" border=0></a></td>";
if($a == 3) { 
echo "</tr><tr>"; 
$a = 0; } 
// put 3 $makes in a row, then add a </tr><tr>
echo "<a href=\"javascript:popUp('pop_usedtrucks.php?ind=$id');\">$make</a>";
if($b == 3) { 
echo "</tr><tr>"; 
$b = 0; } 
// put 3 $model in a row, then add a </tr><tr>
echo "<a href=\"javascript:popUp('pop_usedtrucks.php?ind=$id');\">$model</a>";
if($c == 3) { 
echo "</tr><tr>"; 
$c = 0; } 
// put 3 $year in a row, then add a </tr><tr>
echo "<a href=\"javascript:popUp('pop_usedtrucks.php?ind=$id');\">$year</a>";
if($d == 3) { 
echo "</tr><tr>"; 
$d = 0; } 

$a++;
$b++;
$c++;
$d++;

}
I know I have the concept backwards here, I just haven't ever used loops before and I think thats what I need to use right now. Anyone who feels like helping me with this would be greatly appreciated.
Post Reply