Dynamically create table rows and Columns in php

Dynamically create table rows and Columns in php

Welcome to the In Pakainfo.com website! You will Step By Step learn web programming, easy and very fun. This website allmost provides you with a complete web programming tutorial presented in an easy-to-follow manner. Each web programming tutorial has all the practical examples with web programming script and screenshots available.

Free Live Chat for Any Issue

create table dynamically in php Example

<?php
$rows = 50; //define total no of rows
$collum = 14; //define total no of collum
echo "<table border='2'>";
for($tablerow=1;$tablerow<=$rows;$tablerow++){

    echo "<tr>";
        for($tabledata=1;$tabledata<=$collum;$tabledata++){
               echo "<td>row No: ".$tablerow." column No: ".$tabledata."</td>";
        }
    echo "</tr>";
}

echo "</table>";
?>

create table dynamically in php (Limit Max per row display)

<?php
$max_per_row = 4;
$item_count = 0;
echo "<h1>create table dynamically in php Example</h1>";
echo "<hr/>";

echo "<table border='2'>";
echo "<tr>";
$products = array(
    "a" => "first",
    "b" => "second",
    "c" => "something",
    "d" => "red",
	"e" => "green",
	"f" => "black"
); 
foreach ($products as $item)
{
    if ($item_count == $max_per_row)
    {
        echo "</tr><tr>";
        $item_count = 0;
    }
    echo "<td>".$item."</td>";
    $item_count++;
}
echo "</tr>";
echo "</table>";
echo "<hr/>";
?>

Dynamically create table rows and Columns in php Example

<?php
$products = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
?>
<table border="1" width="50%">
<tr>
<?php 
$procount = 0;
foreach($products as $data) {
    if($procount != 0 && $procount%3 == 0) {
?>
    </tr><tr>
<?php 
    }
?>
    <td>
        <?php echo $data; ?>
    </td>
<?php
    $procount++;
} 
?>
</tr>
</table>

how to create dynamic table in php mysql? Example

<?php
//Simple Product INITIALIZE VARIABLES display colsToDisplay
$colsToDisplay = 3;
$htmlOutput    = array();
 
//All the GET Product LIST
$sqlquery = "SELECT product_name, product_price, product_desc, pictureName FROM products ORDER BY productid DESC";
$dataresult = $mysqli->query($sqlquery);
while($rowdisplay = $dataresult->fetch_assoc()) {
     $htmlOutput[] = "<td><img src='images/{$rowdisplay['pictureName']}' alt='' /><br />{$rowdisplay['product_price']} {$rowdisplay['product_name']}</td>";
}
 
$collumcounter = count($htmlOutput) % $colsToDisplay;
if($collumcounter) 
{
     while($collumcounter < $colsToDisplay) 
	 {          
				$htmlOutput[] = '<td></td>';          
				$collumcounter++;      
	}
} 
//Simple DISPLAY TABLE
print '<table border="2">';
foreach($htmlOutput as $activerow) {
     print '<tr class="top">' . implode('', $activerow) . '</tr>';
}
print '</table>';
	 
?>

Download

Example