patrickm Posted July 15, 2008 Share Posted July 15, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/114807-solved-how-to-combine-a-php-template-with-mysql-database/ Share on other sites More sharing options...
tommyboy123x Posted July 15, 2008 Share Posted July 15, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/114807-solved-how-to-combine-a-php-template-with-mysql-database/#findComment-590314 Share on other sites More sharing options...
patrickm Posted July 15, 2008 Author Share Posted July 15, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/114807-solved-how-to-combine-a-php-template-with-mysql-database/#findComment-590344 Share on other sites More sharing options...
patrickm Posted July 16, 2008 Author Share Posted July 16, 2008 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; } Quote Link to comment https://forums.phpfreaks.com/topic/114807-solved-how-to-combine-a-php-template-with-mysql-database/#findComment-591347 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.