2017-08-09 6 views
0

私は質問と回答の2つのテーブルからデータを取り出し、特定のパネルボックスに表示しようとしています。このコードで問題になっているのは、データが表示されるのは、データベースの1行ごとに減少しています。 私はデータを取得するためにループを使用していますそれは正しいですか?PHPを使用して特定のデザインにデータベースの詳細を表示

<?php 
define('DB_HOST', 'localhost'); 
define('DB_NAME', 'trialdvhub'); 
define('DB_USER','root'); 
define('DB_PASSWORD',''); 
mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error()); 
mysql_select_db(DB_NAME) or die("Failed to connect to MySQL: " . mysql_error()); 
$sql="SELECT * FROM question "; 
$aResult=mysql_query($sql); 
$sqli="SELECT * FROM answer"; 
$anresult=mysql_query($sqli); 

?> 
<?php 
      $question = array(); 

      while($rows = mysql_fetch_assoc($aResult)) 
      { 
       $added  = $rows['added_by']; 
       $date   = $rows['added_on']; 
       $question[]  = $rows['description']; 
      } 
      ?> 

      <?php 
       $answer =array(); 
       while($row = mysql_fetch_assoc($anresult)) 
       { 
        $answer[]= $row['description']; 
      ?> 

     <?php 
      for($i=0;$i<6;$i++) 
      { 
      ?> 
       <div class="row"> 
       <div class="col-sm-8"> 
       <div class="panel-group"> 
       <div class="panel panel-default">    
     <div class="panel-body"><img src="images\15.jpg" class="img-circle" style="width:20px;height:20px;"> 
     <span style="color:#949494;font-size:9;"><?php echo $added?> </span> <span style="color:#949494;font-size:9;"><?php echo $date?> </span> 
     <p><h6><?php echo $question[$i]?></h6></p> 
     <p><h6><span style="color:#949494"><?php echo $answer[$i]?></span></h6></p> 
     <h6><span style="color:#949494">Share 35</span><img src="images\02.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:0.5cm;"><img src="images\03.ico" class="img-circle" 
     style="width:20px;height:20px;margin-left:0.1cm;"><img src="images\04.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:0.1cm;"> <img src="images\b.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:1cm;"><span style="color:#949494;margin-left:0.3cm;"><a data-toggle="modal" data-target="#myModal3">Write your post</a></span><img src="images\07.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:1cm;"><span style="color:#949494;margin-left:0.3cm;">26 Answered</span><img src="images\08.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:1cm;"><span style="color:#949494;margin-left:0.3cm;">52 Views</span></h6>  
    </div> 
    </div> 
    </div> 
      <?php }?>   
      <div class="row"> 
     <div class="col-sm-1"> 
      </div> 
       </div> 
        </div> 
         </div> 
         </div>      
<br>    
<hr> 
</br>  
    <div class="container"> 
     <div class="row"> 
     <div class="col-sm-1"> 
     </div> 
+0

パネルのサイズがどうなっているのですか?問題を理解できるようにスクリーンショットをつけることができますか? –

+0

私はスクリーンショットをここに入れています –

+0

私は残念です写真を埋め込むことは許されていませんbecoz私はサイトには初めてです –

答えて

0

最初に、ビューに接続を含めずにテンプレートレイヤーを含めることです。 PHPでの開始のための正しい方法を見て:http://www.phptherightway.com/#templatingともおそらくlook at a framework

2つ目はalternate syntax for if/for/etcを使用した(pタグ内部のネストH6が正しくないとしても、有効なHTMLを学ぶ)あなたのインデントを整理することです。あなたが正しくインデントしたら、あなたの外側のdivがループ内ですべて閉じているわけではないので、ループ内の行を入れ子にしているので、毎回小さくなるのがわかります。

<?php for($i=0;$i<6;$i++): ?> 
    <div class="row"> 
     <div class="col-sm-8"> 
      <div class="panel-group"> 
       <div class="panel panel-default">    
        <div class="panel-body"> 
         <img src="images\15.jpg" class="img-circle" style="width:20px;height:20px;"> 
         <span style="color:#949494;font-size:9;"><?php echo $added?> </span> 
         <span style="color:#949494;font-size:9;"><?php echo $date?> </span> 
         <p><h6><?php echo $question[$i]?></h6></p> 
         <p><h6><span style="color:#949494"><?php echo $answer[$i]?></span></h6></p> 
         <h6> 
          <span style="color:#949494">Share 35</span> 
          <img src="images\02.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:0.5cm;"> 
          <img src="images\03.ico" class="img-circle" 
     style="width:20px;height:20px;margin-left:0.1cm;"> 
          <img src="images\04.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:0.1cm;"> 
          <img src="images\b.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:1cm;"> 
          <span style="color:#949494;margin-left:0.3cm;"><a data-toggle="modal" data-target="#myModal3">Write your post</a></span> 
          <img src="images\07.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:1cm;"> 
          <span style="color:#949494;margin-left:0.3cm;">26 Answered</span> 
          <img src="images\08.png" class="img-circle" 
     style="width:20px;height:20px;margin-left:1cm;"> 
          <span style="color:#949494;margin-left:0.3cm;">52 Views</span> 
         </h6>  
        </div> 
       </div> 
      </div> 
     <?php endfor; ?>    
     <div class="row"> 
      <div class="col-sm-1"> 
      </div> 
     </div> 
    </div> 
</div> 
</div>      
<br>    
<hr> 
</br>  
<div class="container"> 
    <div class="row"> 
     <div class="col-sm-1"> 
     </div> 

だけが(分であなたが戻ってすべての行を引っ張っだけ史上初の6を表示している)必要なデータを引き戻すために、あなたのSQLでLIMIT文のように、見なければならないいくつかの他のものがあります。

関連する問題