PHP - 将mysql结果数组转换为局部变量数组?

时间:2012-04-22 13:39:52

标签: php arrays

我有以下代码:

$catsQuery = "
        SELECT id, category
        FROM categories
        ORDER BY category
";
$catsSql = mysqli_query($link, $catsQuery);

$cats = array();

mysqli_data_seek($catsSql, 0);
while($rowCats = mysqli_fetch_assoc($catsSql))
{
    $cats[$rowCats['id']]['catName'] = $rowCats['category'];
}

foreach ($cats as $catKey => $cat['$catName'])
{
    $pageContent .= "<p>$catKey | $catName</p>
    ";
}

两列idcategory包含以下数据:

  • id |类
  • 1 |鞋
  • 2 |衬衫
  • 3 |帽子

我想要做的是将表categories中的所有行转换为名为cats

的局部变量

在while循环中,生成本地数组,因为每个行数组键成为类别ID,然后为该键分配类别名称。 var_dump($cats)表示每行都会添加到我所描述的变量中。

但是在foreach中,由于某种原因,每个id都作为新行返回,但类别的名称显示为最后一个可能的类别名称,覆盖所有键。

为什么变量上的转储显示一组数据,但是这种内容的打印循环显示出这种奇怪的打嗝?

像这样:

  • 1 |帽子
  • 2 |帽子
  • 3 |帽子

如果有人能够解释我做错的事情,那将非常感激。

谢谢!!

编辑:

由于以下答案而创建的功能代码:

//categories to local array
$catsQuery = "
    SELECT id, category, catDirPath
    FROM categories
    ORDER BY category
";
$catsSql = mysqli_query($link, $catsQuery);
$cats = array();
mysqli_data_seek($catsSql, 0);
while($rowCats = mysqli_fetch_assoc($catsSql))
{
    $cats[$rowCats['id']]['catName'] = $rowCats['category'];
    $cats[$rowCats['id']]['catPath'] = $rowCats['catDirPath'];
}
// subCats to local array
$subsQuery = "
    SELECT id, subCat, category_id, subDirPath
    FROM subCats, sub_categories
    WHERE subCats.id = sub_categories.sub_id
    ORDER BY category_id
";
$subsSql = mysqli_query($link, $subsQuery);
$subs = array();
mysqli_data_seek($subsSql, 0);
while($rowSubs = mysqli_fetch_assoc($subsSql))
{
    $subs[$rowSubs['id']]['subName'] = $rowSubs['subCat'];
    $subs[$rowSubs['id']]['catId'] = $rowSubs['category_id'];
    $subs[$rowSubs['id']]['subPath'] = $rowSubs['subDirPath'];
}
// loop through categories and if subs exist, add to resultset and display  
foreach ($cats as $catId => $cat)
{
    $pageContent .= "
    <div style='margin-bottom: 10px; border black solid 1px;'>
    <ul>
    <li style='list-style-type: none; margin-bottom: 5px;'><a href='$docPath/category/{$cat['catPath']}/'>{$cat['catName']}</a></li>
    ";
    foreach ($subs as $subId => $sub)
    {
        if($sub['catId'] == $catId)
        {
        $pageContent .= "
        <li style='list-style-type: circle; padding-left: 15px; font-size: 70%;'><a href='$docPath/category/{$cat['catPath']}/{$sub['subPath']}/'>{$sub['subName']}</a></li>
        ";
        }
    }
    $pageContent .= "
    </ul>
    </div>
    ";
}

2 个答案:

答案 0 :(得分:2)

在构建阵列时,你必须按照以下方式进行foreach循环:

foreach ($cats as $catId => $cat)
{
    $pageContent .= "<p>$catKey | ".$cat['catName']."</p>";
}

但为什么不首先只是$cats[$rowCats['id'] = $rowCats['category'];? 那就足够了:

foreach ($cats as $catId => $catName)
{
    $pageContent .= "<p>$catKey | $catName</p>";
}

答案 1 :(得分:2)

您的代码看起来不错。在while循环之后你在$ cats数组上尝试了var_dump吗?我怀疑这条线路引起了你的问题

foreach ($cats as $catKey => $cat['$catName'])

你应该改变它

foreach ($cats as $catKey => $catDetails)
{
    $catName = $catDetails['catName'];
    $pageContent .= "<p>$catKey | $catName</p>";
}