Render SQL query results as an HTML table - php

I need help rendering data in PHP:
I want to use the results of a SQL query to populate a table, displaying images in a 4-column grid. Here's what I've written so far:
$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
while($row = mysql_fetch_array($result))
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}

<table><tr>
<?$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
$result =mysql_fetch_array($result)
$count=0;
for($row =0; $row<count($result); $row++)
{
$count++
if($count==4){
echo '</tr><tr>';
$count=0;
}
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}
?>
</tr>
</table>

You can do a for loop as was suggested by Frederick:
$num_rows = mysql_num_rows($result);
for($i = 0; $i < $num_rows; $i++)
{
// Check if beginning of row
if($i % 4 == 0)
{
//If not the first row, end the last row first
if($i > 0)
{
echo "</tr>";
}
echo "<tr>";
}
$row = mysql_fetch_assoc($result);
//Output each individual image
echo "<td> {*EACH IMAGE code*}</td>";
}
That should give you the idea.

Try this one:
$arr =array("Test1","Test2","Test3","Test4","Test5","Test6","Test7","Test8","Test9","Test10");
echo "<table><tr>";
for($i = 1 ; $i <= count($arr) ; $i++)
{
echo"<td>".$arr[$i-1]."</td>";
$last = $i % 4 == 0? "<tr/><tr>":"";
echo $last;
}
echo "</table>";

Try this, hope it works for you,
while($row = mysql_fetch_array($result))
{
$data[]= $row['image_name'];## Pass the image data to new array
}
$ImagePerRow = 4;### Set the value of image per table row
echo "<table border='1'><tr>";
foreach($data as $key => $ImageName)
{
if(($key+1) == $ImagePerRow)## if key index+1 is equal to ImagePerRow
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$ImageName."\" width=\"200\" height=\"170\" /></td></tr><tr>";## then close the table row and start a new table row
$ImagePerRow+=4;### Add a value of 4 in order to increment the imageperRow value on the next loop
}else{
echo "<td align=\"center\" >".$ImageName."</td>";### else echo the normal table data
}
}
echo "</tr></table>";

Related

Fill a table vertically in php

I'd like to fill a table vertically. For example one name on one row, the age on the second row alternately, and limit the table to 4 columns:
Is this possible with this kind of code?
Here is my Code:
<?php
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
echo "<table border=1>";
for($i=0;$i<count($age);$i++)
{
if ($i > 0 && $i % 4 == 0)
{
echo "</tr><tr>";
}
echo "<td>";
echo $name[$i];
echo "</td>";
echo "<td>";
echo $age[$i];
echo "</td>";
}
echo "</tr>";
?>
Please try this. It is not the most elegant way but will give you an idea and maybe improve your answer. I will suggest you maybe use multi-dimensional array to store age and name together.
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
$ageChunks = array_chunk($age, 4); //divide age array into chunks of 4
$nameChunks = array_chunk($name, 4); //divide name array into chunks of 4
//print table data
function printData($td)
{
echo "<td> ".$td." <td>";
}
//print table row. Assumption here is $nameChunks and $ageChunks are same length.
function printRow($nameChunks, $ageChunks)
{
foreach ($nameChunks as $key=>$val) {
echo "<tr>";
array_map("printData", $nameChunks[$key]);
echo "</tr><tr>";
array_map("printData", $ageChunks[$key]);
echo "</tr>";
}
}
//print table
function printTable($nameChunks, $ageChunks)
{
echo "<table>";
printRow($nameChunks, $ageChunks);
echo "</table>";
}
printTable($nameChunks, $ageChunks);
you need to change condition
if ($i > 0 && $i % 4 == 0) to if ($i > 0 && $i % 2 == 0) because you print 2 columns per iteration

want to display results in horizontal table using php mysql

