[英]fetch result in 2 columns
我想將mysql提取結果顯示為2列。 我在數據庫中有Name
, Description
和Price
。 返回為
Name
Des
Price
Name
Des
Price
但是我想要的是這樣的:
Name Name
Des Des
Price Price
Name Name
Des Des
Price Price
我的代碼如下:
<?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 } ?>
您可以使用此功能
/**
*@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;
}
如何使用
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.