2017-01-18 9 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>タグを使用していないの内側にあなた一度<tbody>内部の結果を表示する必要があり、あなたがあなたの<tr>'s and <td>'s

を使用してこれがどのようですあなたのコードは見えるはずです。

<!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>"; 
       } 
       ?> 

私はそれが<thead><tbody>

0

セットPHPのforeachのがない仕事と<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> 
にforeachの中 <th>を変更願っています
関連する問題