I am using the following code to get results and want to have it displayed in two rows horizontially.
echo "<table border='0' width='700px' align='center'>";
echo "<tr>";
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
**echo "</tr><tr>";**
echo "<td class='search'>" . $number['count'] . "</td>";
}
echo "<tr>";
echo "</table>";
Hence I need this part of the code not to be included in the loop echo "";
Please help, this must be really simple.
I want the results to be displayed 10 across as I limit my query to
the top 10.
So it would be:
Number Number Number etc. Count Count Count etc.
Because the tables need to be drawn one row at a time, you could store all of your number values in one array and all of your count values in a second array, then loop through each array as you're building your rows. So first get your values from the database:
// let's get a variable with the total number of records, i.e. horizontal cells:
$totalRecords = 0;
// get values from db:
while($number = mysqli_fetch_array($result2))
{
$numberArr[] = $number['number'];
$countArr[] = $number['count'];
$totalRecords++;
}
Then in another set of loops render the table (one loop for each row):
// first row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) // you could also use count($numberArr) instead of $totalRecords
{
echo "<td class='ball_p'>" . $numberArr[$i] . "</td>";
}
echo "</tr>";
// second row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i)
{
echo "<td class='ball_p'>" . $countArr[$i] . "</td>";
}
echo "</tr>";
What about this:
$i = 0;
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
while($i++ >= 1)
{
echo "</tr><tr>";
$i = 0;
}
echo "<td class='search'>" . $number['count'] . "</td>";
}
I haven't tested it but the ++ after the $i in the second while loop should increment it after the evaluation which should skip the </tr><tr> the first time and print it the second.

How can I generate HTML table with PHP

Created a script that generates a table with 10 rows and 2 cols.
I can't make the cells generate natural numbers nonzero, and divisible by 5 distributed as the following table:
This is my code:
<?php
$rows = 10;
$cols = 2;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
//generated cols and rows, now the calculus
if ($tr/5!=0) {
echo "<td>".($tr*5)."</td>";
} else {
echo "<td>".($tr*5)."</td>";
}
}
echo "</tr>";
}
echo "</table>";
?>
Where did I go wrong, how can I make it go from row to row like the image ?
You are making it more complicated than needed. Just add 5 to a counter each cell
Is this what you want?
$rows = 10;
$cols = 2;
echo "<table border='1'>";
$val=0;
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
$val=$val+5;
echo "<td>".$val."</td>";
}
echo "</tr>";
}
echo "</table>";

Table designing in while loop

I want to show 4 images from database in a table (2*2) I know how to do that. But I have also special condition (if there are less picture than 4, it will show default pictures to show 4 images totally)
I manage this in a while loop. If there are 4 images there is no problem, it works as I want but when there are fewer images (which means default images will be completed 4 images in total) it doesn't work. I can't figure out how to do that.
Any help?
<table>
<tr>
<?php
$todisplay = 4;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT 0,4;");
while ($row = mysql_fetch_array($query)) {
$x++;
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
$displayed_number++;
if ($x % 2==0) {
echo "</tr><tr>";}
}
echo str_repeat("<td>
<img src='images/png/defthumb.png'> </td>", $todisplay-$displayed_number);
?>
</tr></table>
You were not far from it - simply create another loop that does the same thing, but with the default image instead. Also, the $displayed_number seems to hold the same value as $x, so I deleted it.
<table>
<tr>
<?php
$todisplay = 4;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT 0,4;");
$x = 0;
while ($row = mysql_fetch_array($query)) {
$x++;
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
if ($x % 2==0) {
echo "</tr><tr>";}
}
while ( $x < $todisplay) {
$x++;
echo "<td><img src='images/png/defthumb.png'/></td>";
if ($x % 2==0) {
echo "</tr><tr>";}
}
?>
</tr></table>
Instead of looping over the rows returned by your query, why not just loop four times and attempt to fetch a row instead?
<table>
<tr>
<?php
$tablerows = 2;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT " + ($tablerows * 2) + ";");
for ($x = 1; $x <= ($tablerows * 2); $x++) {
if ($row = mysql_fetch_array($query)) {
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
} else {
echo "<td><img src='images/png/defthumb.png'></td>";
}
if ($x % 2==0 && $x != $tablerows * 2) {
echo "</tr><tr>";}
}
?>
</tr></table>

Make a new table row every 2 echoed

I'm trying to make a new table row every time a user is echoed.
Here is what I've tried to zero success.
<?
$i = 0;
while($row = mysql_fetch_array($admin, MYSQL_ASSOC)){
if($i <= 2){
$i++;
echo "<tr><td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' />";
}
else{
echo "<td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' /></td></tr>"
$i=0;
}
}
?>
Any ideas peeps?
This should work:
<?
$i = 0;
echo "<table><tr>";
while($row = mysql_fetch_array($admin, MYSQL_ASSOC)) {
if($i++ % 2 == 0 && $i > 1) {
echo "</tr><tr>";
}
echo "<td>".$row['id']."</td><td>".$row['ip']."</td><td>".$row['username']."</td><td>".$row['password']."</td><td><img src='uploads/avatars/".$row['avatar']."' style='width:25px;height:25px;' /></td>"
}
echo "</tr></table>";

Categories