【问题标题】:How to get counts into a table with a name next to it如何将计数放入旁边有名称的表中
【发布时间】:2015-01-16 20:03:21
【问题描述】:

我正在尝试让计数数据显示在表格中,所以就像第 1 行中的一个 2 第 2 行中的一个 第 4 行中的三个。不知道如何去做,我必须为每一行创建一个语句吗?还是我可以在一个声明中完成所有操作?

<!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="description" content="">
            <meta name="author" content="">
            <link rel="icon" href="favicon.ico">
            <title>test</title>
            <!-- Bootstrap core CSS -->

            <link href="../css/custom.css" rel="stylesheet">

          </head>
          <body>
            <nav>
        <?php
            /* For the 2 different types of tables */
            $dataArray = array("one"=>"status='Received'", "two"=>"Department='Claims'");
            require_once("../db_connect.php");
            foreach ($dataArray as $i=>$v)
            {
        ?>      
            <a href="#" data-id="<?php echo $i; ?>"> 
        <?php       
                $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
                $stmt->execute();
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
                echo $row['rows_cnt'];
                }
        ?>
            </a> 
        <?php
            }
        ?>
        </nav>

        <?php
            foreach ($dataArray as $i=>$v)
            {
        ?>
        <div id="<?php echo $i; ?>" class="toggle_content">

        <?php
            //prepared statement with PDO to query the database
            $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE ".$v);
            $stmt->execute();
        ?>

            <?php //start of the while loop ?>
            <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
         <table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">

            <tr> 
                <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
                <strong>Request#</strong></th>
                <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
                <strong>Status</strong></th>
                <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Comments</strong></th>
                <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Date Requested</strong></th>
                <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Name</strong></th>
                <th style="width:10%;  background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Department</strong></th>
            <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>VasLblDate</strong></th>
            </tr>
            <tr>
            <?php $id = $row['RequestNumber'];?>
            <?php echo  "<td> <a href='../update.php?id=".$id."'>".$id."</a></td>"; ?>

                <td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>

            </tr>

            </table>
         <?php } //end of the while loop?>

        </div>
        <?php
            }
        ?>

        <!-- Bootstrap core JavaScript
            ================================================== -->
            <!-- Placed at the end of the document so the pages load faster -->


            <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>

        <script>
            $( document ).ready(function() {
                $('a').on('click', function() {
                    var div_id = $(this).data('id');

                    $('.toggle_content').hide();
                    $('#' + div_id).toggle();
                });
            });
        </script>



          </body>
        </html>

从这部分是我想要进入一个表的内容,所以该表有两列名称和计数

 <nav>

    <?php
        /* For the 2 different types of tables */
        $dataArray = array("one"=>"status='Received'","two"=>"Department='Claims'","three"=>"Department='flat 1'","four"=>"Department='flat 2'","Five"=>"Department='Inbound'");
        require_once("../db_connect.php");
         foreach ($dataArray as $i=>$v)

        {
    ?>    

  <a href="#" data-id="<?php echo $i; ?>">  
    <?php       
            $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
            echo $row['rows_cnt'];
            }
    ?>
        </a> 
    <?php
        }
    ?>  

    </nav>

目前是这样的

我希望它看起来像这样。由于数据未更新而忽略那里的计数

【问题讨论】:

  • 只需在查询中添加名称列,然后添加 echo $row['rows_cnt'] . " " . $row['rows_name']; 类型的东西。
  • @Fred-ii- 我不认为rows_cnt 是表格中的一列。
  • @Barmar OP 有 COUNT(*) AS rows_cnt 所以我想她可以在查询中添加一列。然而,不确定她应该使用或想要使用哪个查询。有几个。

标签: php html mysql pdo


【解决方案1】:

你可以这样做,$key 是每个$rows 元素的索引

echo "<table><tr><th>row number</th><th>column</th></tr>";
foreach($row['rows_cnt']; as $key=>$rows)
{
   echo "<tr><th>".$key."</th><th>".$rows["columnName"]."</th></tr>";
}
echo "</table>";

