2016-09-12 3 views
-3

부트 스트랩 프레임 워크로 웹 사이트를 만들었습니다. 이 웹 사이트에서는 데이터베이스 레코드가있는 테이블을로드합니다.이 데이터베이스를 위해 PHP를 사용합니다.부트 스트랩 테이블에 데이터로드

내 질문은 : php는 테이블에 데이터를로드하는 가장 좋은 방법인가요? 더 좋은 옵션이 있습니까? 수

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

<title>AddCloud - Edicions</title> 

     <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"> 
     <link rel="stylesheet" href="assets/bootstrap-table/dist/bootstrap-table.css"> 

</head> 
<body> 
<?php 
    session_start(); 

    if(isset($_SESSION['username']) and $_SESSION['username'] <> ''){ 
?> 
     <nav class="navbar navbar-default"> 
      <div class="container-fluid"> 
      <!-- Brand and toggle get grouped for better mobile display --> 
      <div class="navbar-header"> 
       <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#defaultNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> 
       <a class="navbar-brand" href="http://www.addvantage.es"><font color=#8abe44>AddCloud</font></a></div> 
      <!-- Collect the nav links, forms, and other content for toggling --> 
      <div class="collapse navbar-collapse" id="defaultNavbar1"> 
       <ul class="nav navbar-nav"> 
       <li class="active"><a href="edicions.php">Edicions<span class="sr-only">(current)</span></a></li> 
       <li><a href="productes.php">Productes</a></li> 
       </ul> 
       <ul class="nav navbar-nav navbar-right"> 
       <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-user"></span> <?php echo $_SESSION['username'] ?></a> 
        <ul class="dropdown-menu" role="menu"> 
        <li><a href="preferences.html"><span class="glyphicon glyphicon-wrench"></span> preferències</a></li> 
        <li><a href="changepassword.html"><span class="glyphicon glyphicon-lock"></span> canviar password</a></li> 
        <li class="divider"></li> 
        <li><a href="logout.php"><span class="glyphicon glyphicon-log-out"></span> log out</a></li> 
        </ul> 
       </li> 
       </ul> 
      </div> 
      <!-- /.navbar-collapse --> 
      </div> 
      <!-- /.container-fluid --> 
     </nav> 
     <div class="container-fluid"> 
      <div class="row"> 
      <div class="col-md-6 col-md-offset-3"> 
       <h1 class="text-center">Miquel Alimentació</h1> 
      </div> 
      </div> 
      <hr> 
     </div> 
     <div class="container"> 
      <div class="row text-center"> 
      <div class="col-md-6 col-md-offset-3"><h3>Edició 12</h3></div> 
      </div> 
      <hr> 
      <div class="row"> 
      <?php 
        // Conexió a la base de dades 
        include("connect.php"); 
        $conn = Conectarse("localhost", "5432", "dbname", "dbuser", "dbpass"); 
        //query 
        $query = "SELECT * FROM produccion.ma_origen ORDER BY id_articulo ASC"; 
        $result = pg_query($conn, $query); 
        //se despliega el resultado 
        echo "<table id='table' 
            data-toggle='table' 
            data-toolbar='#toolbar' 
            data-show-refresh='true' 
            data-show-toggle='true' 
            data-sort-name='name' 
            data-sort-order='desc' 
            data-show-columns='true' 
            data-pagination='true' 
            data-search='true' 
            data-click-to-select='true'>"; 
         echo "<thead class='thead-inverse'>"; 
          echo "<tr>"; 
           echo "<th data id='seleccion' data-switchable='false' data-checkbox='true'></th>"; 
           echo "<th data id='pagina' data-sortable='true'>pagina</th>"; 
           echo "<th data id='codigo' data-sortable='true' data-switchable='false'>codigo</th>"; 
           echo "<th data id='descripcion' data-sortable='true' data-switchable='false'>descripcion</th>"; 
           echo "<th data id='pvp-cat' data-sortable='true'>pvp-cat</th>"; 
           echo "<th data id='pvp-lev' data-sortable='true'>pvp-lev</th>"; 
           echo "<th data id='pvp-and' data-sortable='true'>pvp-and</th>"; 
           echo "<th data id='pvp-cen' data-sortable='true'>pvp-cen</th>"; 
           echo "<th data id='pvp-nor' data-sortable='true'>pvp-nor</th>"; 
           echo "<th data id='pvp-vas' data-sortable='true'>pvp-vas</th>"; 
           echo "<th data id='fecha-mod' data-sortable='true'>fecha-mod</th>"; 
           echo "<th data id='user' data-sortable='true' data-visible='false'>user</th>"; 
          echo "</tr>"; 
         echo "</thead>"; 
         echo "<tbody>"; 
        while ($row = pg_fetch_row($result)){ 
          echo "<tr>"; 
           echo "<td></td>"; 
           echo "<td>$row[2]></td>"; 
           echo "<td>$row[3]</td>"; 
           echo "<td>$row[4]</td>"; 
           echo "<td>$row[5]</td>"; 
           echo "<td>$row[6]</td>"; 
           echo "<td>$row[7]></td>"; 
           echo "<td>$row[8]</td>"; 
           echo "<td>$row[9]</td>"; 
           echo "<td>$row[10]</td>"; 
           echo "<td>$row[11]</td>"; 
           echo "<td>$row[12]</td>"; 
          echo "</tr>"; 
        } 
         echo "</tbody>"; 
        echo "</table>"; 
       ?> 
      </div> 
      </div> 
      <hr> 
      <div class="row"> 
      <div class="text-center col-md-6 col-md-offset-3"> 
       <p>Copyright &copy; 2016 &middot; All Rights Reserved &middot; <a href="http://www.addvantage.es/" >http://www.addvantage.es</a></p> 
      </div> 
      </div> 
      <hr> 
     </div> 
    <script>  
     $("td").dblclick(function(){ 
      alert("The paragraph was double-clicked"); 
     }); 
    </script> 
     <script src="assets/jquery/jquery.min.js"></script> 
     <script src="assets/bootstrap/js/bootstrap.min.js"></script> 
     <script src="assets/bootstrap-table/dist/bootstrap-table.js"></script> 
<?php 
    } else{ 
     ?><p>La sesión no está activa, por favor ingrese <a href="login_ok.html">aquí</a></p> 
<?php 
    }?> 


