【问题标题】:Formatting Table with PHP使用 PHP 格式化表格
【发布时间】:2012-03-17 14:50:38
【问题描述】:

我不知道如何格式化我的表格,以便每个属性部分都有一个标题。当我实际上需要查看更多内容时,我只会看到一个标题。

例如,我能够获得 MOTOROLA 的 DROID 4,它的所有属性都从全零售价到语音拨号。语音拨号后应该有另一个电话的第二个标题。显然我的逻辑搞砸了,我已经为此苦苦挣扎了大约 4 个小时。我还需要两个表并排,而不是像这样的长列表。

这就是我想要实现的目标:

我的代码如下所示:

<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="theme.css" />
    </head>
    <body>
<?php

include_once 'config.php';

$conf = new config();

mysql_connect($conf->getdbServ(), $conf->getdbUser(), $conf->getdbPwd()) or die(mysql_error()); 
 mysql_select_db($conf->getDB()) or die(mysql_error());



 $selectedPhones = $_POST['phones'];


 $totalSelected = count($selectedPhones);
 //echo $totalSelected;
 $idList = "";
 for($i=0;$i < $totalSelected; $i++){
         $idList .= $selectedPhones[$i] . ",";
     }
 $idList = substr($idList,0,-1);


    $query = "Select name from ".$conf->getproductTbl()." WHERE id='$idList'";
    $res=mysql_query($query);



 //echo $idList;

 $data  = mysql_query("SELECT ".$conf->getproductTbl().".id,   ".$conf->getproductAttr().".* from ".$conf->getproductTbl()."
                       LEFT JOIN ".$conf->getproductAttr()." ON   ".$conf->getproductTbl().".id=".$conf->getproductAttr().".prodFK
                     Where ".$conf->getproductTbl().".id IN(" . $idList . ");");

echo "<table width = 100% border = '1' cellspacing = '2' cellpadding = '0'>";

while ($result = mysql_fetch_assoc($res))
{
    echo "<th colspan='2'>".$result["name"]."</th>";
}
while ($row = mysql_fetch_assoc($data)) {
    echo "<tr><td>";
    echo $row["Name"];
    echo "</td><td>";
    echo $row["value"];
    echo "</td></tr>";
}   

echo "</table>";

 ?>
    </body>
</html>

【问题讨论】:

    标签: php mysql html-table


    【解决方案1】:

    1 - 您可能希望查看 MYSQL JOIN 以组合您的两个查询。 (http://www.tizag.com/mysqlTutorial/mysqljoins.php)

    2 - 使用 CSS 设置结果集的样式。

    【讨论】:

      【解决方案2】:

      您可以尝试这样的事情。希望对您有所帮助。

      <table>
          <tr>
              <?php
              for($i=0;$i < $totalSelected; $i++){
              //$idList .= $selectedPhones[$i] . ",";
              ?>
              <td><?php
              $data  = mysql_query("SELECT ".$conf->getproductTbl().".id,   ".$conf->getproductAttr().".* from ".$conf->getproductTbl()."
              LEFT JOIN ".$conf->getproductAttr()." ON   ".$conf->getproductTbl().".id=".$conf->getproductAttr().".prodFK
              Where ".$conf->getproductTbl().".id = ".$selectedPhones[$i]);
      
              echo "<table width = 100% border = '1' cellspacing = '2' cellpadding = '0'>";
      
              while ($result = mysql_fetch_assoc($res))
              {
                  echo "<th colspan='2'>".$result["name"]."</th>";
              }
              while ($row = mysql_fetch_assoc($data)) {
                  echo "<tr><td>";
                  echo $row["Name"];
                  echo "</td><td>";
                  echo $row["value"];
                  echo "</td></tr>";
              }   
      
              echo "</table>";
      
              ?>
              </td>
              <?php }?>
          </tr>
      </table>
      

      【讨论】:

      • 这产生了上述结果。没有任何改变。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多