Access variables in PDO Object Class

I have a database in mysql and I am accessing it via PDO and I can insert and query the database.
My question relates specifically to Object classes and their use.

I have the following SQL query:

$selection = $dbh->query("SELECT customer.*, company.*, address.*
FROM    customer
        INNER JOIN company
            ON customer.company_id = company.company_id
        INNER JOIN address
            ON customer.address_id = address.id ORDER BY customer.surname");

Open in new window


and I have the following object class:

class unique_user {
 public $id, $title, $forename, $surname, $name, $building_name, $address1, $address2, $town, $postcode, $county, $telephone, $email,
 $unique_user;
 public function __construct(){
 $this->unique_user ="{$this->surname}, {$this->title} {$this->forename} {$this->name}, {$this->address1}, {$this->address2}, {$this->postcode}, {$this->telephone}, {$this->email}";
 }
}

Open in new window


and I am querying the database to populate a dropdown list.

          
<?php
$unique_user->setFetchMode(PDO::FETCH_CLASS, 'unique_user');
while ($r = $unique_user->fetch()) {
	echo '<option id="customer" value="',$r->id,'">',  ' ', $r->unique_user, '</option>';
}
?>

Open in new window


All of this works fine but what I would like to do is to display just particular parts of this class unique_user in a form… e.g. On submission from the dropdown I wish to reload the form but with the fields for name address etc already populated.

Can I pull these attributes out of an object class and put them into an existing (rather extensive - so I don't want to re-write it all in php) html form?

… or is there a better way to do this? Should I be using something other than an object class?

Just learning PDO after having not touch php for 8 years… Many thanks for your help.
xxalexAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
I don't think you need a separate class - just some separation of the statements.  All of that mixed up PHP and HTML is a sure recipe for confusion, so I try to separate the programming from the presentation whenever possible.  This uses HEREDOC notation to build the HTML input controls - very useful for templating!

<?php 

// SET DEFAULT VALUES
$surname = $other = NULL;

// IF THERE IS A POST REQUEST, RETRIEVE THE DATA
if (isset ($_POST['enquirertoupdate']))
{
    $customer_to_update->setFetchMode(PDO::FETCH_CLASS, 'customer_to_update'); 
    while ($r = $customer_to_update->fetch()) 
    {  
        $surname = $r->surname;
        $other   = $r->other;
    }
}

// CREATE THE HTML INPUT USING THE DEFAULT VALUES OR THE VALUES FROM THE QUERY
$inputs = <<<EOD
<input 
 type="text" 
 id="surname" 
 maxlength="254" 
 placeholder="Smith"
 autocomplete="off" 
 data-hint="" 
 name="surname" 
 value="$surname"
 />

<input 
 type="text" 
 name="other" 
 value="$other"
 />

EOD;
echo $inputs;

Open in new window

0
 
Ray PaseurCommented:
Can I pull these attributes out of an object class and put them into an existing (rather extensive - so I don't want to re-write it all in php) html form?
Yes,  think so.  The design pattern is not unique to PDO, it's just the regular "request/response" pattern.

Step 1 - query the database and use the rows to create the initial form for the client dropdown select options
Step 2 - when the client selects and submits, your action= script gets the request
Step 3 - using the request data from the initial selection, look up the active record in the database
Step 4 - create the secondary form with data prepopulated from the active record
0
 
xxalexAuthor Commented:
Hi Ray - thanks for taking a look at this.

I have steps 1-3 all working okay I'm just stuck on step 4.
Using the id from my dropdown list I have loaded a page and have the details of the user displayed in  my $unique_user class... I can echo this back at the top of my page so I know that works.

So, if I have a form with the fields Title, Forename, Surname etc how do I populate these with the fields from my $unique_user... I can do the following:

<input type="text" id="surname" maxlength="254" placeholder="Smith"
          autocomplete="off" data-hint="" name="surname" value="<?php if (isset ($_POST['enquirertoupdate'])){$customer_to_update->setFetchMode(PDO::FETCH_CLASS, 'customer_to_update'); while ($r = $customer_to_update->fetch()) {echo $r->surname;}}else {echo"";}?>"/>

Open in new window


and this works - My form shows the surname of the unique user but how do I then fill in the other fields? Do I need to create a separate class for each field that I want to show?

Thanks
Alex
0
 
xxalexAuthor Commented:
This is perfect! I just wasn't sure how to access all the details stored in the object but this makes perfect sense.
0
 
Ray PaseurCommented:
Thanks for the points -- it's a great question! ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.