Page 1 of 1
Populating Fields in Webpage with Data from Database
Posted: Fri Mar 11, 2005 10:57 am
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
Posted: Fri Mar 11, 2005 11:16 am
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.
Output?
Posted: Fri Mar 11, 2005 11:32 am
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!
Posted: Fri Mar 11, 2005 11:37 am
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.
Still Confused ...
Posted: Fri Mar 11, 2005 1:46 pm
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!
Posted: Fri Mar 11, 2005 1:48 pm
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>');