Forum Moderators: coopster

Message Too Old, No Replies

Need Help With Some PHP CODE

         

JustDontGetIt

12:43 pm on Aug 19, 2010 (gmt 0)

10+ Year Member



I understand HTML to the L but I am a little confused on PHP. This is what I am trying to do so hopefully I will describe it to where you will understand.

I am using a MySQL database and have designed up an html template for each of my "customers" using html. What I am wanting to do is insert the information from a row in the database for that particular customers.. so in other words on row 2 in the database in under the column agent_name that i want to insert into a certain area on the template, then on same row I have a column named description that I want to insert that information into a certain area on the template. I understand that I will need to save the template as an .php. Also, for each template I do for each customer, will I have to use the open database code for each one? BTW I have Macromedia Dreamweaver 8 where I've noticed you can insert php but am still a little confused.

Any help would be greatly appreciated!

Matthew1980

2:00 pm on Aug 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there JustDontGetIt,

Welcome to WebmasterWorld: [webmasterworld.com ]

Could you post any code relevant to your issue then we can hopefully advise you from there :) So long as it isn't a huge code dump - no one likes other peoples homework...

Cheers,
MRb

rocknbil

6:00 pm on Aug 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP is a dynamic programming language, it's not a different type of "web page." So you'll need to start thinking a little differently.

I understand that I will need to save the template as an .php.


You could, but generally speaking, false, read on . . .

on row 2 in the database in under the column agent_name that i want to insert into a certain area on the template


First, get used to pulling data values by numeric unique columns. Don't do "select * from table where field='some-text-value' " unless you have to, this is slower and can be error prone.

<a href="somescript.php?rec=1234">John Smith</a>

select * from table where id=$_GET['rec'];

You need to dig into cleansing user input too, this is very important.

So you get your record, and store the data in variables.

select * from table where id=$_GET['rec'];
$result = mysql_query($query) or die(mysql_error());
if ($row=mysql_fetch_array($result)) {
$agent_name = $row['agent_name'];
$description = $row['description'];
}

Now your template: it can be an HTML file, a text file, doesn't matter, it's all text anyway. The advantage using it as a PHP file is that you can execute PHP instead of markers, in the example below,

<title><?php echo $agent_name ?></title>

So either method works. What you want in it are "markers" or "placeholders". You use something that would never occur normally for your markers. For example,

<html>
<head>
<title>[HEADING]</title>
</head>
<body>
<h1>[HEADING]</h1>
[DESCRIPTION]
</body>
</html>

Then you open the template and substitute the markers for the variables. For later versions of PHP, file_get_contents works as well. I use this method because it's easier to go line by line (I have reasons . . .)


$template='/full/path/to/template.txt';
$fout=null;
$fhandle = fopen($template, "r");
//
if (is_file($template) and $fhandle) {
while (!feof($fhandle)) {
$buffer = fgets($fhandle, 4096);
if (preg_match("/\[HEADING]/",$buffer)) {
$buffer = stripslashes(preg_replace("/\[HEADING\]/",$agent_name,$buffer));
}
if (preg_match("/\[DESCRIPTION]/",$buffer)) {
$buffer = stripslashes(preg_replace("/\[DESCRIPTION\]/",$description,$buffer));
}
$fout .= $buffer;
}
else { $fout = "not a file"; }
echo $fout;


should give you

<html>
<head>
<title>John Smith</title>
</head>
<body>
<h1>John Smith</h1>
This is the description for John Smith.
</body>
</html>


Lots of stuff "left out," but that's the general concept.

Dreamweaver


Ugh. :-) You'll be spending much more time in the code window from here on out. You should also have HomeSite too, if you got it on CD . . . it's in the disk and best.editor.ever.