如何计入一个旁边有一个名字的表

时间:2015-01-16 20:03:22

标签: php html mysql pdo

我正在尝试将计数数据显示在表中,所以就像第1行中的第2行第2行中的第3行第3行中的第3行。不确定如何去做我是否必须为每一行创建一个语句?或者我可以在一个声明中完成所有这些吗?

<!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>

From This部分是我想要进入表格,因此该表有两列名称和计数

 <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>

目前它看起来像这样

enter image description here

我希望它看起来像这样。因数据未更新而忽略其中的计数

enter image description here

4 个答案:

答案 0 :(得分:0)

我所做的只是添加一个名为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>

答案 1 :(得分:0)

你可以这样做,$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>";

答案 2 :(得分:0)

考虑到这一点,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 :(得分:0)

我在nav下面的数组上面添加了表格,然后在关闭nav之前放入了关闭表格。 TR和TD高于<a href="#" data-id="<?php echo $i; ?>">

<!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>