我正在尝试为我的网站制作供稿系统,其中我使用mysql_fetch_array
系统从数据库获取数据并为mysql_fetch_array
创建了一个while循环,并且在该循环内我重复了同样的事情又一次while循环,但问题是嵌套的while循环只执行一次而不重复行....这里是代码我已经使用了一些函数和OOP但是你会得到什么意思!
我想到了一个替代方案,但尚未尝试过vaz我想以这种方式出路,即使用for循环而不是while循环可能有效......
public function get_feeds_from_my_friends(){
global $Friends;
global $User;
$friend_list = $Friends->create_friend_list_ids();
$sql = "SELECT feeds.id, feeds.user_id, feeds.post, feeds.date FROM feeds WHERE feeds.user_id IN (". $friend_list. ") ORDER BY feeds.id DESC";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result)) {
$id = $rows['user_id'];
$dp = $User->user_detail_by_id($id, "dp");
$user_name = $User->full_name_by_id($id);
$post_id = $rows['id'];
$final_result = "<div class=\"sharedItem\">";
$final_result .= "<div class=\"item\">";
$final_result .= "<div class=\"imageHolder\"><img src=\"". $dp ."\" class=\"profilePicture\" /></div>";
$final_result .= "<div class=\"txtHolder\">";
$final_result .= "<div class=\"username\"> " . $user_name . "</div>";
$final_result .= "<div class=\"userfeed\"> " . $rows['post'] . "</div>";
$final_result .= "<div class=\"details\">" . $rows['date'] . "</div>";
$final_result .= "</div></div>";
$final_result .= $this->get_comments_for_feed($post_id);
$final_result .= "</div>";
echo $final_result;
}
}
public function get_comments_for_feed($feed_id){
global $User;
$sql = "SELECT feeds.comments FROM feeds WHERE feeds.id = " . $feed_id . " LIMIT 1";
$result = mysql_query($sql);
$result = mysql_fetch_array($result);
$result = $result['comments'];
$comment_list = get_array_string_from_feild($result);
$sql = "SELECT comment_feed.user_id, comment_feed.comment, comment_feed.date FROM comment_feed ";
$sql .= "WHERE comment_feed.id IN(" . $comment_list . ") ORDER BY comment_feed.date DESC";
$result = mysql_query($sql);
if(empty($result)){ return "";}
else {
while ($rows = mysql_fetch_array($result)) {
$id = $rows['user_id'];
$dp = $User->user_detail_by_id($id, "dp");
$user_name = $User->full_name_by_id($id);
return "<div class=\"comments\"><div class=\"imageHolder\"><img src=\"". $dp ."\" class=\"profilePicture\" /></div>
<div class=\"txtHolder\">
<div class=\"username\"> " . $user_name . "</div>
<div class=\"userfeed\"> " . $rows['comment'] . "</div>
<div class=\"details\">" . $rows['date'] . "</div>
</div></div>";
}
}
}
答案 0 :(得分:0)
你的内部循环在循环的第一次迭代中返回。你确定你应该循环吗?如果你确定它总是第一行,为什么不抓住第一行呢?
答案 1 :(得分:0)
这是因为你正在使用return
,这会阻止函数进一步执行。你要做的是
//...
else {
$out = '';
while ($rows = mysql_fetch_array($result)) {
$id = $rows['user_id'];
$dp = $User->user_detail_by_id($id, "dp");
$user_name = $User->full_name_by_id($id);
$out .= "<div class=\"comments\"><div class=\"imageHolder\"><img src=\"". $dp ."\" class=\"profilePicture\" /></div>
<div class=\"txtHolder\">
<div class=\"username\"> " . $user_name . "</div>
<div class=\"userfeed\"> " . $rows['comment'] . "</div>
<div class=\"details\">" . $rows['date'] . "</div>
</div></div>";
}
return $out;
}
答案 2 :(得分:0)
永远不要使用嵌套的while循环,它不会给出您期望的结果。按照以下方式执行操作,就像我在此脚本中所做的那样:
<?php
include('dbconnect.php');
$query = "SELECT * FROM tiles_categories" . " LIMIT 0, 3" ;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) { $category_id = $row["cat_id"] ;
echo "<div class='product'>";
echo "<h2 style='padding-bottom:10px; color:#6F0D00;'>". $row['cat_name'] . "</h2>";
gettiles($category_id);
echo"</div>";
}
function gettiles($category_id) {
$tiles_query = "SELECT * FROM tiles_info where cat_id = '".$category_id."'";
$result = mysql_query($tiles_query) or die(mysql_error());
while($row_tiles = mysql_fetch_array($result)) {
echo "<div align='center' style='float:left; width:100px; margin:0px 35px 0px 0px;'>";
echo "</div>";
}
}
?>