2013-06-30 53 views
0

我想通過自己的領域中的網頁打印項目的內容以這種方式:類別的顯示內容,PHP動態表

組別:
項目1 ITEM2項目3
ITEM4 ITEM5 ...

產品組別
物品1 ITEM2 ...

這是我的PHP代碼:

$cat=""; 
$maxcols = 3; 
$i = 0; 

while ($row = $result->fetch_assoc()) { 

    if ($i == $maxcols) { 
     $i = 0; 
     echo "</tr><tr>"; 
    } 

    if($row['name']!=$cat) 
    { 
     echo "<table> 
     <tr><td collspan='3'>".$row['name']."</td></tr> 
     <tr>"; 
    } 

    echo "<td>".$row['title']."</td>"; 
    $cat=$row['name']; 
    $i++; 

} 

while ($i <= $maxcols) { 
    echo "<td>&nbsp;</td>"; 
    $i++; 
    echo "</table>"; 
} 

我所得到的是:

<table> 
<tr><td collspan='3'>Archivers</td></tr> 
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr><tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td><table> 
<tr><td collspan='3'>Benchmark</td></tr> 
<tr><td>Fresh Diagnose</td><td>&nbsp;</td></table> 

我想要得到的是:

所有的
<table> 
<tr><td collspan='3'>Archivers</td></tr> 
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr> 
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdfsd</td>**<td>&nbsp;</td></tr> 
</table>** 
<table> 
<tr><td collspan='3'>Benchmark</td></tr> 
<tr><td>Fresh Diagnose</td><td>&nbsp;</td><td>&nbsp;</td> 
</table> 
+0

我不知道,但你就不能加「\ r \ n」來得到新的線? – dayuloli

回答

0

首先,你必須很 '凌亂' 的代碼。我想你需要更多的代碼來在星星之間添加部分,但我認爲你應該使用更清晰的方法。

我想你使用的是mysql,它支持GROUP_CONCAT()的功能。在這種情況下使用它是明智的。

進行查詢:

SELECT name, GROUP_CONCAT(title SEPERATOR '|') as titles FROM your_database GROUP BY name 

每個類別的結果現在是在一排。

//Amount of rows 
$maxcols = 3; 

while($row = $result->fetch_assoc()) { 
    //The seperator needs to be something that isn't in the values it seperates 
    //This creates an array with the titles for this category 
    $titles = explode('|', $row['titles']); 

    echo ' 
    <table> 
    <tr> 
     <td colspan="' .$maxcols. '">' .$row['name']. '</td> 
    </tr>'; 

    //$offset + $i is the position in the array of titles 
    $offset = 0; 

    //This loop ensures each row is properly opened and closed 
    while($offset < count($titles)) { 

    echo '<tr>'; 
    //This will ensure each row has $maxcols td's in it 
    for($i = 0; $i < $maxcols; $i++) { 
     if(isset($titles[ $offset + $i ])) { 
     echo '<td>' .$titles[ $offset + $i ]. '</td>'; 
     } else { 
     echo '<td>&nbsp;</td>'; 
     } 
    } 
    $offset += $maxcols; 
    echo '</tr>'; 
    } 

    echo '</table>'; 
} 
0

非常感謝你,Sumurai8!你的想法很好。這是我的PHP代碼:

<?php 
include_once('include/db.inc.php'); 

$sql="SELECT m.name, GROUP_CONCAT(s.title order by s.title SEPARATOR '|') as titles FROM menu m, software s where m.id=s.category 
GROUP BY m.name 
order by m.name"; 
$result = $mysqli->query($sql); 
$cat=""; 

$maxcols = 3; 
//$i = 0; 

echo " 
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'> 
<html> 
    <head> 
    <title>content</title> 
     <meta content='text/html; charset=utf-8' http-equiv='content-type'> 
     <meta content='Software Details' name='description'> 
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'> 
<link href='styles/style.css' rel='stylesheet' type='text/css'> 
</head> 

<body class='body'>"; 


while($row = $result->fetch_assoc()) { 
    //The seperator needs to be something that isn't in the values it seperates 
    //This creates an array with the titles for this category 
    $titles = explode('|', $row['titles']); 

    echo " 
<table> 
<tr><td colspan='" .$maxcols. "'>" .$row['name']. "</td></tr> \n"; 

    //$offset + $i is the position in the array of titles 
    $offset = 0; 

    //This loop ensures each row is properly opened and closed 
    while($offset < count($titles)) { 

    echo "<tr>"; 
    //This will ensure each row has $maxcols td's in it 
    for($i = 0; $i < $maxcols; $i++) { 
     if(isset($titles[ $offset + $i ])) { 
     echo "<td>" .$titles[ $offset + $i ]. "</td>"; 
     } else { 
     echo "<td>&nbsp;</td>"; 
     } 
    } 
    $offset += $maxcols; 
    echo "</tr> \n"; 
    } 

    echo "</table><br> \n"; 
} 

這是輸出,就是這個樣子,我希望它:

<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'> 
<html> 
    <head> 
    <title>content</title> 
     <meta content='text/html; charset=utf-8' http-equiv='content-type'> 
     <meta content='Software Details' name='description'> 
<link href='styles/fontstyle.css' rel='stylesheet' type='text/css'> 
<link href='styles/style.css' rel='stylesheet' type='text/css'> 
</head> 

<body class='body'> 
<table> 
<tr><td colspan='3'>Archivers</td></tr> 
<tr><td>7-Zip</td><td>IZArc</td><td>dfssdfsdf sdfsdf</td></tr> 
<tr><td>fgdgdfgd</td><td>sdfsdfsdfsdf dsfsdf</td><td>&nbsp;</td></tr> 
</table><br> 

<table> 
<tr><td colspan='3'>Benchmark</td></tr> 
<tr><td>Fresh Diagnose</td><td>&nbsp;</td><td>&nbsp;</td></tr> 
</table><br> 



</body> 
</html> 
+0

而不是使用doctype回顯第一部分,您可以用'?>'來停止php塊,然後在帶有<?php'的html之後再次啓動它。 – Sumurai8

+0

我知道,但真的很重要嗎?我認爲這是一個習慣問題。我不知道我是否正確,我是PHP中的新成員:) – phantomlord

+0

這兩個工作都很好,但編輯文檔時,字符串通常不是由IDE「着色」,而如果關閉了php-tag,則IDE通常會將您的html着色爲html,而不是字符串。 – Sumurai8