如何创建一个从PHP数组一个HTML表? [英] How to create a HTML Table from a PHP array?

查看:144
本文介绍了如何创建一个从PHP数组一个HTML表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从一个PHP数组创建一个HTML表?与标题为A表的标题价格数字

  $ =铺阵列(
    阵列(玫瑰,1.25,15)
    阵列(菊花,0.75,25)
    阵列(兰花,1.15,7)
);


解决方案

这将是最好只把数据放到数组是这样的:

 < PHP
$ =店铺阵列(阵列(称号=>中玫瑰,价格=> 1.25,数字=→15)
               阵列(称号=>菊花,价格=> 0.75,数字=→25)
               阵列(称号=>中的兰花,价格=> 1.15,数字=大于7)
             );
?>

然后做这样的事情,当你在数据库中后期添加更多的列到你的表应该很好地工作,甚至。

 < PHP如果(计数($店)0):??>
<表>
  <&THEAD GT;
    &所述; TR>
      百分位>< PHP的回声破灭('< /第i<第i',array_keys(电流($店))); ?>< /第i
    < / TR>
  < / THEAD>
  <&TBODY GT;
?< PHP的foreach($店作为$行):array_map('ヶ辆',$行); ?>
    &所述; TR>
      < TD>< PHP的回声破灭('< / TD>< TD>',$行); ?>< / TD>
    < / TR>
< PHP endforeach; ?>
  < / TBODY>
< /表>
< PHP ENDIF; ?>

How do I create a HTML table from a PHP array? A table with heading as 'title', 'price', and 'number'.

$shop = array(
    array("rose",   1.25, 15),
    array("daisy",  0.75, 25),
    array("orchid", 1.15, 7 ),
); 

解决方案

It would be better to just fetch the data into array like this:

<?php
$shop = array( array("title"=>"rose", "price"=>1.25 , "number"=>15),
               array("title"=>"daisy", "price"=>0.75 , "number"=>25),
               array("title"=>"orchid", "price"=>1.15 , "number"=>7) 
             ); 
?>

And then do something like this, which should work well even when you add more columns to your table in the database later.

<?php if (count($shop) > 0): ?>
<table>
  <thead>
    <tr>
      <th><?php echo implode('</th><th>', array_keys(current($shop))); ?></th>
    </tr>
  </thead>
  <tbody>
<?php foreach ($shop as $row): array_map('htmlentities', $row); ?>
    <tr>
      <td><?php echo implode('</td><td>', $row); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>
<?php endif; ?>

这篇关于如何创建一个从PHP数组一个HTML表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