为每个事物运行两个查询

时间:2011-09-24 13:40:44

标签: php

对于蹩脚的头衔感到抱歉,但我真的不知道如何解释它!

基本上,我想从表中查询游戏类别,然后从另一个表中查询游戏,这些游戏等于从类别表中查询的类别的类别。

到目前为止得到了我?

这是我到目前为止的代码......

$get_categories_query = mysql_query("
  SELECT * 
    FROM game_categories 
ORDER BY category_name ASC");
while ($row = mysql_fetch_array($get_categories_query)) {
  $category_name      = $row['category_name'];
  $get_category_games = mysql_query("
    SELECT * 
      FROM games 
     WHERE category = '$category_name' 
  ORDER BY RAND() LIMIT 5");
  while ($row = mysql_fetch_array($get_category_games)) {
    $category_game_id    = $row['id'];
    $category_game_title = $row['game_title'];
    $category_game_display .= '<li><img 
      class="category_module_img" 
      src="http://www.game.assets.buddyweb.me/' . 
        $category_game_id . 
        '/_thumb_100x100.png"></li>';
  }
  $category_display .= '<div class = "category_module">
  <h4>'.$category_name.'</h4>
  '.$category_game_display.'
  <div class="play_more_btn">More</div>
  </div>';
}

但我得到的是从列表中第一个类别的查询中出现的每个游戏。

1 个答案:

答案 0 :(得分:0)

我认为问题是$ row变量。

试试这个:

$get_categories_query = mysql_query("SELECT * FROM game_categories ORDER BY category_name ASC");

while($row = mysql_fetch_array($get_categories_query))
{
    $category_name = $row['category_name'];
    $get_category_games = mysql_query("SELECT * FROM games WHERE category = '$category_name' ORDER BY RAND() LIMIT 5");
    $category_game_display = false; // new!!

    while($row_cat = mysql_fetch_array($get_category_games))
    {
        $category_game_id = $row_cat['id'];
        $category_game_title = $row_cat['game_title'];
        $category_game_display .= '<li><img class = "category_module_img" src = "http://www.game.assets.buddyweb.me/'.$category_game_id.'/_thumb_100x100.png"></li>';
    }

    $category_display .= '<div class = "category_module"><h4>'.$category_name.'</h4>'.$category_game_display.'<div class = "play_more_btn">More</div></div>';
}