I want to create dynamic rows and column with the help of PHP and HTML but I am little confused about this code so some help is definitely appreciated.
<table>
<?php
$tr = 0;
foreach ($data as $db_data) {
$tr++;
if ($tr == 1) {
echo "<tr>";
}
echo "<td>";
echo $db_data['id'];
echo "</td>";
}
if($tr == 2){
}
?>
</table>
Scenario is so simple:
Mysql data return 6 no of records from for-each loop the result will be show like this image
Same way the Mysql data return 3 no of records the result will be show like this image
Something like this, maybe
function create_table($data) {
$res = '<table width="200" border="1">';
$max_data = sizeof($data);
$ctr = 1;
foreach ($data as $db_data) {
if ($ctr % 2 == 0) $res .= '<td align="center">' . $db_data['id']. '</td></tr>';
else {
if ($ctr < $max_data) $res .= '<tr><td align="center">' . $db_data['id']. '</td>';
else $res .= '<tr><td colspan="2" align="center">' . $db_data['id']. '</td></tr>';
}
$ctr++;
}
return $res . '</table>';
}
Course, you can modify style of table to fit your needs.
Call it like this:
echo create_table($data);
(example for 7, 4, 3 and 8 id's)
It returns table with same number of rowsin each column if you pass even number of id's or table where last row is merged if you pass odd number of id's into function.
Use % php operator (division remainder) to break rows where you need
Do something like this:
foreach ($data as $ord => $db_data)
{
if (($ord == count($data) - 1) && (count($data) % 2))
{
// Do a colspan of 2, as it is the last item (first clause)
// and there are an odd number of items (second clause)
}
}
i think you should try doing it in a while loop . for example
$sql = your.sql.query;
$row = mysql_query($sql);
$result = mysql_fetch_arrey($row)
<table>
while($row!=0) {
echo "
<tr>
// number of <td> you need according to your data return in the query:
<td>$result['column']</td>
<td>$result['column']</td>
</tr>
";
}
if i understand what you're trying to do.
Here's a better way. This one uses Bootstrap syntax, but you can easily change it to a table
format or anything else. Logic is the same. Remember to update the $items
array name with your own where appropriate.
<div class="container">
<div class="row">
<?php
// Sample array
$items = array(1, 2, 3, 4, 5, 6, 7, 8);
// Define how many columns
$cols = 6;
$tdCount = 1; // Don't change
foreach ($items as $key => $item)
{
?>
<div class="col"><?php echo $key; ?></div>
<?php
// Close and open new rows
if( (($key + 1) % $cols) === 0 && ($key + 1) !== count($items) )
{
?>
</div>
<div class="row">
<?php
}
// Increment column count
$tdCount++;
// Fill with empty columns at the end
if( ($key + 1) === count($items) && $tdCount <= $cols )
{
$spacers = $cols - $tdCount;
for ($i = $spacers; $i >= 0 ; $i--)
{
?>
<div class="col spacer"> </div>
<?php
}
}
// Reset columns count
if( $tdCount > $cols )
{
$tdCount = 1;
}
}
?>
</div>
</div>
It will output:
<div class="container">
<div class="row">
<div class="col">0</div>
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
</div>
<div class="row">
<div class="col">4</div>
<div class="col">5</div>
<div class="col">6</div>
<div class="col">7</div>
</div>
<div class="row">
<div class="col">8</div>
<div class="col">9</div>
<div class="col spacer"> </div>
<div class="col spacer"> </div>
</div>
</div>