Jump to content

[SOLVED] How to combine a php template with MySQL database


patrickm

Recommended Posts

I am having a problem with a script I’m writing. It is using a MySQL database, where it should retreive general information from a product (a template with several variables defined in it in plain text). From the same table, questions are retreived and shown to the visitor of the site. After the questions are answered, the template should be displayed and all variables in it should be substituted with the corresponding answers.

 

In concept, this script works. If I put the template text as a string in the php file, the variables get substituted (example 1). But when it gets the template from a database, it doesn’t work (example 2) ?!?

 

Example 1:

// template text

$text = “Article: dummy1<br>Length: $answer1<br>Height: $answer2… etc. “;

 

Visual output:

Article: dummy1

Length: 315

Height: 504

 

Example 2:

// template text

include("../source/config.php");

$sql = "SELECT * FROM article_info WHERE id = '$id' ";

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

$dump_data = mysql_fetch_object($result) or die (mysql_error());

mysql_close();

text = $dump_data->text;

 

Visual output:

Article: dummy1

Length: $answer1

Height: $answer2

 

And yes, displaying $answer1 and $answer2 in example 2 ("echo $answer1;") gives the proper results.

 

I have been going through php documentation, but I can't seem to find it.

Could anybody give me clue on this one?

Link to comment
Share on other sites

I'm not sure if I fully understand your database / table setup, however try using a different function

 

$row = mysql_fetch_array($result);
$text = "Article: dumm1
Length: $row['col_name_for_answer1']
Height: $row['col_name_for_answer2']
etc...";

 

 

hope it helps!

Link to comment
Share on other sites

Thanks for the thought, but the setup of the database it different.

 

I'll try to explain: the table contains the following colomns:

id, text, question1, specs1, question2, specs2, ... , question15, specs15

 

Depending the product, certain questions are used/not used (not all have the same 15 questions).

Because the questions differ per product, the relevant specs describe what kind of question should be asked (text input, selection with <SELECT>, etc.).

 

When a visitor selects a specific product, the script goes through all questions/specs info and shows the relevant questions.

The answers gieven are not stored in the database. The database is used only for asking the right questions  ;)

Link to comment
Share on other sites

A good night sleep helped a lot.

I solved it by using the str_replace command.

 

The script checks all answers (default being "") and replaces where needed the info in the template (defined as ANSWER1, ANSWER2, etc.).

 

		if ($answer1 <> ""){
			$temp-string = str_replace("ANSWER1",$answer1,$text);
			$text = $temp-string;
		}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.