Solved

Access variables in PDO Object Class

Posted on 2014-12-08
5
167 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now