Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access variables in PDO Object Class

Posted on 2014-12-08
5
Medium Priority
?
199 Views
Last Modified: 2014-12-09
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.
0
Comment
Question by:xxalex
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40488617
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
 

Author Comment

by:xxalex
ID: 40488637
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40488704
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
 

Author Closing Comment

by:xxalex
ID: 40488727
This is perfect! I just wasn't sure how to access all the details stored in the object but this makes perfect sense.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40490104
Thanks for the points -- it's a great question! ~Ray
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question