Populating Fields in Webpage with Data from Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hbnmgr
Forum Newbie
Posts: 13
Joined: Mon Mar 07, 2005 8:22 pm

Populating Fields in Webpage with Data from Database

Post by hbnmgr »

While understanding the Logic, I lack the necessary coding to perform the following. To populate a webpage by replacing keywords with data from a database thereby customizing the webpages relating to a specific member.

Creating a Replicated Webpage on the Fly using PHP & MySQL;
Using Session_Query to capture the DirectoryName,

How do I populate the Fields in the webpage with data from a Database?
What code would best perform this feature?

I understand using function calls to access the database, and believe it would be best to use SessionID to keep the browser on the same Member so as to keep populating additional pages with the same Member Data. But unsure how to do this either.

Any help is most appreciated. Trying to Learn PHP / MySQL with book "PHP & MySQL WEB DEVELOPMENT".

Thanks! SRR/arr
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

// database connection already established...

$sql = 'SELECT * FROM `users` WHERE `user_id` = \'' . $_SESSIONї'userid'] . '\' LIMIT 1';

$query = mysql_query($sql) or die(mysql_error());

$userdata = mysql_fetch_assoc($query);

if($userdata === false || intval($userdataї'userid']) !== $_SESSIONї'userid'])
  die('invalid user data');

function templatize($var)
{
  return '{' . strtoupper($var) . '}';
}

$vars = array();
foreach($userdata as $key => $data)
  $varsїtemplatize($key)] = htmlentities(stripslashes($data), ENT_QUOTES);

// $template is the content with the keywords stored in it.

$output = str_replace(array_keys($vars), array_values($vars), $template);

echo $output;

// ... page continues ...
for more information.. look up templates, template engines, and similar things. Smarty is an oft used template engine.
hbnmgr
Forum Newbie
Posts: 13
Joined: Mon Mar 07, 2005 8:22 pm

Output?

Post by hbnmgr »

echo output has me confused ....

while different tags in the webpage (ie. ~name~ or ~email~, etc.) would refer to different and specific cells in the table how would PHP know to call that specific value from the database and insert it (or replace) where the tags are in the webpage?

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

str_replace() .. the user's data is already extracted into an array via the mysql_* function stuff. The code processes the fields into template variable calls, such as {NAME}, which is searched for and replaced by the vale retrieved from mysql.
hbnmgr
Forum Newbie
Posts: 13
Joined: Mon Mar 07, 2005 8:22 pm

Still Confused ...

Post by hbnmgr »

It is my understanding that if both the search and replace params are arrays, str_replace() will apply the substitutions incrementally.

Wherefore how do you replace say NAME and EMAIL in the example below, with "John" and his email "johnatsomedomain" from the database ?

Code: Select all

<p>If you need to contact {NAME} please use his email {EMAIL}.</p>
Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not try it?

Code: Select all

echo str_replace(array('{NAME}','{EMAIL}'), array('John','johnatsomedomain'), '<p>If you need to contact {NAME} please use his email {EMAIL}.</p>');
Post Reply