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] => enquiries@digitrol.com
    [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
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;
}
Link to comment
Share on other sites

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
Share on other sites

[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
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.