想要一行数组值而不是列

时间:2012-02-26 18:09:21

标签: php arrays

我整夜都在编码所以我可能忽略了一些东西,但我的输出看起来像这样:

Apartment Name #1                Apartment Name #1            
5.0                              5.0
 1                               1

我希望它看起来像这样:

Apartment Name #1            5.0

以下是我的类'方法:

public function apartmentList($zip = '')
    {
        $this->_zip = $zip;
        $list = array();
            $sql = 'SELECT DISTINCT apartment.apartmentName, ratings.apartmentOverall, apartment.apartmentID
                    FROM apartment_information apartment, apartment_ratings ratings, user_user user
                    WHERE user.preferred_zip = :zip
                    ORDER BY ratings.apartmentOverall';

                    try{
                        $stmt = $this->_db->prepare($sql);
                        $stmt->bindParam(":zip", $this->_zip, PDO::PARAM_STR);
                        $stmt->execute();            
                        while($row = $stmt->fetch())
                        {
                            $list['apartmentName'] = $row['apartmentName'];
                            $list['apartmentOverall'] = $row['apartmentOverall'];
                            $list['apartmentID'] = $row['apartmentID'];
                            print_r($list);
                        }
                        return $list;

                   }
                   catch(PDOException $e)
                   {
                       echo $e->getMessage();
                   }

    }

这就是我打电话时的样子:

include_once 'class.apartment.php';
                        $apartment = new Apartment($db);
                        $apartmentList = $apartment->apartmentList($_SESSION['zip']); 
                        foreach($apartmentList as $key => $value){
                        ?>
                        <li id="listing">
                            <a href="../template/apartment.php">
                            <span class="arrow">&nbsp;</span>
                                <span class="title">
                                    <?php echo $value; ?>
                                </span>
                                <span class="rating">
                                    <?php echo $value; ?>
                                </span>
                            </a>    
                            </li>                   

2 个答案:

答案 0 :(得分:0)

代码很好,这是css的问题,真的。但是如果您想要精确格式化,请确保使用容器(li)包装列表项(ul)。无论如何,这都是必需的。

另外,删除id="listing"。它不是唯一的节点,因为它应该是。

答案 1 :(得分:0)

您打印的值是两次,而不是键和值