Jump to content

Prob : Object oriented design with database.


rahul.pache

Recommended Posts

Here is the situation

 

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.

 

Please suggest me a solution I will be thankful.

 

 

---  EDIT 1  ---

 

Here is the situation

 

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

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;

 

Please suggest me a solution I will be thankful.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.