Php 导航列表正在复制

Php 导航列表正在复制,php,mysql,Php,Mysql,我之前得到了关于如何将多个表连接在一起以使此导航列表正常工作的帮助,正如您所看到的,我已经完成了此操作,但现在我正在尝试在列表中输出导航,但它是根据这些类别中的产品数量复制顶部和底部类别:这是显示我的表格设置的上一个链接: 下面是我的代码,它试图正确地回显导航 try { $result = $pdo->query('SELECT product.*, bottom_category.bottom_name, top_category.top_name

我之前得到了关于如何将多个表连接在一起以使此导航列表正常工作的帮助,正如您所看到的,我已经完成了此操作,但现在我正在尝试在列表中输出导航,但它是根据这些类别中的产品数量复制顶部和底部类别:这是显示我的表格设置的上一个链接:

下面是我的代码,它试图正确地回显导航

try
{ 
    $result = $pdo->query('SELECT product.*, bottom_category.bottom_name, top_category.top_name
                            FROM product
                            INNER JOIN bottom_category ON bottom_category.id = product.bottom_category_id 
                            INNER JOIN top_category ON top_category.id = bottom_category.top_category_id
                            ORDER BY top_category.id,bottom_category.id');
} // end try
catch (PDOException $e) 
{ 
    echo 'There was a error fetching the products.' . $e->getMessage();
    exit(); 
} // end catch

$products = array();

foreach ($result as $row)
{
$products[] = array('top_name' => $row['top_name'], 
              'bottom_name' => $row['bottom_name']);
}

?>

<div class="sidebar">
    <h4 class="sidebar-header">Select Products</h4>
    <form class="nav-search-form">
        <input type="search" name="search" placeholder="Search products">
    </form>
    <nav class="sidebar-links"> 
        <ul>
            <li><a id="red" href="/semtronics/index.php">New Products</a></li>
            <?php
            foreach ($products as $product):
            ?>
            <li><a href="#"><?php echo htmlspecialchars($product['top_name']);?></a>

                <ul>
                    <li><a href=""><?php echo htmlspecialchars($product['bottom_name']);?></a></li>
                </ul>
            </li>
            <?php endforeach; ?>        
        </ul>
    </nav>
</div><!-- sidebar -->
试试看
{ 
$result=$pdo->query('SELECT product.*,bottom\u category.bottom\u name,top\u category.top\u name
从产品
在bottom\u category.id=product.bottom\u category\u id上内部联接bottom\u category
在top\u category.id上的内部联接top\u category=底部\u category.top\u category\u id
按顶部_category.id、底部_category.id')排序;
}//结束尝试
捕获(PDO$e)
{ 
echo“提取产品时出错”。$e->getMessage();
退出();
}//端盖
$products=array();
foreach($结果为$行)
{
$products[]=数组('top_name'=>$row['top_name'],
'bottom_name'=>$row['bottom_name']);
}
?>
精选产品

现在一切都正常了唯一的问题是,它会根据链接到该类别的产品数量复制导航列表。

我认为您需要另一个解决方案来完成此任务。 这里不需要产品

查询您需要的是:

select bottom_category.name as bottom_name, top_category.name as top_name
from bottom_category
inner join top_category on top_category.id = bottom_category.top_category_id
order by top_category.id, bottom_category.id
我认为您需要代码中
product
表中的一些内容,但如果没有,请使用上面的SQL查询

或者您需要产品表中的数据

如果需要从
产品
运行

select bottom_category.name as bottom_name, top_category.name as top_name
from product
inner join bottom_category on bottom_category.id = product.bottom_category_id 
inner join top_category on top_category.id = bottom_category.top_category_id
group by product.bottom_category_id
order by top_category.id, bottom_category.id

但是要小心,您不知道在这种情况下会使用product中的哪一行,top query正在工作,但它仍在循环重复的top_名称类别,因为我有多个top_名称类别链接回一个top_名称id。这是因为我有多个top_名称类别,它们也属于一个top_名称类别。我想我不明白... 查找
插入到顶级类别值(1,“1”),(2,“2”)
插入底部类别值(1,“b11”,1),(2,“b12”,1),(3,“b21”,2),(4,“b22”,2)在输出中需要4条记录还是2条?