Hi there,

i am trying to use the classes proplery and i have a basic question....The following is the class i am using in order to get information from the DB...It is workinkg and i dont have any problem. But the question is how do i show the information on a form based on the data i got from the class?


file name: user.class.php

class User 
{


	function __construct()
	{	
	}

	public  function getRecord($var)
	{
	   try
		{
		/*** query ***/
		$sql  = " SELECT * FROM users WHERE userID= :id ";		  
			
		/*** prepare the select statement ***/
		$rs = db::getInstance()->prepare($sql);
			
		/*** bind the parameters ***/
		$rs->bindParam(':id'	, $var);
			
		/*** execute the prepared statement ***/
		$rs->execute();
			
		/*** Save result in $result ***/
		$result = $rs->fetch(PDO::FETCH_ASSOC);
			
		return $result;
		}  
		catch (PDOException  $e)	
		{
			print $e->getMessage();
		}
	}
}//close class

on the web form i do this:

$User =new User();
$User->getRecord($_GET[userid]);

i know that i could do the following and it will work, but i am not sure if it;s the best way to do it:

$User =new User();
$Row = $User->getRecord($_GET[userid]);
<input name="first_name" type="text" id="first_name" value="<?=$Row['first_name']?>" />

I just want to do it right....

I would set some public variables and when a new members is conctructed populate those variables. i.e.

class User {
    public $userID;
    public $FirstName;
    public $LastName;
    public $EMail;

    function user($ID)
    {
      $sql  = " SELECT * FROM users WHERE userID=$ID";

      //INSERT YOUR DB CODE HERE AND STORE THE RESULTS IN ARRAY CALLED fieldValues

      foreach($fieldValues as $key => $value) {
        $this->$key = $value;
      }
    }
}

Than when you create a new user object you can access the public variables with ease.

<?php

$user = new User($_GET['userid']);

?>

<input name="first_name" type="text" id="first_name" value="<?= $user->FirstName; ?>" />

The way you have it above acts more like a function than a class

Thank you for your quick reply.....i have few more basic questions:
The fact that you called the method user, the same as the class...and then you just did

<?php
 $user = new User($_GET['userid']);
?>

without calling the method...would that work?

and...
what about if i want to call the method user like i did..."getRecord()"
how would you use the it?

Thanks again...and very sory for the basic question

By the way...
i love the

foreach($fieldValues as $key => $value) {
$this->$key = $value;
}

you have no idea how many lines of code you just saved me....

The function is named the same as the class therefor when you initiate the class it automatically runs the function. If you wanted to manually do it yourself than just change the function name to something else i.e.

class User {
    public $userID;
    public $FirstName;
    public $LastName;
    public $EMail;

    function populateData($ID)
    {
      $sql  = " SELECT * FROM users WHERE userID=$ID";

      //INSERT YOUR DB CODE HERE AND STORE THE RESULTS IN ARRAY CALLED fieldValues

      foreach($fieldValues as $key => $value) {
        $this->$key = $value;
      }
    }
}
<?php

$user = new User();
$user->populateData($_GET['userid']);

?>

<input name="first_name" type="text" id="first_name" value="<?= $user->FirstName; ?>" />

Hi,

Here is my humble contribution using FETCH_CLASS PDO object.. I posted this as a response in one of the question weeks ago. I brushed it up a little to become relevant to your question.

<?php
class User{
## Property declaration NOTE! All these variables or properties are the column names of table user
public $name;
public $lastname;
public $username;
public $hobby;
## end of property declation

## YES! NO! we are not going to have a __construct this time.. we will be passing this class to our pdo object

public function show_userInfo(){
## The following will be output on the page once the PDO statement is fetched.
$u_info = "";
$u_info .= 'Name: '.$this->name.'<br/>';
$u_info .= 'Lastname: '.$this->lastname.'<br/>';
$u_info .= 'Username: '.$this->username.'<br/>';
$u_info .= 'Hobby: '.$this->hobby.'"/><br/>';
 return $u_info;
 
 ## end of function show_userInfo()
}

} 
## settings.php is a file where the database user, password, and db name is located.
## they are defined as follows; $db_host = "localhost";$db_user = "username"; $db_password = "db password";$db_database = "dbName";
include_once 'includes/settings.php';

#### The PDO objects ###############
try {
    $someDb = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
    ## Say Hi to Morpheous and Neo, if we are connected
    echo 'Hi, Morpheous and Neo!<br />'; // remove this just trying to amaze myself.

   ## define our connection query
   $query = "SELECT * FROM users WHERE userID= '".$u_id."'";

    ## prepare to fetch PDOStatement objects 
    $pdo_Object = $someDb->query($query);
	
	## comment two lines below if you don't need to show count of affected rows.
    $count = $pdo_Object->rowCount();
    echo $count."<br/>";
	
    ## Now, lets fetch the  User class above. using the FETCH_CLASS pdo object.
    $obj = $pdo_Object->fetchALL(PDO::FETCH_CLASS, 'User');

  ## results are now in users
    foreach($obj as $users)
        {
     ## remember $u_info above? This is where we can call it.
        echo $users->show_userInfo().'<br />';
        } 
    ## Again, we need to be polite to our database server by closing what we just opened.
    $someDb = null;
}
## We can either eliminate these remaining two lines below if needed be, but it is highly necessary when testing your class or the script.
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
	?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.