0

Hello everyone.

I am pretty new to oop, so this might be a simple question.

I need to get data from my database and I need it in several different places on my site. So I decided to create a class to get the data instead of writing it over and over again.

now, the method i created is nothing but a mysql query and a while loop to get the variables:

function getGroupData(){

$result = mysql_query("SELECT * FROM groups ")
or die(mysql_error());

while($row = mysql_fetch_array($result)){



	$id = $row["id"];
	$groupname = $row["groupname"];
	$groupurpose = $row["groupurpose"];
	$groupmessage = $row["groupmessage"];

}
}

of course this sits inside a class and there is a "connect to my db.php" included...

the problem is that the variables (i.e $id, $groupname etc...)do not seem to exist outside of the method.
How can I use them in other methods and outside the method? I need a solution that can handle methods which create dozens of variables.

even if I declare them global (i.e global $id; global $groupname; etc...) before the mysql query, they still don't exist outside the method.


Thank you for your help!

4
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by pritaeas
1

One solution would be to return them as an array from your function, like this:

function getGroupData() {
  $return_value = array();
  $result = mysql_query('SELECT * FROM groups') or die(mysql_error());
  while ($row = mysql_fetch_array($result)) {
    $return_value[] = $row;
  }
  return $return_value;
}

$data = getGroupData();
print_r($data);

Edited by pritaeas: n/a

0

Returning an array makes most sense for a 'getter' method. You can also create class properties to hold the information:

class Model_Group {

  public $groups = array();

  public function getGroupData()
  {
    // Check if property was already initialized earlier.
    if(!empty($this->groups)) return $this->groups;  

    $return_value = array();
    $result = mysql_query('SELECT * FROM groups') or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
      $return_value[] = $row;
    }
    $this->groups = $return_value;  // store in property for future retrieval.
    return $return_value;
     
  }
}
0

Thank you both for your help. But I'm not sure I understand how to use your solution. (As I said, I am new to OOP). Could you show me how to retrieve the variables outside the method? And how do I send them to other methods in the class?

Thank a lot!

0

here for ur last reply .. hope can help
to access the variable just refer the it by

$yourCLasssinstance->yourVariable;

and for returning function

$watEver = $yourCLasssinstance->yourMethod();

and if u mean how to use the vars outside the method but in same class
you need to declare them in the class header out side the methods
and then use

$this->maVariable;

where $this is always refer to ur class instance it self
hope u got it
good luck

0

Building on madCoder's nice example:

<?php
class Model_Group {
 
  public $groups = array ();
 
  public function getGroupData()
  {
    // Check if property was already initialized earlier.
    if(! empty($this->groups)) 
      return $this->groups;  
 
    $result = mysql_query('SELECT * FROM groups') or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
      $this->groups[] = $row; // store in property for future retrieval.
    }
  }
  
  public function Test()
  {
    // You should connect to the database first
    $this->getGroupData();
    print_r($this->groups);
  }
}

$object = new Model_Group();
$object->Test();

// you can also do
// print_r($object->groups);
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.