Jump to content

Populate table from database


takisis666

Recommended Posts

I am populating a table from a database. The data is being retrieved correctly however it is not being displayed correctly. This is how I want it to be displayed:

 

+----------------------------------------------------+

| vehicle 1 | vehicle 2 | vehicle 3 | vehicle 4 |

|-----------------------------------------------------|

| vehicle 5 | vehicle 6 | vehicle 7 | vehicle 8 |

|-----------------------------------------------------|

| vehicle 9 | veh 10    | veh 11     | veh 12    |

+----------------------------------------------------+

 

This is what I am getting:

 

+----------------------------------------------------+

| vehicle 1 | vehicle 1 | vehicle 1 | vehicle 1 |

|-----------------------------------------------------|

| vehicle 2 | vehicle 2 | vehicle 2 | vehicle 2 |

|-----------------------------------------------------|

| vehicle 3 | vehicle 3 | vehicle 3 | vehicle 3 |

+----------------------------------------------------+

 

Here is my code:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

$conn=mysqli_connect("localhost","root","","ezwayautos");

if($conn)
{
    $sql="SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
    $result=mysqli_query($conn,$sql);
}

if(! $result )
{
  die('Could not get data: ' . mysqli_error($conn));
}	
	
$rows = 3;
$cols = 4;

echo "<table>";
while($row = mysqli_fetch_assoc($result))
{
	for($tr=1;$tr<=$rows;$tr++)
	{
    echo "<tr>";
	for($td=1;$td<=$cols;$td++)
		{
    	echo "<td height='160' valign='top' class='featured'>";
    	echo "<div class='Image-Thumbnail'>";
    	echo "<a href=''>";
    	echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
    	echo "</a>";
   		echo "</div> <a href=''>" .$row['vehicle_name']. "</a>";
    	echo "</td>";
		}
    echo "</tr>";
	}
}
echo "</table>";
?>

Please help

Link to comment
Share on other sites

That's because $row's value don't change until the next iteration happens in the while () loop.

 

Both of your for statements are executing within the while so $row doesn't change.

 

What you're looking for is a counter that checks if the current iteration of the while loop is divisible by 4 (because you want 4 columns) to then break and start a new row.

 

This is untested but I think this is what you're after.

echo '<tr>';
$counter = 0;
while($row = mysqli_fetch_assoc($result)) {
    echo "<td height='160' valign='top' class='featured'>";
    echo "<div class='Image-Thumbnail'>";
    echo "<a href=''>";
    echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
    echo "</a>";
    echo "</div> <a href=''>" .$row['vehicle_name']. "</a>";
    echo "</td>";

    if ($counter % 4 === 0)
        echo '</tr><tr>';
    $counter++;
}
echo '</tr>';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.