【问题标题】:Populate html table with php multidimensional array用php多维数组填充html表
【发布时间】:2012-11-20 23:31:01
【问题描述】:

我想将MySQL 表中的数据放入(HTML Table)。一切都是动态的。产品名称并不总是相同(列)。记录数并不总是相同(行)。

MySQL 表(填充 ROWS 数据)

 iduser       idprod      quantity         date
  1234          65          60      2012-11-30 09:13:41.628
  1234          66          50      2012-11-30 09:13:41.628
  1234          64          80      2012-11-30 09:13:41.628

MySQL 表(填充 COLUMNS 数据)

idprod  name                     date
66      Panettone de Truffas     2012-11-29 15:19:41
65      Panettone de Maracujá    2012-11-29 15:16:56
64      Panettone de Brigadeiro  2012-11-29 15:16:44

代码

    $panettones = array();
    $querySel = "SELECT * FROM registro_panettone";
    $resultSel = mysql_query($querySel);
    $rows = mysql_num_rows($resultSel);
    $panettones = array();
    for($i=0;$i<$rows;$i++) {
        /*$panettones["id_user"] = mysql_result($resultSel,$i,0);
        $panettones["id_pan"] = mysql_result($resultSel,$i,1);
        $panettones["qtd"] = mysql_result($resultSel,$i,2);
        $panettones["data"] = mysql_result($resultSel,$i,3);*/
        $retorno = mysql_fetch_array($resultSel);
        $texto = "<tr>";
        $texto .= "<td>".$retorno[0]."</td>";
        $texto .= "<td>".$retorno[1]."</td>";
        $texto .= "<td>".$retorno[2]."</td></tr>";
        echo $texto;
}

目前的样子:

http://www.cacaushow.net.br/panettone_2012/rel.php

我希望它看起来像:

http://www.cacaushow.net.br/panettone_2012/wish.php

有什么建议吗?

【问题讨论】:

  • 将缺少的(空)列添加到您的数组生成中,以便您的数据列与您的标题计数相匹配。
  • 我强烈建议您转至PDO。从长远来看,这将使您的生活更轻松,同时考虑到 mysql_ 命令已被弃用。 php.net/manual/en/mysqlinfo.api.choosing.php
  • @dognose 可能不行,问题出在reference。把东西放好。将关于代码 65 的数据放入 id = 65 的 'td' 中。

标签: php html mysql arrays html-table


【解决方案1】:

如果您使用的是 html 表(就像您在示例中所做的那样),您应该始终首先检查 mysql 字段是否为空。如果是用 &nbsp 替换它;价值。空表格单元格(在某些浏览器中)倾向于显示与您的第一个示例类似的结果。

希望这会有所帮助...

【讨论】:

    【解决方案2】:
    <td>&nbsp;</td>;
    

    只要你有空值而不是在那里不做任何事情。和

    在每个外部 for 循环之后
    <table border="1" cellspacing="0" cellpadding="5" width="100%">
        <tr>
            <td><b>Código</b></td>
            <td id='66'><b>Panettone de Truffas</b></td>
            <td id='65'><b>Panettone de Maracujá</b></td>
            <td id='64'><b>Panettone de Brigadeiro</b></td>
            <td><b>Data</b></td>
        </tr>
        <tr>
            <td>1234</td>
            <td>60</td>
            <td>2012-11-30 09:13:41.628</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>1234</td>
            <td>50</td>
            <td>2012-11-30 09:13:41.628</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 2014-09-02
      • 2016-02-18
      • 2012-11-13
      • 1970-01-01
      • 2011-01-11
      相关资源
      最近更新 更多