Page 1 of 1

Table loop help

Posted: Tue Sep 06, 2005 11:04 pm
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.

Posted: Tue Sep 06, 2005 11:17 pm
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...

Posted: Tue Sep 06, 2005 11:23 pm
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"];


}

Posted: Tue Sep 06, 2005 11:40 pm
by feyd
how are they supposed to break into 3 tables?

Posted: Wed Sep 07, 2005 12:07 am
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.

Posted: Wed Sep 07, 2005 12:12 am
by feyd
fyi, that's not 3 tables, but 3 columns ;)

PHP & MySQL formatting problem is from the useful posts thread.

Posted: Wed Sep 07, 2005 1:41 am
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

Posted: Wed Sep 07, 2005 6:11 pm
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.