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
Populating Fields in Webpage with Data from Database
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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 ...Output?
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!
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!
Still Confused ...
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 ?
Thanks!
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>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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>');