Table loop help
Moderator: General Moderators
Table loop help
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.
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"];
}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.
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
fyi, that's not 3 tables, but 3 columns 
PHP & MySQL formatting problem is from the useful posts thread.
PHP & MySQL formatting problem is from the useful posts thread.
Code: Select all
$i=1;
while($array=mysql_fetch_assoc($result))
{
if($i=3)
{
echo "<BR /><BR />";
$i=0;
}
echo "your table here";
$i++;
}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.
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:
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.
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++;
}