【讨论】:

  • 我会说这是吗?
  • 你可以在你从数据库中获取数组之后放。
  • 我编辑了上面的部分,当我将表格放入表格时,我需要将代码放入表格中,出于某种原因,该部分位于代码下方
  • 我听不懂你想说什么。
  • 我在上面添加了一些示例图片,它现在看起来如何以及我希望它看起来如何。第二张图片是我以前的做法,我会让每个链接都有自己的页面。所以我只想用一页来完成所有这一切,而不是像 36 页的 36 个链接
【解决方案2】:

考虑到这一点,GROUP BY 和 COUNT 是 ANSI/ISO sql,所以:

$stmt = $db->query("SELECT department, COUNT(1) rows_cnt
                    FROM receivingrequests
                    GROUP BY department");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC) {
    echo '<a href="#" data-id="' . $row['department'] . '">' .
          $row['rows_cnt'] . '</a>';
}

不需要准备好的语句,因为不涉及参数。

【讨论】:

  • 如果不是像部门这样的状态怎么办
【解决方案3】:

我所做的只是添加一个名为 count 的变量,它在循环结束时增加 1。

<!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="favicon.ico">
        <title>test</title>
        <!-- Bootstrap core CSS -->

        <link href="../css/custom.css" rel="stylesheet">

      </head>
      <body>
        <nav>
    <?php
        /* For the 2 different types of tables */
        $count = 1;
        $dataArray = array("one"=>"status='Received'", "two"=>"Department='Claims'");
        require_once("../db_connect.php");
        foreach ($dataArray as $i=>$v)
        {
    ?>      
        <a href="#" data-id="<?php echo $i; ?>"> 
    <?php       
            $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
            echo $row['rows_cnt'];
            }
    ?>
        </a> 
    <?php
        }
    ?>
    </nav>

    <?php
        foreach ($dataArray as $i=>$v)
        {
    ?>
    <div id="<?php echo $i; ?>" class="toggle_content">

    <?php
        //prepared statement with PDO to query the database
        $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE ".$v);
        $stmt->execute();
    ?>

        <?php //start of the while loop ?>
        <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
     <table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">

        <tr> 
            <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
            <strong>Request#</strong></th>
            <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
            <strong>Status</strong></th>
            <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Comments</strong></th>
            <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Date Requested</strong></th>
            <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Name</strong></th>
            <th style="width:10%;  background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Department</strong></th>
        <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
        <strong>VasLblDate</strong></th>
        </tr>

        <tr>
        <td><?=$count?></td>
        <?php $id = $row['RequestNumber'];?>
        <?php echo  "<td> <a href='../update.php?id=".$id."'>".$id."</a></td>"; ?>

            <td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
        <td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>

        </tr>

        </table>
     <?php 
      $count+=1;
      } //end of the while loop?>

    </div>
    <?php
        }
    ?>

    <!-- Bootstrap core JavaScript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->


        <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>

    <script>
        $( document ).ready(function() {
            $('a').on('click', function() {
                var div_id = $(this).data('id');

                $('.toggle_content').hide();
                $('#' + div_id).toggle();
            });
        });
    </script>



      </body>
    </html>

另外,这个文件有点乱,你可能想利用endif;和endforeach;可用,使这些东西更清洁。

这是我的清理版本:

<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="favicon.ico">
<title>test</title>
<!-- Bootstrap core CSS -->

<link href="../css/custom.css" rel="stylesheet">

</head>
<body>
<nav>
<?
/* For the 2 different types of tables */
$count = 1;
$dataArray = array("one"=>"status='Received'", "two"=>"Department='Claims'");
require_once("../db_connect.php");
foreach ($dataArray as $i=>$v):
?>
<a href="#" data-id="<?php echo $i; ?>">
  <?php
  $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests         WHERE ".$v);
  $stmt->execute();
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
    echo $row['rows_cnt'];
  }
  ?>
  </a>
  <? endforeach; ?>
  </nav>
