Php 而在数据中循环两次
所以我有这个代码,我已经为此烦恼了好几个小时了,我和我的朋友都找不到解决方案。它根据从数据库中提取的递减ID显示评论。这是一种无休止滚动的脚本类型,用于消除分页。出于某种原因,当我点击页面底部并运行脚本时,while循环在数据中迭代了两次。所以如果它应该显示评论ID 5-10,那么它会显示10,9,8,7,6,5,10,9,8,7,6,5;而不是10,9,8,7,6,5。这将是一个非常大的代码位,但我希望你能帮助我解决我的问题。 提前谢谢Php 而在数据中循环两次,php,html,ajax,while-loop,Php,Html,Ajax,While Loop,所以我有这个代码,我已经为此烦恼了好几个小时了,我和我的朋友都找不到解决方案。它根据从数据库中提取的递减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_
<?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 />";
}
while($review\u row=mysql\u fetch\u assoc($mysqlQuery)){
echo$review_row['review_title']。“
”;
}
您可以发布调用/发布到此脚本的代码吗?正如其他人所说,这段代码很可能被调用了两次
另外,如果直接在数据库中运行此查询,将返回什么
SELECT *
FROM `reviews`
WHERE `review_id` < (insert the id here)
ORDER BY `review_id` DESC
LIMIT 0, 10
选择*
来自“评论”
其中“review\u id”<(在此处插入id)
订单由'review_id'描述
限制0,10
如果循环两次,则脚本可能实际运行了两次,这可能是因为重写规则。我不确定是否理解。你能给我一个重写规则的例子吗?如果你不知道它是什么,那肯定不是问题。如果你的web服务器被设置为重定向请求,并且这样做不正确,可能会导致对脚本的两次调用。阅读您的web服务器日志。您是否看到此脚本的每次调用都有两行?@user1371489重写规则通常在.htaccess
中找到,并指示浏览器转到与其最初发送的URL不同的URL。这只会返回审阅标题,这完全正常,问题是,它会返回两次。一切正常,显示完美,但运行/显示两次。因此,如果查看_title=“apple Iphone”,那么对于上述代码,它将返回:appleIphone
appleIphone
。当然,在包含的脚本中没有其他“echo”?