【问题标题】:How to display multiple tables based on value如何根据值显示多个表格
【发布时间】:2013-07-20 22:36:24
【问题描述】:

我有一个显示我的信息的库存页面,我试图有多个基于部门价值的表格,基本上我想在表格中单独显示每个部门而不是单个表格,看起来它会让该页面更易于查看,是的,我是新用户,是的,我正在使用 mysql,我知道它已被弃用,这是一个本地数据库,我是自学的。我必须使用某种循环还是有更简单的方法?

<?php
   $connect = mysql_connect("localhost", "Me", "password") or die ("Check your connection.");
   mysql_select_db("radio");
   $quey1="select * from inventory order by department, user";
   $result=mysql_query($quey1) or die(mysql_error());
?>
<table border=1 cellpadding="10 cellspacing="1 style="background-color:#F0F8FF;" >
 <tr>
  <th>Serial Number</th>
  <th>Model</th>
  <th>Department</th>
  <th>User</th>
  <th>Date</th>
 </tr>
<?php
   while($row=mysql_fetch_array($result)){
      echo "<tr><td>";
      echo $row['serialnumber'];
      echo "</td><td>";
      echo $row['model'];
      echo "</td><td>";
      echo $row['department'];
      echo "</td><td>";
      echo $row['user'];
      echo "</td><td>";
      echo $row['date'];
      echo "</td></tr>";
   }
   echo "</table>";
?>

【问题讨论】:

    标签: php mysql html-table echo


    【解决方案1】:
        <?php
       $connect = mysql_connect("localhost", "Me", "password") or die ("Check your connection.");
       mysql_select_db("radio");
       $quey1="select * from inventory order by department, user";
       $result=mysql_query($quey1) or die(mysql_error());
       while($row=mysql_fetch_array($result)){
          $resultsArr[] = $row;
          $departments[] = $row['department'];
       }
    
    
    foreach($departments as $department) { ?>
      <table border=1 cellpadding="10 cellspacing="1 style="background-color:#F0F8FF;" >
         <tr>
            <th>Serial Number</th>
            <th>Model</th>
            <th>Department</th>
            <th>User</th>
            <th>Date</th>
         </tr>
       <?php foreach($resultArr as $value) 
            if($value['department'] == $department) { ?>
               <tr>
                    <td><?php echo $value['serialnumber']; ?></td>
                    <td><?php echo $row['model']; ?></td>
                    <td><?php echo $row['department']; ?></td>
                    <td><?php echo $row['user']; ?></td>
                    <td><?php echo $row['date']; ?></td>
               </tr>
       <?php } ?>
    <?php } ?>
    

    试试这个,告诉我它是否有效。

    罗密欧奥尼西姆

    【讨论】:

    • 感谢罗密欧的建议,我在第 9 行遇到 Parse error: syntax error, unexpected '}',但我的方向是正确的。大卫我不确定你的意思,这个问题怎么不会发生?我让它在一页上显示每条记录,我只是试图将表格分成多个以便更容易查看。
    • 添加;之后:$departments[] = $row['department']
    【解决方案2】:

    您可以在循环内创建整个表格。然后检查新的部门值何时到来,然后关闭之前的表并打开一个新的。

    您知道,这种混合逻辑会在不需要的情况下产生复杂性。如果您考虑将所有 DB 交互移动到一个部分,在另一个部分处理 DB 查询的结果,在另一个部分输出结果,您会发现这个问题甚至不会发生。这是一个非常基本的 MVC 概念。

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 2020-05-22
      • 1970-01-01
      • 2015-09-10
      • 2014-07-30
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      相关资源
      最近更新 更多