Php 在2列中获取结果
我想将mysql获取结果显示为2列。我在数据库中有Php 在2列中获取结果,php,mysql,Php,Mysql,我想将mysql获取结果显示为2列。我在数据库中有名称、说明和价格等记录。它作为 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>
名称
、说明
和价格
等记录。它作为
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 } ?>
“/>
价格:
Rs.
你可以使用这个功能
/**
*@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;
}
/**
*@param数组您的数组数据
*@参数整数如果1将是垂直的如果>1将是水平的
*@参数整数计数列
*@参数整数缩进量
函数drawTable($data、$type=1、$columns=10、$tabs=0)
{
$tbl=null;
如果($tabs==false)
{
$tr=$td=null;
}
其他的
{
$tr=“\n”.str\u重复(“\t”,$tabs);
$td=$tr.“\t”;
}
如果($type==1)
{
$all_columns=array_chunk($data,ceil(count($data)/$columns));
对于($i=0,$c=count($all_columns[0]);$i<$c;$i++)
{
$tbl.=$tr.';
对于($si=0;$si<$columns;$si++)
{
$tbl.=$td.''(isset($all_列[$si][$i])?$all_列[$si][$i]:'');
}
$tbl.=$tr.';
}
}
其他的
{
对于($i=0,$n=1,$d=ceil(计数($data)/$columns)*$columns;$i<$d;$i++,$n++)
{
如果($n==1)
$tbl.=$tr.';
$tbl.=$td.'。(isset($data[$i])?$data[$i]:'';
如果($n==$columns)
{
$n=0;
$tbl.=$tr.';
}
}
}
如果($tabs!==false)
$tbl.=“\n”;
返回$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
$data=array();
$query=mysql_query(“您的sql查询”);
while($row=mysql\u fetch\u row($query))
{
$data=array\u merge($data,$row);
}
echo“”。drawTable($data,1,2,0)。“”;//垂直输出
<代码> EXT/MySQL < /代码>已过时,不再使用。使用<代码> PdoyMySQL < /C> >或<代码> mySqLI>代码>。第二个TR在哪里结束?