Prob : Object oriented design with database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rahul.pache
Forum Newbie
Posts: 9
Joined: Thu Apr 10, 2008 4:07 am

Prob : Object oriented design with database.

Post by rahul.pache »

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

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;
Please suggest me a solution I will be thankful.
Last edited by rahul.pache on Sun Apr 13, 2008 8:36 pm, edited 2 times in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Prob : Object oriented design with database.

Post by s.dot »

Something like this?

Code: Select all

class A
{
    private $data = array();
 
    public function readDB()
    {
        $result = $db->query('QUERY HERE');
        if ($db->numRows())
        {
             while ($array = $db->fetchRow())
             {
                  $this->data[] = $array;
             }
        }
      }
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
rahul.pache
Forum Newbie
Posts: 9
Joined: Thu Apr 10, 2008 4:07 am

Re: Prob : Object oriented design with database.

Post by rahul.pache »

Sir,

That will not solve my problem coz that will give me a string.
I want to load data into object not string...

Thanx for replying....


Plz someone help mee ..... I am really confused !!!!! :banghead:
Post Reply