Достъп до променлива - ООП

DirectX

Registered
Във функцията getPersonaldata ми е завката и т.н. Как мога да изкарам на екрана само името на потребителя или пък името и фамилията и т.н.
PHP:
include 'connect.db.php';

class Users
{
		
	function getPersonalData($id)
	{
		$getData = mysql_query("SELECT * FROM `sobstvenici` WHERE `id`='$id'");
		$getDataRows = mysql_num_rows($getData);
		if($getDataRows == 0)
		{
			echo "няма селекция за $id";
		}
		else
		{
			while($row = mysql_fetch_array($getData))
				{
					$name = $row['ime'];
					$surName = $row['prezime'];
				}
		}
	}
	
}

$user = new Users;
$id = $_GET['id'];
echo $user->getPersonalData($id);
 
PHP:
include 'connect.db.php';


class Users {

private $personalData = array(
'ime' => null,
'prezime' => null
);
		
public function getName() {
  if($this->personalData['ime'] != null) {
       return $this->personalData['ime'];
  }
       return 'няма селекция на име с ID: '. $id;
}


public function getPrezIme() {
  if($this->personalData['prezime'] != null) {
       return $this->personalData['prezime'];
  }
       return 'няма селекция на prezime с ID: '. $id;
}


public function getPersonalData($id) {
$getData = mysql_query("SELECT * FROM `sobstvenici` WHERE `id`='$id' LIMIT 1 ");
          $row = mysql_fetch_array($getData);
           if(count($row) > 0) {
                $this->personalData['ime'] = $row['ime'];
                $this->personalData['prezime'] = $row['prezime'];
           }
           return $this;
}



/** Cast to int. */
$id = (int)$_GET['id'];

/** New instance. */
$user = new Users;

/** Collect personal data. */
$userData = $user->getPersonalData($id);

/** Show personal data. */
echo $userData->getName();
echo $userData->getPrezIme();

/** Can be called like this but a new request to DB will be made. */
echo $user->getPersonalData($id)
                     ->getName();
 
Coooooooooool :lol: Благодарско !
А сега за вскяа една колона от таблицата си създавам функция (не че ми е проблем, просто са около 17-18) или има нещо по-рационално
 
Сега ще ти записва всички имена от таблицата и съответната им стойност. Ще бъдат достъпни през getData('pole_ot_tablicata');

PHP:
include 'connect.db.php';


class Users {

private $personalData = array();

public function getData($k) {
  if(isset($this->personalData[$k])) {
       if(!empty($this->personalData[$k])) {
           return $this->personalData[$k]; 
      }
   }
         return 'Record dosent exists.';
}


public function getPersonalData($id) {
$getData = mysql_query("SELECT * FROM `sobstvenici` WHERE `id`='$id' LIMIT 1 ");
          $this->personalData = mysql_fetch_array($getData);
           return true;
}



/** Cast to int. */
$id = (int)$_GET['id'];

/** New instance. */
$user = new Users();

/** Collect personal data. */
$user->getPersonalData($id);

echo $user->getData('ime');
echo $user->getData('prezime');
echo $user->getData('nqkakvo_pole_ot_db');
 

Back
Горе