使用PHP格式化表

时间:2012-02-28 05:14:26

标签: php mysql html-table

我无法弄清楚如何格式化表格,以便每个属性部分都有一个标题。当我真的需要看到更多时,我只看到一个标题。

例如,我可以通过MOTOROLA获得DROID 4,并且所有属性都从完全零售价格转为语音拨号。语音拨号后,应该有另一部电话的第二个标题。很明显,我的逻辑搞砸了,我已经挣扎了大约4个小时。我还需要将两个表并排放在一起,而不是像这样的长列表。

enter image description here

这就是我想要实现的目标:enter image description here

我的代码如下所示:

<?
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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

你可以尝试这样的事情......我希望这会对你有帮助。

<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>