I have a database >> table
I want to display those entries in a page
I already have made a class and want to make a function to read data from database.
I want to load those data in the member variables in the class when the function is called.
But I cant understand one thing. The class(object created) will hold only one row from database.
Here is the problem
What if i want to show all the data. I can use only functions inside the class coz i want to make it object oriented design.
For this i will have to read all rows and create an array of objects. But I dont know how to implement it using a function which itself is inside same class.
Or Is there any way to create a next() function to load next row into the object.
Like $myObj->next(); will load values from next row into the object.
Here is the class structure
Code: Select all
class A {
private $x;
private $y;
private $z;
function __construct() {
$x=NULL;
$y=NULL;
$z=NULL;
}
public function getDataFromDB() {
// connect to database
// Get a row from database and store it to x,y,z
$this->x = row['x'];
$this->y = row['y'];
$this->z = row['z'];
}
public function retX() {
return $this->x;
}
public function retY() {
return $this->y;
}
public function retZ() {
return $this->z;
}
public function displayData() {
echo $x.$y.$z;
}
}
$myA = new A();
$myA->getDataFromDB(); /// This will read all the rows but load only one row.
$myA->displayData(); /// This will display 1st row coz only fst row is loaded into $myA
/// To solve the problem I need an array of objects. Like $myA[];
/// But I cant understand how to load 1st row in $myA[0] and 2nd row in $myA[1] and sooo on.
/// ANother way to solve this problem is to make some $myA->next(); function
// Which will load the next row into $myA;