scottybwoy Posted November 24, 2006 Share Posted November 24, 2006 Hello buddies,I'm trying to insert data into a template file I have, if the content is available. At the moment the template is loaded into the right place however the fields are not inserted :( here's my replacement code :[code]<?php function insertCompInfo() { global $recordset_array, $template; $this->LoadTemplate(DISPLAY); foreach ($recordset_array as $key => $value) { str_replace("<!-- " . $key . " -->", $value, $template); } return $template; }?>[/code]This is allot simpler than the tutorial I was using but seems like it should do the job, however it doesn't.Here is also a screen print of a test array ($recordset_array) :Array( [custId] => DIG001 [company] => DIGITROL LTD [addLine1] => Test [addLine2] => Test [townCity] => Test [county] => Test [country] => UK [postcode] => WA1 3SS [type] => 1 [pay] => 1 [tel] => 1234567890 [fax] => 1234567890 [email] => [email protected] [web] => www.digitrol.com)And here is how I have my template replacement entries :[code]<?php<!-- START_CUST --><table width="570" class="form"> <tr> <td width="160" class="labelL">Company Name</td> <td width="200"><input type="text" name="company" value="" size="40" style="input"><!-- [company] --></td> <td width="160" class="labelR">Customer Id</td> <td width="50"><input type="text" name="custId" value="" size="15" style="input"><!-- [custId] --></td> </tr></table>...etc<!-- END_CUST -->[/code]So as you can see I am using an associative array, and using the fields in my SQL database in the template for easy transfer. But all I have is just the template with no fields filled in. Thanks in advance ;) Link to comment https://forums.phpfreaks.com/topic/28333-replacing-template-values-solved/ Share on other sites More sharing options...
mansuang Posted November 24, 2006 Share Posted November 24, 2006 Try to put "[", "]" in your code.Because your template have "[" and "]": function insertCompInfo() { global $recordset_array, $template; $this->LoadTemplate(DISPLAY); foreach ($recordset_array as $key => $value) { str_replace("<!-- [b][color=red][[/color][/b]" . $key . "[b][color=red]][/color][/b] -->", $value, $template); } return $template; } Link to comment https://forums.phpfreaks.com/topic/28333-replacing-template-values-solved/#findComment-129592 Share on other sites More sharing options...
scottybwoy Posted November 24, 2006 Author Share Posted November 24, 2006 Hmm yeah, it doesn't work both ways, I tried that first but when I looked at the printed keys, I thaught it might help, but thanks for clearing it up. Should I have my return template inside the foreach? or how do I keep updating it it to hold the changes for the final return $template? Link to comment https://forums.phpfreaks.com/topic/28333-replacing-template-values-solved/#findComment-129633 Share on other sites More sharing options...
mansuang Posted November 24, 2006 Share Posted November 24, 2006 [quote author=scottybwoy link=topic=116153.msg473139#msg473139 date=1164384703]Hmm yeah, it doesn't work both ways, I tried that first but when I looked at the printed keys, I thaught it might help, but thanks for clearing it up. Should I have my return template inside the foreach? or how do I keep updating it it to hold the changes for the final return $template?[/quote]- you don't have your return template inside the foreach.- To keep updating it to hold the changes for the final return $template,Try the revise one: function insertCompInfo() { global $recordset_array, $template; $this->LoadTemplate(DISPLAY); foreach ($recordset_array as $key => $value) { [b][color=red]$template =[/color][/b] str_replace("<!-- [b][color=red][[/color][/b]" . $key . "[b][color=red]][/color][/b] -->", $value, $template); } return $template; }Hope it helps Link to comment https://forums.phpfreaks.com/topic/28333-replacing-template-values-solved/#findComment-129650 Share on other sites More sharing options...
scottybwoy Posted November 28, 2006 Author Share Posted November 28, 2006 Thanks, that done the trick Link to comment https://forums.phpfreaks.com/topic/28333-replacing-template-values-solved/#findComment-131580 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.