首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >显示类别的内容,php动态表

显示类别的内容,php动态表
EN

Stack Overflow用户
提问于 2013-06-30 22:23:32
回答 2查看 204关注 0票数 0

我尝试以这种方式在网页中按类别打印项目的内容:

Category1:

Item1 item2 item3

item4 item5...

Category2

item1 item2 ...

下面是我的PHP代码:

代码语言:javascript
复制
$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>";
}

我得到的是:

代码语言:javascript
复制
<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>

我想要得到的是:

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-01 00:21:44

首先,你有非常“混乱”的代码。我想你需要更多的代码来添加星号之间的部分,但我认为你应该使用一种更干净的方法。

我想您使用的是mysql,它支持GROUP_CONCAT()-function。在这种情况下使用它将是明智的。

进行查询:

代码语言:javascript
复制
SELECT name, GROUP_CONCAT( title SEPERATOR '|') as titles FROM your_database GROUP BY name

现在,每个类别的结果都在一行中。

代码语言:javascript
复制
//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
EN

Stack Overflow用户

发布于 2013-07-01 21:24:43

非常感谢你,Sumurai8!你的想法很管用。这是我的php代码:

代码语言:javascript
复制
<?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";
}

这就是我想要的输出:

代码语言:javascript
复制
<!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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17391315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档