2012-06-18 276 views
0

所以我有这个代码,我一直在烦恼了几个小时,现在我的朋友和我找不到解决方案。它根据从数据库中提取的降序ID显示评论。这是一个无尽的滚动式脚本来消除分页。出于某种原因,当我点击页面底部并运行脚本时,while循环遍历数据两次。因此,如果它应显示评论ID 5 - 10,它将显示10,9,8,7,6,5,10,9,8,7,6,5;而不是10,9,8,7,6,5。这将是一个非常大的代码,但我希望你能帮我解决我的问题。 在此先感谢!虽然循环遍历数据两次

<?php 
include("../mysql_server/connect_to_mysql.php"); 


if($_GET['lastPost']) { 
$mysqlQuery = mysql_query('SELECT * FROM `reviews` WHERE `review_id` < "' . $_GET['lastPost'] . '" ORDER BY `review_id` DESC LIMIT 0, 10'); 

while ($review_row = mysql_fetch_assoc($mysqlQuery)) { 

    $review_title = $review_row['review_title']; 
    $user_id = $review_row['user_id']; 
    $user_firstname = $review_row['user_firstname']; 
    $user_lastname = $review_row['user_lastname']; 
    $review_id = $review_row['review_id']; 
    $review_body = $review_row['review_body']; 
    $review_referral = $review_row['review_referral']; 

     // Code to append text for title 
     // strip tags to avoid breaking any html 
     $review_title = strip_tags($review_title); 

     if (strlen($review_title) > 30) { 

      // truncate string 
      $stringCut = substr($review_title, 0, 30); 

      // make sure it ends in a word so assassinate doesn't become ass... 
      $review_title = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
     } 
     // Code to append text and add Read More 
     // strip tags to avoid breaking any html 
     $review_body = strip_tags($review_body); 

     if (strlen($review_body) > 230) { 

      // truncate string 
      $stringCut = substr($review_body, 0, 230); 

      // make sure it ends in a word so assassinate doesn't become ass... 
      $review_body = substr($stringCut, 0, strrpos($stringCut, ' ')).'... <a class="reviewContentLink" href="../../pages/home_page/post_content.php?id='. $review_id .'">See Full Post</a>'; 
     } else { 
      $review_body .= '<a class="reviewContentLink" href="../../pages/home_page/post_content.php?id='. $review_id .'">See Full Post</a>'; 
     } 

    $review_date = $review_row['review_date']; 
     $review_date = date_create($review_date); 
     $review_date = date_format($review_date, 'g:ia \o\n F jS\, Y'); 
    $user_firstname = $review_row['user_firstname']; 
    $review_rating = $review_row['rating']; 



    /////// Mechanism to Display Pic. See if they have uploaded a pic or not ////////////////////////// 
    $check_pic = "../members/$user_id/thumb_image01.jpg"; 
    $default_pic = "../members/0/image01.jpg"; 
     if (file_exists($check_pic)) { 
      $review_pic = "<img src=\"../$check_pic\" width=\"80px\" />"; 
     } else { 
      $review_pic = "<img src=\"../$default_pic\" width=\"80px\" />"; 
     } 





    include_once('../include/star_display.php');    




    //Pull the Review Category from the row 
    $review_category = ""; 
    $review_category = $review_row['review_category']; 
    include_once('../include/review_category.php'); 


    //Pull the Referral Category from the row 
    $referral_category = $review_row['referral_category']; 
    include_once('../include/referral_category.php'); 


    //Code for URL for Each Review 
    $review_url = $review_row['review_url']; 
    if (!function_exists('remove_http')) { 
     function remove_http($url = '') { 
      return(str_replace(array('http://','https://'), '', $url)); 
     } 
    } 
    //CODE TO DISTINGUISH REFERRALS FROM REVIEWS   
     //Final Output List 
     if($review_referral == 0) { 
       //Code for Displaying URL Link 
       $review_url = remove_http($review_url); 
       $review_url = "<a target='_BLANK' href='http://". $review_url ."'>Buy It Here!</a> "; 

        echo ''. $review_id .' '. $review_referral .' 
        <div class="display_newsfeed" id="'.$review_id.'"> 
         <table id="'.$review_id.'" style="width:98.5%; border:#00B347 1px solid; margin:10px 0px 20px 10px;"> 
         <td style="float:left; width:15%; border-right:1px solid #DDDDDD; margin:5px 0px 5px 0px;"> 
              <div class="review_user_name"><a href="../../pages/profile_page/profile_page.php?id='. $user_id .'">'. $user_firstname.' '. $user_lastname .'</a></div> 
              <div class="review_prof_pic"><a href="../../pages/profile_page/profile_page.php?id='. $user_id .'">'. $review_pic .'</a></div> 
              <div class="bought_it_newsfeed">Bought It!</div> 
           </td> 
           <td style="float:right; width:82%;"> 
             <div class="review_title_p"> 
              <p><span class="review_title">'. $review_title .'</span><span class="review_date_p">'. $review_date .'</span><span class="review_stars">'. $review_stars .'</span></p> 
             </div> 
             <div class="review_read_more"> 
              <p class="review_body_p">'. $review_body .'</p><br /> 
              <div> 
               <div style="float:left;" class="review_black_font_link">Website:'.$review_url.'</div> 
               <div style="float:right; margin-right:20px;" class="review_black_font_link">Category: '.$review_category_post .'</div> 
              </div> 
             </div> 
           </td> 

         </table> 
         <hr style="margin:0px 20px 0px 20px;" />  
         </div> 
            '; 

       } else if($review_referral == 1) {  
       //Code for Displaying URL Link 
       $review_url = remove_http($review_url); 
       $review_url = "<a target='_BLANK' href='http://". $review_url ."'>Click Here</a> "; 

       echo ' 
         <div class="display_newsfeed" id="'.$review_id.'"> 
         <table id="'.$review_id.'" style="width:98.5%; border:#0099FF 1px solid; margin:10px 0px 20px 10px;"> 
         <td style="float:left; width:15%; border-right:1px solid #DDDDDD; margin:5px 0px 5px 0px;"> 
              <div class="review_user_name"><a href="../../pages/profile_page/profile_page.php?id='. $user_id .'">'. $user_firstname.' '. $user_lastname .'</a></div> 
              <div class="review_prof_pic"><a href="../../pages/profile_page/profile_page.php?id='. $user_id .'">'. $review_pic .'</a></div> 
              <div class="referral_newsfeed">Referral</div> 
           </td> 
           <td style="float:right; width:82%;"> 
             <div class="review_title_p"> 
              <p><a href="../../pages/home_page/post_content.php?id='. $review_id .'">'. $review_title .'</a><span class="review_date_p">'. $review_date .'</span><span class="review_stars">'. $review_stars .'</span></p> 
             </div> 
             <div class="review_read_more"> 
              <p class="review_body_p">'. $review_body .'</p><br /> 
              <div> 
               <div style="float:left;" class="review_black_font_link">Business Website:'. $review_url .'</div> 
               <div style="float:right; margin-right:20px;" class="review_black_font_link">Category: '. $referral_category_post .'</div> 
              </div> 
             </div> 
           </td> 

         </table> 
         <hr style="margin:0px 20px 0px 20px;" />  
         </div> 
            '; 
       } 
}    

} else { 
echo "didn't work"; 
} 

?> 
+0

如果它循环两次,很可能脚本实际上运行了两次,这可能是因为重写规则。 –

+0

我不知道我明白。你能给我一个重写规则的例子吗? – lhasson

+0

如果你不知道它是什么,那肯定不是问题。 –

回答

0

您是否确定数据库中没有损坏?你可以试试

while ($review_row = mysql_fetch_assoc($mysqlQuery)) { 
echo $review_row['review_title'] . "<br />"; 
} 
+0

这只会返回审查标题,完全正常,问题是,它会返回两次。一切运行正常并完美显示,但它运行/显示两次。因此,如果review_title =“apple iphone”,那么它将返回:appleIphone
appleIphone
上述代码.. – lhasson

+0

当然,在包含的脚本中没有其他“回声”? – Arcadien

0

你可以发布调用/发布到这个脚本的代码吗?正如其他人所说,这个代码很可能被调用两次。

此外,如果您直接在数据库中运行此查询,返回的是什么?

SELECT * 
FROM `reviews` 
WHERE `review_id` < (insert the id here) 
ORDER BY `review_id` DESC 
LIMIT 0, 10