只返回1项

时间:2013-11-28 08:52:49

标签: php mysql

有人可以帮助我,因为我是php的新手吗?

我正在运行一个使用智能模板引擎的电影网站,而我正在尝试做的是返回并显示所有类别的电影,但是当我使用这个时,它会为所有电影显示相同类别的“喜剧”

功能

public function getByPerma($perma, $lang=null){
    $perma = mysql_real_escape_string($perma);
    $movie = array();
    $e = mysql_query("SELECT * FROM movies WHERE perma='$perma'") or die(mysql_error());
    if (mysql_num_rows($e)>0){
        $movie = $this->formatMovieData(mysql_fetch_assoc($e), $lang);
    }
    return $movie;
}

public function getMovieCategoryDetails($movieid,$lang=null){
    $movieid = mysql_real_escape_string($movieid);
    $e = mysql_query("SELECT * FROM movie_tags WHERE id IN (SELECT tag_id FROM movie_tags_join WHERE movie_id=$movieid)") or die(mysql_error());
    $tags = array();
    if (mysql_num_rows($e)){
        while($s = mysql_fetch_array($e)){
            $s['tag'] = json_decode($s['tag'],true);
            if ($lang){
                $s['tag'] = $s['tag'][$lang];
            }

            $tags[$s['id']] = $s;
        }
    }
    return $tags;
}

调用函数

$movies = $movie->getByPerma($perma,$language);
if (empty($movies)) {
    $movies = '';
} else {

    $tags = $movie->getMovieCategoryDetails($movies['id'],$language);
    if (!count($tags)){
        $smarty->assign("tags","");
    } else {
        $smarty->assign("tags",$tags);
    }
} 

显示类别

{foreach from=$tags key=id item=val name=tags}
    {$val.tag}
{/foreach}

我希望有人可以提供帮助吗?

1 个答案:

答案 0 :(得分:1)

我认为你的getByPerma()只返回1个电影信息,当把项目添加到数组时,[]丢失了:

$movie = $this->formatMovieData(mysql_fetch_assoc($e), $lang);
-->
$movie[] = $this->formatMovieData(mysql_fetch_assoc($e), $lang);