</body> 
</html> 

하십시오

또한 때 먼저 포맷없이 텍스트를 표시하고 CSS 스타일 :(

와 일초 쇼가 끝나고 이것은 코드, 웹 사이트에서 데이터를로드입니다 당신은) (Ajax를 사용하여 데이터를 가져 주셔서 감사합니다!

+1

코드를 표시하십시오. –

+0

코드에서 Ajax를 사용해보십시오 – Melchizedek

답변

0

?이 문제를 도와, 그리고 * 기능 (jQuery를) $의 표를 기입) ((문서) .ready을으로 .Append 사용 또는 다른 트리거 이벤트.

* jQuery.each(data, function(index, value) { $("#table_div").append("<tr><td>" + value + "</td></tr>"); });

당신은 아래의 예를 확인할 수 있습니다.

var mydata = [{"Nome":"", 
 
       "Cognome":"", 
 
       "DataN":"0000-00-00", 
 
       "Provincia":"", 
 
       "Comune":"", 
 
       "CAP":"", 
 
       "Indirizzo":"", 
 
       "Fisso":"", 
 
       "Mobile":"", 
 
       "Note":""}, 
 
       {"Nome":"Federico", 
 
       "Cognome":"Lupieri", 
 
       "DataN":"2015-09-16", 
 
       "Provincia":"", 
 
       "Comune":"", 
 
       "CAP":"34170", 
 
       "Indirizzo":"Via Ascoli 1", 
 
       "Fisso":"00112233445566", 
 
       "Mobile":"00112233445566", 
 
       "Note":"Vediamo se funziona questo"}]; 
 

 
$(document).ready(function(){ 
 
    $.ajax({ 
 
     url: '/echo/json/', 
 
     dataType: 'json', 
 
     success: function(data) { 
 
      //alert('done'); 
 
      $('#clienti').bootstrapTable({ 
 
       data: mydata 
 
      }); 
 
     }, 
 
     error: function(e) { 
 
      console.log(e.responseText); 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table class="display table table-bordered" id="clienti"> 
 
    <thead> 
 
    <tr> 
 
     <th data-field="Nome">Nome</th> 
 
     <th data-field="Cognome">Cognome</th> 
 
     <th data-field="DataN">Data Nascita</th> 
 
     <th data-field="Provincia">Provincia</th> 
 
     <th data-field="Comune">Comune</th> 
 
     <th data-field="CAP">CAP</th> 
 
     <th data-field="Indirizzo">Indirizzo</th> 
 
     <th data-field="Fisso">Fisso</th> 
 
     <th data-field="Mobile">Cellulare</th> 
 
     <th data-field="Note">Note</th> 
 
    </tr> 
 
    </thead> 
 
</table>

때문에 웹 사이트에서 볼 수됩니다 모든 데이터는 테이블에 정렬됩니다, 직접 테이블을 채우기 다음 Ajax를 사용하여 데이터 가져 오기를.

0

질문 : "내 질문은 : php가 테이블에 데이터를로드하는 가장 좋은 방법인가요? 더 좋은 옵션이 있습니까?"

A : 소스 (예 : 데이터베이스 또는 파일)에서 가져온 동적 데이터를로드하는 경우, PHP를 사용하는 스크립트 언어를 사용하는 것이 가장 좋은 방법입니다 (또는 유일한 방법). 말하는.

+0

예, 저는 postgresql 데이터베이스에서 데이터를 검색했습니다. 나는 웹 환경에 대한 expirience를 가지고 있지 않다. 보통 Java 또는 bash 스크립팅을 프로그래밍한다. – ruzD

관련 문제