PHP-MYSQL记录显示多个

时间:2018-10-16 23:32:05

标签: php mysql

我创建了一个代码,用于显示记录中的ITEMS中的类别。

如果ITEM 1是Phones,则代码显示电话。但是问题是,当同一类别中有两个以上时,代码显示相同类别的2倍以上

https://i.imgur.com/Zq5oahQ.png

我的代码是

$select_parent = "SELECT ads_id, ads_title, category_id FROM public_ads WHERE ads_active = 1 AND ads_genre = 'Loja' AND ads_end = 0";
$parent_query = $con->query($select_parent);

if($parent_query->num_rows > 0) {

    while($parents = $parent_query->fetch_assoc()) {

        $select_categories = "SELECT category_id, category_title FROM public_categories WHERE category_id = '".$parents['category_id']."' ORDER BY category_id Limit 1";
        $category_query = $con->query($select_categories);            

        while($options = $category_query->fetch_assoc()) {

           echo '<li><a href="store?p='. $parents['category_id'] .'&article='. $options['category_title'] .'"><i class="fas fa-angle-right"></i> '. $options['category_title'] .'</a></li>';
        }

   }                                
}  

2 个答案:

答案 0 :(得分:0)

在查询末尾(在$(document).ready(function() { <?php echo $jsLanguageLookupArray ?> $('textarea').each(function() { if ($(this).hasClass('editorHook') || ($(this).attr('name') != 'message' && ! $(this).hasClass('noEditor'))) { index = $(this).attr('name').match(/\d+/); if (index == null) index = <?php echo $_SESSION['languages_id'] ?>; CKEDITOR.replace($(this).attr('name'), { coreStyles_underline : { element : 'u' }, width : 760, language: lang[index] }); } }); }); 条款之后)添加GROUP BY category_id。这样会将结果限制为每个category_id仅一行。

答案 1 :(得分:0)

尝试添加“ DISTINCT” ....,如“ SELECT DISTINCT ads_id ....”中一样