简体   繁体   中英

PHP Bind Array of Class Objects to List

In a PHP project I need to list items from a MySql database table. Im using a Data access class to create an array of Actor objects and return it to html page & loop Object Array to list Actors in dropdown list. Below given code does not produce any arror but it does not list the actors. (I think the problem is in the Array loop in HTML page.) Please help me to fix this code...

Data Access (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;
}

Business Logic (Actor.php)

include_once  DATAACCESS . 'ActorDA.php';

Class Actor
{

public $ActorId;
public $Name;

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

HTML Page (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>

Why you call $oActionDA->SelectAll() up there in Actor class? Any details for this method? I think you should call $oActionDA->GetList() that line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM