繁体   English   中英

在2列中提取结果

[英]fetch result in 2 columns

我想将mysql提取结果显示为2列。 我在数据库中有NameDescriptionPrice 返回为

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] : '&nbsp;').'</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] : '&nbsp;').'</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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM