Jump to content

Replacing Template Values **SOLVED**


scottybwoy

Recommended Posts

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

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;
}
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 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.