2017-01-18 68 views
1

我遵循與引導程序有關的數據表的基本教程,但它只適用於硬編碼數據。如何在數據表中顯示來自數據庫的數據

如果我想動態顯示它,我該怎樣才能使它工作?它正在顯示它,但搜索和顯示條目數不起作用。它也表示當它實際顯示來自我的數據庫的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"> 
     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
     <title>Bootstrap 101 Template</title> 

     <!-- Bootstrap --> 
     <link href="css/bootstrap.min.css" rel="stylesheet"> 
     <link href=" //maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
     <link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet"> 



     </head> 
    <body> 
     <br/> 
     <hr/> 
     <?php 
     require_once("/dao/CategoryDAO.php"); 
     require_once("/dao/TopicDAO.php"); 

     $category = new CategoryDAO(); 
     $topic = new TopicDAO(); 
     $allCategories_arr = $category->getAllCategories(); 
     $allTopics_arr = $topic->getAllTopicTitles(); 

     ?> 
     <div class="container"> 
      <div class="row"> 
       <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
        <thead> 
        <tr> 
         <th>Category ID</th> 
         <th>Category Name</th> 
         <th>Action</th> 
        </tr> 
        <?php 
        foreach ($allCategories_arr as $ar) { 
         echo "<tr><th>".$ar['category_id']."</th><td>".$ar['categoryname']."</td><th><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a></th></tr>"; 
        } 
        ?> 
        </thead> 
        <tbody> 


        </tbody> 
       </table> 


      </div> 
     </div> 


     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
     <!-- Include all compiled plugins (below), or include individual files as needed --> 
     <script src="js/bootstrap.min.js"></script> 
     <script src="//code.jquery.com/jquery-1.12.4.js"></script> 
     <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> 
     <script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $('#example').DataTable(); 
      }); 
     </script> 

    </body> 
     </html> 

回答

1

快速修復,應關閉<thead>標記你開始循環之前,並顯示裏面的<tbody>你不使用<th>標籤了,你用你的<tr>'s and <td>'s

這是怎麼<tbody>一旦你內心的結果你的代碼應該看起來;

<!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"> 
     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
     <title>Bootstrap 101 Template</title> 
     <!-- Bootstrap --> 
     <link href="css/bootstrap.min.css" rel="stylesheet"> 
     <link href=" //maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
     <link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet"> 
    </head> 
    <body> 
     <br/> 
     <hr/> 
     <?php 
      require_once("/dao/CategoryDAO.php"); 
      require_once("/dao/TopicDAO.php"); 

      $category = new CategoryDAO(); 
      $topic = new TopicDAO(); 
      $allCategories_arr = $category->getAllCategories(); 
      $allTopics_arr = $topic->getAllTopicTitles(); 

      ?> 
     <div class="container"> 
      <div class="row"> 
       <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
        <thead> 
         <tr> 
          <th>Category ID</th> 
          <th>Category Name</th> 
          <th>Action</th> 
         </tr> 
        </thead> 
        <tbody> 
         <?php 
          foreach ($allCategories_arr as $ar) { 
           echo "<tr>"; 
           echo "<td>".$ar['category_id']."</td>"; 
           echo "<td>".$ar['categoryname']."</td>"; 
           echo "<td><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a>"; 
           echo "</tr>"; 
          } 
          ?> 
        </tbody> 
       </table> 
      </div> 
     </div> 
     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
     <!-- Include all compiled plugins (below), or include individual files as needed --> 
     <script src="js/bootstrap.min.js"></script> 
     <script src="//code.jquery.com/jquery-1.12.4.js"></script> 
     <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> 
     <script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $('#example').DataTable(); 
      }); 
     </script> 
    </body> 
</html> 

還是應該像這樣

<!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"> 
     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> 
     <title>Bootstrap 101 Template</title> 
     <!-- Bootstrap --> 
     <link href="css/bootstrap.min.css" rel="stylesheet"> 
     <link href=" //maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
     <link href="https://cdn.datatables.net/1.10.13/css/dataTables.bootstrap.min.css" rel="stylesheet"> 
    </head> 
    <body> 
     <br/> 
     <hr/> 
     <?php 
      require_once("/dao/CategoryDAO.php"); 
      require_once("/dao/TopicDAO.php"); 

      $category = new CategoryDAO(); 
      $topic = new TopicDAO(); 
      $allCategories_arr = $category->getAllCategories(); 
      $allTopics_arr = $topic->getAllTopicTitles(); 

      ?> 
     <div class="container"> 
      <div class="row"> 
       <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
        <thead> 
         <tr> 
          <th>Category ID</th> 
          <th>Category Name</th> 
          <th>Action</th> 
         </tr> 
        </thead> 
        <tbody> 
         <?php 
          foreach ($allCategories_arr as $ar) :?> 
         <tr> 
          <td><?php echo $ar['category_id'] ;?></td> 
          <td><?php echo $ar['categoryname'];?></td> 
          <td><a href="ManageSubCategory.php?catid="<?php echo $ar['category_id'];?>">Show Sub Categories</a> 
         </tr> 
         <?php 
          endforeach; 


          ?> 
        </tbody> 
       </table> 
      </div> 
     </div> 
     <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
     <!-- Include all compiled plugins (below), or include individual files as needed --> 
     <script src="js/bootstrap.min.js"></script> 
     <script src="//code.jquery.com/jquery-1.12.4.js"></script> 
     <script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> 
     <script src="https://cdn.datatables.net/1.10.13/js/dataTables.bootstrap.min.js"></script> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       $('#example').DataTable(); 
      }); 
     </script> 
    </body> 
</html> 
+1

非常感謝您,先生這個工作對我來說!上帝保佑。 – ranger

+0

快樂的人,很高興我能幫到你 –

0

寫這個代碼在body標籤

<?php 
       foreach ($allCategories_arr as $ar) { 
        echo "<tr><td>".$ar['category_id']."</td><td>".$ar['categoryname']."</td><td><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a></td></tr>"; 
       } 
       ?> 

我希望它的工作

0

組PHP的foreach內<tbody><thead>和的foreach改變<th><td>

<table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
        <thead> 
        <tr> 
         <th>Category ID</th> 
         <th>Category Name</th> 
         <th>Action</th> 
        </tr> 

        </thead> 
        <tbody> 

<?php 
        foreach ($allCategories_arr as $ar) { 
         echo "<tr><td>".$ar['category_id']."</td><td>".$ar['categoryname']."</td><td><a href='ManageSubCategory.php?catid=".$ar['category_id']."'>Show Sub Categories</a></td></tr>"; 
        } 
        ?> 
        </tbody> 
       </table> 
相關問題