<? foreach ($dataArray as $i=>$v): ?>
<div id="<?php echo $i; ?>" class="toggle_content">

 <?php
  //prepared statement with PDO to query the database
 $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE ".$v);
 $stmt->execute();
 ?>

 <!-- start of the while loop -->
 <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ): ?>
 <table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">

  <tr>
    <th style="width:15%; background-color: #000000;color: #FFFFFF;"    class="style3">
      <strong>Request#</strong></th>
      <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
        <strong>Status</strong></th>
        <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
          <strong>Comments</strong></th>
          <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Date Requested</strong></th>
            <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
              <strong>Name</strong></th>
              <th style="width:10%;  background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Department</strong></th>
                <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
                  <strong>VasLblDate</strong></th>
                </tr>

                <tr>
                  <td><?=$count?></td>
                  <?php $id = $row['RequestNumber'];?>
                  <?php echo  "<td> <a href='../update.php?id=".$id."'>".$id."</a></td>"; ?>

                  <td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
                  <td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
                  <td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
                  <td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
                  <td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
                  <td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>

                </tr>

              </table>
              <?php
              $count+=1;
              endwhile; ?>

            </div>
          <? endforeach; ?>

          <!-- Bootstrap core JavaScript
          ================================================== -->
          <!-- Placed at the end of the document so the pages load faster -->


          <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>

          <script>
          $( document ).ready(function() {
            $('a').on('click', function() {
              var div_id = $(this).data('id');

              $('.toggle_content').hide();
              $('#' + div_id).toggle();
            });
          });
          </script>



        </body>
        </html>

【讨论】:

  • 很简单,只要 if(true): //stuff endif;
  • 是否可以编辑代码并添加它,这样我就可以看到它是如何完成的,我是真正的新手,在编程方面我需要学习很多东西
  • 您是否在项目中测试过此代码以确保其有效?
  • 我添加了我的清理版本
【解决方案4】:

我在导航下方的数组上方添加了表格,然后在关闭导航之前放置了关闭表格。还有上面的TR和TD&lt;a href="#" data-id="&lt;?php echo $i; ?&gt;"&gt;

<!DOCTYPE html>
        <html lang="en">
          <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="description" content="">
            <meta name="author" content="">
            <link rel="icon" href="favicon.ico">
            <title>test</title>
            <!-- Bootstrap core CSS -->

            <link href="../css/custom.css" rel="stylesheet">

          </head>
          <body>

            <nav>

       <table border='1'>
        <?php
            /* For the 2 different types of tables */
            $dataArray = array("one"=>"status='Received'","two"=>"Department='Claims'","three"=>"Department='flat 1'","four"=>"Department='flat 2'","Five"=>"Department='Inbound'");
            require_once("../db_connect.php");

             foreach ($dataArray as $i=>$v)

            {
        ?>    

    <tr><td>
      <a href="#" data-id="<?php echo $i; ?>"> 
        <?php       

                $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE ".$v);
                $stmt->execute();
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
                echo $row['rows_cnt'];

      }
        ?>
            </a> 

    </td>
        <td>
       <?php echo$v;?></td>
    </tr>
     <?php
            }
        ?>  
    </table>
        </nav>

        <?php
            foreach ($dataArray as $i=>$v)
            {
        ?>

        <div id="<?php echo $i; ?>" class="toggle_content">

        <?php
            //prepared statement with PDO to query the database
            $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE ".$v);
            $stmt->execute();
        ?>

            <?php //start of the while loop ?>
            <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
         <table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">

            <tr> 
                <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
                <strong>Request#</strong></th>
                <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
                <strong>Status</strong></th>
                <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Comments</strong></th>
                <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Date Requested</strong></th>
                <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Name</strong></th>
                <th style="width:10%;  background-color: #000000; color: #FFFFFF;" class="style3">
                <strong>Department</strong></th>
            <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>VasLblDate</strong></th>
            </tr>
            <tr>
            <?php $id = $row['RequestNumber'];?>
            <?php echo  "<td> <a href='../update.php?id=".$id."'>".$id."</a></td>"; ?>

                <td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
                <td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>

            </tr>

            </table>
         <?php } //end of the while loop?>

        </div>
        <?php
            }
        ?>

        <!-- Bootstrap core JavaScript
            ================================================== -->
            <!-- Placed at the end of the document so the pages load faster -->


            <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>

        <script>
            $( document ).ready(function() {
                $('a').on('click', function() {
                    var div_id = $(this).data('id');

                    $('.toggle_content').hide();
                    $('#' + div_id).toggle();
                });
            });
        </script>



          </body>
        </html>

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 2022-01-03
    • 2014-05-26
    • 2014-04-14
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多