在不同的 div 中使用一个 table 得到两个结果

Use one table for two results in diffrent divs

我有两个计数和两个 table。第一个计数是针对已接收状态 table。当我点击接收计数时,它在 div id 中显示 table。第二个计数是针对部门 claims.when 我点击了声明计数,它在 div id 2 中显示了 table。他们都使用相同的 table 格式。我的问题是如何获得它,以便我可以对两者使用一个 table,而不必继续向我的页面添加 table。

    <!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>
        <a href="#" data-id="one"> <?php 
          require_once("../db_connect.php");
      $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE status='Received'");
      $stmt->execute();
      while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
        echo $row['rows_cnt'];
      }

      ?></a> 


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

      ?></a> 


    </nav>

    <div id="one" class="toggle_content">

     <?php
        //conects to the database
        require_once("../db_connect.php");

        //prepared statement with PDO to query the database
        $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE status='Received'");
        $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>


    <div id="two" class="toggle_content">

     <?php
        //conects to the database
        require_once("../db_connect.php");

        //prepared statement with PDO to query the database
        $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE Department='Claims'");
        $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>


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

尝试以下操作。已经进入循环,因为两个表的代码似乎相同,除了查询部分。 请注意数组 $dataArray,并且您在行

中也有错误
<?php echo  "<td> <a href='../update.php?id=".$id."'>".$id."</a></td>"; ?>

在您的原始代码中,您漏掉了包含 $id 变量的引号,它只会打印“$id”,而不是实际值。

代码:

   <!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 $rowCount = 1; //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:10%; background-color: #000000;color: #FFFFFF;" class="style3">
            <strong>Rows</strong></th>
            <th style="width:10%; background-color: #000000;color: #FFFFFF;" class="style3">
            <strong>Request#</strong></th>
            <th style="width:10%; 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"><strong><?php echo $rowCount++; ?></strong></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>