我在foreach循环中有以下内容(显示我的各种视频),我正在尝试为前三个投票视频显示一些替代文字。我究竟做错了什么(很清楚)......
$sql = "SELECT video_id FROM videos WHERE displayable='y' ORDER BY votes desc LIMIT 0,3";
$result = mysql_query($sql);
$row = @mysql_fetch_array($result);
if(in_array($video->getProperty('video_id')) == $row['video_id']) {
do this...
} else {
do this..
}
答案 0 :(得分:1)
mysql_fetch_array只返回一行,你需要遍历你的结果来构建一个包含前三个ID的数组。
$sql = "SELECT video_id FROM videos WHERE displayable='y' ORDER BY votes desc LIMIT 0,3";
$result = mysql_query($sql);
while($row = @mysql_fetch_array($result)) {
$topthree[] = $row["video_id"];
}
然后你可以使用in_array但语法正确:
if(in_array($video->getProperty('video_id'), $topthree)) {
do this...
} else {
do this..
}
答案 1 :(得分:1)
首先用一些错误替换你的代码,防止这样的技术!
$sql = "SELECT video_id FROM videos WHERE displayable='y' ORDER BY votes desc LIMIT 0,3";
if(false != ($result = mysql_query($sql))
{
$row = mysql_fetch_assoc($result); //Dont need the @ restraint as the result is not false above.
//Also to get associate keys you need to use mysql_fetch_assoc
if($video->getProperty('video_id') == $row['video_id'])) //Remove the in array as your directly comparing the to entities with ==
{
//Match
}else
{
//Video does not match
}
}
你的主要问题是mysql_fetch_array(),请研究与mysql_fetch_array()和mysql_fetch_assoc()的差异;
-
编辑:我要去的方式
//Change the query and the loop way.
$sql = "SELECT video_id FROM videos WHERE displayable='y' AND video_id != '".(int)$video->getProperty('video_id')."' ORDER BY votes desc LIMIT 0,3";
if(false != ($result = mysql_query($sql))
//Use the @ restraint if you have E_NOTICE on within E_Reporting
{
while($row = mysql_fetch_assoc($result))
{
//Print the $row here how you wish for it to be displayed
}
}else
{
//We have an error?
echo '<strong>Unable to list top rated videos, please check back later.</strong>'
}
}