2012-02-28 77 views
1

我不知道如何格式化表格,以便每個屬性部分都有一個標題。當我真的需要看到更多內容時,我只會看到一個標題。使用PHP格式化表格

例如,我可以通過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> 

回答

0

1 - 你可能想看看MYSQL JOIN結合您的兩個查詢。 (http://www.tizag.com/mysqlTutorial/mysqljoins.php)

2 - 使用CSS來設置結果集的樣式。

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

這產生了上述結果。沒有什麼改變。 – 2012-02-28 16:01:04