PHP数组只有唯一值

时间:2017-05-09 18:09:11

标签: php mysql arrays

我是编码的初学者,我正在尝试理解和调整以下代码,只返回唯一值而不重复数据库。我尝试过unique_array()的不同变体,但遗憾的是没有成功。如何将数据库返回的值限制为唯一值?

<?php
$sql ="SELECT COUNT( status) AS total_building FROM add_bulding WHERE status=1";
$query = $this->db->query($sql);
$total_building = $query->row_array();
?>

            <div class="b-list-section">                                   

                    <?php
                    if(!empty($building_info))
                    {   
                        foreach ($building_info as $value) 
                        {                       
                            $id = $value['id'];
                            $sql = "SELECT COUNT(DISTINCT building_id) AS total_list FROM add_listing WHERE  building_id='$id'";
                            $query = $this->db->query($sql);                                
                            $total_list = $query->row_array();

                        ?>
                            <a class="blink" target="_blank" href="<?php echo base_url() . 'details_building/' . $value['id'] ?>">
                                <div class="bilding-wrap">
                                    <h3 class="buildingtitle"><?php echo $value['title']; ?></h3>
                                      <?php if(!empty($value['image'])):?>
                                       <img class="bimage" src="<?php echo base_url("uploads/thumb/" . $value['image']); ?>"  alt="no image yet">
                                      <?php else:?>

                                      <?php $qry12=mysql_query("SELECT * FROM add_bulding_image WHERE add_building_id=$id");
                                         $sqr1=mysql_fetch_array($qry12);                        ?>                      
                                         <img class="bimage" src="<?php echo base_url("uploads/thumb/" . $sqr1['image']); ?>"  alt="no image yet">

                                         <?php endif;?>


                                </div> 
                            </a>

以上代码返回与用户搜索内容相匹配的列表。由于2个列表可以位于同一建筑物中,因此代码将返回同一建筑物两次。

1 个答案:

答案 0 :(得分:0)

尝试将您的查询更改为:

SELECT COUNT(building_id) AS total_list FROM add_listing WHERE  building_id='$id' GROUP BY building_id