Show contents of categories, php dynamical tables
我试图以这种方式在网页中按类别打印项目的内容:
组别:
第1项第2项第3项
item4 item5 ...
产品组别
item1 item2 ...
这是我的PHP代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | $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></td>"; $i++; echo"</table>"; } |
我得到的是:
1 2 3 4 5 | <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></td></table> |
我想得到的是:
1 2 3 4 5 6 7 8 9 | <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></td></tr> </table>** <table> <tr><td collspan='3'>Benchmark</td></tr> <tr><td>Fresh Diagnose</td><td></td><td></td> </table> |
非常感谢Sumurai8! 你的想法很棒。 这是我的PHP代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <?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> content <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> "; //$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></td>"; } } $offset += $maxcols; echo"</tr> "; } echo"</table> "; } |
这是输出,就像我想要的那样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'> <html> <head> content <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></td></tr> </table> <table> <tr><td colspan='3'>Benchmark</td></tr> <tr><td>Fresh Diagnose</td><td></td><td></td></tr> </table> </body> </html> |
首先,你的代码非常"混乱"。 我想你需要更多的代码来添加星星之间的部分,但我认为你应该使用更清洁的方法。
我想你正在使用mysql,它支持GROUP_CONCAT() - 函数。 在这种情况下使用它是明智的。
进行查询:
1 | SELECT name, GROUP_CONCAT( title SEPERATOR '|') as titles FROM your_database GROUP BY name |
每个类别的结果现在都在一行中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | //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></td>'; } } $offset += $maxcols; echo '</tr>'; } echo '</table>'; } |