PHP数组从其他数组中选择结果

时间:2016-01-18 15:04:28

标签: php arrays

我有一个带有某个类别的sql表,我把它们放在一个数组中..一切都很好但是当我尝试从另一个表foreach类别获取数据时,总是返回我选择的第一个类别。

这是我的代码:

$gameguidecategoryes = array();
$gameguides = array();  


$dbselectgameguidecategoryes = new DB_MSSQL;
$dbselectgameguidecategoryes->Database=$newweb_db;
$dbselectgameguidecategoryes->query("Select GameGuideCatNr,GameGuideCatName_$languageid as GameGuideCatName,GameGuideCatImage from GameGuide_Category where GameGuideCatVisible = 1 order by GameGuideCatOrder asc");
for($i=0;$i < $dbselectgameguidecategoryes->num_rows();++$i) { 
    if ($dbselectgameguidecategoryes->next_record()){
        $GameGuideCatNr = $dbselectgameguidecategoryes->f('GameGuideCatNr');
        $GameGuideCatName = $dbselectgameguidecategoryes->f('GameGuideCatName');
        $GameGuideCatImage = $dbselectgameguidecategoryes->f('GameGuideCatImage');
    }
    $gameguidecategoryes_temp = array(
        'ggcname' => $GameGuideCatName,
        'ggcimg' => $GameGuideCatImage,
    );

    $gameguidecategoryes[$i] = $gameguidecategoryes_temp;

    $dbselectgameguide = new DB_MSSQL;
    $dbselectgameguide->Database=$newweb_db;
    $dbselectgameguide->query("Select GameGuideID,GameGuideName_$languageid as GameGuideName from GameGuide_Content where GameGuideCat = $GameGuideCatNr and GameGuideVisible = 1 order by GameGuideOrder asc");

    for($ii=0;$ii < $dbselectgameguide->num_rows();++$ii) {
        if ($dbselectgameguide->next_record()){
            $GameGuideID = $dbselectgameguide->f('GameGuideID');
            $GameGuideName = $dbselectgameguide->f('GameGuideName');
        }
        $gameguides_temp = array(
            'ggid' => $GameGuideID,
            'ggn' => $GameGuideName,
        );

        $gameguides[$ii] = $gameguides_temp;
    }
}

为什么$ gameguides仅从第一类返回数据?

谢谢

2 个答案:

答案 0 :(得分:1)

更好地处理清洁代码

$gameguidecategoryes = $gameguides = $gameguidescategoryids = array();  

$dbselectgameguidecategoryes = new DB_MSSQL;
$dbselectgameguidecategoryes->Database=$newweb_db;
$dbselectgameguidecategoryes->query("Select GameGuideCatNr,GameGuideCatName_$languageid as GameGuideCatName,GameGuideCatImage from GameGuide_Category where GameGuideCatVisible = 1 order by GameGuideCatOrder asc");

while ($dbselectgameguidecategoryes->next_record()) {
    $gameguidescategoryids[] = $dbselectgameguidecategoryes->f('GameGuideCatNr');

    $gameguidecategoryes[] = array(
        'ggcname' => $dbselectgameguidecategoryes->f('GameGuideCatName'),
        'ggcimg' => $dbselectgameguidecategoryes->f('GameGuideCatImage'),
    );
}

if (count($gameguidescategoryids)) {
    $dbselectgameguide = new DB_MSSQL;
    $dbselectgameguide->Database=$newweb_db;
    $dbselectgameguide->query("Select GameGuideID,GameGuideName_$languageid as GameGuideName from GameGuide_Content where GameGuideCat IN (".implode(',', $gameguidescategoryids).") and GameGuideVisible = 1 order by GameGuideOrder asc");

    while ($dbselectgameguide->next_record()){
        $gameguides[] = array(
            'ggid' => $dbselectgameguide->f('GameGuideID'),
            'ggn' => $dbselectgameguide->f('GameGuideName'),
        );
    }
}

答案 1 :(得分:1)

第二个循环不断被第一个循环破坏。例如考虑一下会发生什么:

  1. 您可以抓取您的类别,并(让我们假装)其中有4个。
  2. 您可以在$gameguidecategoryes[0]
  3. 中存储一些信息
  4. 您运行第二个查询,获取类别#0的一些内容,比如3条记录
  5. 它存储在$gameguides[0][1][2]
  6. 您的外部循环再次打勾,然后从categoryes[1]
  7. 开始
  8. 内部循环再次勾选,您将获得4条记录,现在您再次将它们存储到 SAME 中:$gameguides[0][1],{{1 },[2]等...
  9. 您现在已经删除了在第一个循环中提取的数据,并且
  10. 为您获取的每个类别执行此操作。

    此代码效率很低。您应该学习如何使用[3],并获取单个结构,例如

    JOIN

    然后像

    SELECT category.id, category.name, ...., content.id, content.name
    FROM categories
    LEFT JOIN content ON categories.id = content.category_id
    ORDER BY ...