簡體   English   中英

While循環兩次遍歷數據

[英]While loop looping through data twice

因此,我有了這段代碼,而現在已經為它煩惱了幾個小時,而我的朋友和我找不到解決方案。 它根據從數據庫中提取的降序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";
}

?>

您還確定數據庫中沒有損壞嗎? 你可以嘗試

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

您可以將調用/發布到此腳本的代碼發布嗎? 正如其他人所說,此代碼很有可能被調用兩次。

另外,如果直接在數據庫中運行此查詢,將返回什么?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM