【问题标题】:Use one table for two results in diffrent divs在不同的 div 中使用一个表来处理两个结果
【发布时间】:2015-03-15 09:26:27
【问题描述】:

我有两个计数和两个表。第一个计数用于接收状态表。当我单击收到的计数时,它会在 div id 中显示表格。第二个计数用于部门索赔。当我单击索赔计数时,它会在 div 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>
        <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>

【问题讨论】:

    标签: php html sql-server pdo


    【解决方案1】:

    试试下面的。已在循环中完成,因为两个表的代码似乎相同,但查询部分除外。 注意数组$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>

    【讨论】:

    • 我希望它打印 id 而不是值,所以它显示 6 表示收到,31 表示索赔
    • 我怎样才能把计数放在一张桌子上?
    • 我正在尝试让计数数据显示在表格中,所以就像第 1 行中的一个、第 2 行中的一个、第 4 行中的三个一样。不知道如何去做我必须创建一个每一行的声明?还是我可以在一份声明中完成所有工作?
    • @Veronica 更新了代码,因此它将在每行前面显示行号。我想这就是你想要的。我添加的是表格的另一列,带有一个名为 $rowCount 的变量,最初为 1,每行后会自动增加 1。所以它会根据行显示2,3,4。
    猜你喜欢
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多