需要在 PHP class 中创建 return 无序列表

Need to create and return unordered list in PHP class

我正在尝试创建如下列表:

<option value="1">Location 1</option>
<option value="2">Location 2</option>
<option value="3">Location 3</option>

class 中的 Public 函数应该从 DB 中获取所有值并以上面显示的格式输出结果。这是我的功能:

public function FitArea(){
    $sth = $this->db->prepare("SELECT * FROM  `delivery_cost` ORDER BY 'id'");

    $sth->execute();

    $row = $sth->fetch(PDO::FETCH_ASSOC);

    $FitLocation = array("id" => $row["id"], "location" => $row["location"], "cost" => $row["cost"]);

    return $FitLocation;

}

显然我首先在页面上调用 class,但我不确定我是否正确地完成了其余部分,因为没有发生任何事情。

    <?php
    require_once("class/class.get.materials.php");
$getMaterials =  new Getmaterials();
    $FitLocation = $getMaterials->FitArea();
    while($FitLocation > 0){
    print<<<END
    <option value="{$FitLocation['cost']}">{$FitLocation['location']}</option>
    END;
    }
    ?> 

我刚刚弄好了,对于像我这样的新手,这里是我的详细答案:

Public 函数应如下所示:

public function FitArea(){
    $sth = $this->db->prepare("SELECT * FROM  `delivery_cost` ORDER BY id");

    $sth->execute();

    $row = $sth->fetchAll(PDO::FETCH_ASSOC);

    $FitLocation = array();

    for($i=0;$i<count($row);$i++){
        array_push($FitLocation,
            array(
                "id" => $row[$i]["id"],
                "location" => $row[$i]["location"],
                "cost" => $row[$i]["cost"],
            )
        );

    }

    return $FitLocation;

}

剩下的应该是:

<?php
require_once("class/class.get.materials.php");
$getMaterials =  new Getmaterials();
$FitLocation = $getMaterials->FitArea();
foreach($FitLocation as $key => $value){
print<<<END
<option value="{$value['cost']}">{$value['location']}</option>
END;
}
?>

总而言之,自己一个人搞定的感觉真好

感谢大家