duanquan1243 2015-01-16 17:38
浏览 33
已采纳

在一个表中使用不同div的两个结果

I have two counts and two tables. The first count is for Received status table. when I click count for received it shows the table in the div id one. the second count is for department claims.when I click count for claims it shows the table in the div id two. They both use the same table format. The question I have is how do I get it so that I can use one table for both and not have to keep adding tables to my page.

    <!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>
  • 写回答

1条回答 默认 最新

  • doulang2311 2015-01-16 17:56
    关注

    Try the following. Have made it in a loop, as the code seems to be the same for both the tables, except for the query part. Note the array $dataArray, and also you had a mistake in the line

    <?php echo  "<td> <a href='../update.php?id=".$id."'>".$id."</a></td>"; ?>
    

    In your original code, you missed out the quotes enclosing the $id variables, which would have just printed "$id", instead of the actual value.

    The code:

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

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站