2016-09-23 60 views
-1

它顯示六個帖子,我的數據庫中有6個項目。問題在於它只顯示第一個,並重復五次。這是我第一次編寫PHP,所以請不要太熟悉術語!爲什麼我的PHP while循環重複相同的數據庫結果?

<?php 

    $pagetitle = "My Portfolio | Projects"; 

    $header = $_SERVER['DOCUMENT_ROOT']; 
    $header .= "/includes/header.php"; 
    include_once($header); 

    $sql="SELECT * FROM projects ORDER BY job DESC"; 
    $sql_query=mysql_query($sql); 
    $post = mysql_fetch_array($sql_query); 
    $job = $post['job']; 
    $category = $post['category']; 
    $title = $post['title']; 
    $inside = $post['imageinside']; 
    $outside = $post['imageoutside']; 
    $body = $post['body']; 
    $description = $post['description']; 

?> 


      <!-- Blog - Start --> 
      <?php do {?> 
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 blog blog_altered blog_left"> 
       <div class="row"> 
        <!-- Blog Image - Start --> 
        <div class=" col-lg-6 col-md-6 col-sm-10 col-xs-12 pic inviewport animated delay1" data-effect="fadeIn"> 
         <img alt="blog-image" class="img-responsive" src="<?php echo "$outside" ?>"> 
        </div> 
        <!-- Blog Image - End --> 
        <!-- Blog Info - Start --> 
        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 inviewport animated delay1" data-effect="fadeIn"> 
         <div class="info"> 
          <span class="date"><?php echo "$category" ?></span> 
          <h4 class="title"><a href="<?php echo "$job" ?>.php"><?php echo "$title" ?></a></h4> 
          <p><?php echo "$description" ?></p> 
          <a class="btn btn-primary text-on-primary" href="<?php echo "$job" ?>.php">Read More</a> 
         </div> 
        </div> 
        <!-- Blog Info - End --> 
       </div> 
      </div> 
      <?php } while ($post = mysql_fetch_array($sql_query)) ?> 
      <!-- Blog - End --> 
+0

使用While循環FOR ALL抵達。 – devpro

+0

使用while循環代替while循環。並且不要使用mysql開始使用mysql。* apis – Bhavin

回答

2

這是因爲您已經將它分配到腳本的頂部並稍後使用這些變量。所以下一次你通過的時候,它不會使用剛提取的數據,而只是從以前設置的代碼中獲取數據。

簡單地擺脫這個部分

$post = mysql_fetch_array($sql_query); 
$job = $post['job']; 
$category = $post['category']; 
$title = $post['title']; 
$inside = $post['imageinside']; 
$outside = $post['imageoutside']; 
$body = $post['body']; 
$description = $post['description']; 
你做,而

更改爲「正常」,而循環,並更改HTML位裏面的變量後的。 因此<?php echo "$category" ?><?php echo "$post['category']" ?>

所以說,你需要這個才能讓這部分工作。但是,最好是使用while循環,而不是do,while。它們之間的區別在於,while循環只有在傳遞給循環的數據實際上持有某些東西時纔會執行。儘管會一直執行,然後在你的時間內檢查有效的東西。所以當數據庫沒有結果時,html塊仍然會打印一次。

你也可以考慮使用mysqli或者pdo進行數據庫連接。 mysql不再是首選的方式(從5.5開始不推薦使用,並在php 7中刪除)

最後,如果您打算只使用關聯密鑰,則可以使用mysql_fetch_assoc

+0

謝謝!這工作! –

1
<?php 

    $pagetitle = "My Portfolio | Projects"; 
    $header = $_SERVER['DOCUMENT_ROOT']; 
    $header .= "/includes/header.php"; 
    include_once($header); 
    $sql="SELECT * FROM projects ORDER BY job DESC"; 
    $sql_query=mysql_query($sql); 

?> 


     <!-- Blog - Start --> 
     <?php while ($post = mysql_fetch_array($sql_query){ 
$job = $post['job']; 
$category = $post['category']; 
$title = $post['title']; 
$inside = $post['imageinside']; 
$outside = $post['imageoutside']; 
$body = $post['body']; 
$description = $post['description']; 

     ?> 
     <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 blog blog_altered blog_left"> 
      <div class="row"> 
       <!-- Blog Image - Start --> 
       <div class=" col-lg-6 col-md-6 col-sm-10 col-xs-12 pic inviewport animated delay1" data-effect="fadeIn"> 
        <img alt="blog-image" class="img-responsive" src="<?php echo "$outside" ?>"> 
       </div> 
       <!-- Blog Image - End --> 
       <!-- Blog Info - Start --> 
       <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 inviewport animated delay1" data-effect="fadeIn"> 
        <div class="info"> 
         <span class="date"><?php echo "$category" ?></span> 
         <h4 class="title"><a href="<?php echo "$job" ?>.php"><?php echo "$title" ?></a></h4> 
         <p><?php echo "$description" ?></p> 
         <a class="btn btn-primary text-on-primary" href="<?php echo "$job" ?>.php">Read More</a> 
        </div> 
       </div> 
       <!-- Blog Info - End --> 
      </div> 
     </div> 
     <?php }) ?> 
     <!-- Blog - End --> 
+0

第二個支架? '})' – devpro

1

您正在設置用於在循環外顯示的變量。他們被設置爲第一個結果,並且在之後不會改變。

你應該做這樣的事情instaed:

<?php 
    $sql_query=mysql_query($sql); 

    while ($post = mysql_fetch_array($sql_query)) 
    { 
     $job = $post['job']; 
     $category = $post['category']; 
     $title = $post['title']; 
     $inside = $post['imageinside']; 
     $outside = $post['imageoutside']; 
     $body = $post['body']; 
     $description = $post['description']; 


     echo '....'; 
     echo $job; 
     // or directly 
     echo $post['job']; 
    } 
?> 
相關問題