i want to show mysql fetch result into 2 coloumns. I have records in database as Name
, Description
and Price
. It is returned as
Name
Des
Price
Name
Des
Price
But what I would like as a layout is this:
Name Name
Des Des
Price Price
Name Name
Des Des
Price Price
My code is as follows:
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td><img src="<?php echo $row['picture']?>" /><br />
<b><?php echo $row['name']?></b><br />
<?php echo $row['description']?><br/>
Price:<big style="color:green">
Rs.<?php echo$row['price']?></big><br /><br />
<input type="button" value="Add />
</td>
</tr>
<tr><td colspan="2"><hr size="1" /></td>
<?php } ?>
You can use this function
/**
*@param array your array data
*@param integer if 1 will wertical if > 1 will horizontal
*@param integer count columns
*@param integer amount of indentation
function drawTable($data, $type=1, $columns=10, $tabs=0)
{
$tbl = null;
if($tabs === false)
{
$tr = $td = null;
}
else
{
$tr = "\n".str_repeat("\t", $tabs);
$td = $tr."\t";
}
if($type == 1)
{
$all_columns = array_chunk($data, ceil(count($data) / $columns));
for($i = 0, $c = count($all_columns[0]); $i < $c; $i++)
{
$tbl .= $tr.'<tr>';
for($si = 0; $si < $columns; $si++)
{
$tbl .= $td.'<td>'.(isset($all_columns[$si][$i]) ? $all_columns[$si][$i] : ' ').'</td>';
}
$tbl .= $tr.'</tr>';
}
}
else
{
for($i = 0, $n = 1, $d = ceil(count($data) / $columns) * $columns; $i < $d; $i++, $n++)
{
if($n == 1)
$tbl .= $tr.'<tr>';
$tbl .= $td.'<td>'.(isset($data[$i]) ? $data[$i] : ' ').'</td>';
if($n == $columns)
{
$n = 0;
$tbl .= $tr.'</tr>';
}
}
}
if($tabs !== false)
$tbl .= "\n";
return $tbl;
}
How to use
$data = array();
$query = mysql_query('Your sql query');
while($row = mysql_fetch_row($query))
{
$data = array_merge($data, $row);
}
echo '<table>'.drawTable($data, 1, 2, 0).'</table>'; // Vertical output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.