gpt4 book ai didi

PHP 将类对象数组绑定(bind)到列表

转载 作者:行者123 更新时间:2023-11-30 23:34:18 26 4
gpt4 key购买 nike

在 PHP 项目中,我需要列出 MySql 数据库表中的项目。我使用数据访问类创建一个 Actor 对象数组并将其返回到 html 页面并循环对象数组以在下拉列表中列出 Actors。下面给出的代码不会产生任何错误,但不会列出 Actor 。 (我认为问题出在 HTML 页面中的 Array 循环中。)请帮助我修复此代码...

数据访问(ActorDA.php)

function SelectAll(){

$oDb = new db;
$result = $oDb->Query('SELECT Id, Name from actor WHERE IsActive=1 ORDER BY Name');

$result_array = array();
while($row = $result->fetch_assoc())
{
$oActor = new Actor();
$oActor->ActorId = $row['Id'];
$oActor->Name = $row['Name'];
$result_array[] = $oActor;
}

return $result_array;
}

业务逻辑 (Actor.php)

include_once  DATAACCESS . 'ActorDA.php';

Class Actor
{

public $ActorId;
public $Name;

public function GetList()
{
$oActorDA = New ActorDA();
return $oActorDA->SelectAll();
}

HTML 页面 (ManageActors.php)

require_once CODE . 'Actor.php';


<select name="ddlActor">
<option value="0">Select a Actor</option>
<?php
$arrActors = Actor::GetList();
foreach($arrActors as $actor){
echo '<option value="' . $actor->ActorId . '">'. $actor->Name . '</option>';
}

?>
</select>

最佳答案

为什么在 Actor 类中调用 $oActionDA->SelectAll()?此方法的任何详细信息?我认为你应该调用 $oActionDA->GetList() 那一行。

关于PHP 将类对象数组绑定(bind)到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8913269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com