【问题标题】:Show contents of categories, php dynamical tables显示分类内容,php动态表
【发布时间】:2013-06-27 19:08:34
【问题描述】:

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

类别 1:
项目 1 项目 2 项目 3
第 4 项 第 5 项...

类别2
项目 1 项目 2 ...

这是我的 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>

【问题讨论】:

  • 我不确定,但你不能只添加“\r\n”来获取新行吗?

标签: php html-table


【解决方案1】:

首先,您的代码非常“杂乱”。我想您需要更多代码来添加星号之间的部分,但我认为您应该使用更简洁的方法。

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

进行查询:

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>';
}

【讨论】:

    【解决方案2】:

    非常感谢你,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>
    

    【讨论】:

    • 您可以用?&gt; 停止php 块并在html 之后用&lt;?php 重新启动它,而不是用doctype 回显第一部分。
    • 我知道,但这真的重要吗?我认为这是一个习惯问题。我不知道我是否正确,我是 PHP 新手 :)
    • 两者都可以正常工作,但是在编辑文档时,IDE 通常不会为字符串“着色”,而如果您关闭 php-tag,IDE 通常会为您的 html 着色,就好像它是html,而不是字符串。
    猜你喜欢
    • 2012-08-24
    • 2014-09-28
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    相关资源
    最近更新 更多