ShoeLace1291 Posted April 23, 2009 Share Posted April 23, 2009 I've been having this problem with my PHP template variables script, not only with this project, but for other projects as well. I have HTML in TPL files with variables from an array such as SITE_NAME. The problem is that only the last item in the array works. For example, say my array is the following: array('SITE_NAME' => "MySite.com",SITE_MOTTO => "We are freakin awesome!"); Only the site motto will work. Here is my current script: <?php if($_POST['submit']){ $book = array( 'FIRST_NAME' => $_POST['authfirst'], 'LAST_NAME' => $_POST['authlast'], 'MI' => $_POST['authmi'], 'TITLE' => $_POST['booktitle'], 'PUBLISHER' => $_POST['publisher'], 'PULICATION_LOCATION' => $_POST['publocation'], 'VOLUME' => $_POST['volume'], 'DATE' => $_POST['date'] ); $file = file_get_contents("templates/displays/MLA_book.tpl"); foreach($book as $key => $value){ $display = str_replace($key, $value, $file); } echo $display; } else { $vars = array( 'ACTION' => $_SERVER['PHP_SELF'] ); $file = file_get_contents("templates/forms/MLA_book.tpl"); foreach($vars as $key => $value){ $display = str_replace($key, $value, $file); } echo $display; } ?> This is "templates/display/MLA_book.tpl": <div style='width: 300px;'> LAST_NAME. <u>TITLE</u>. PUBLICATION_LOCATION: PUBLISHER, DATE </div> The only thing that works is DATE, and everything else displays as is. Thanks for any help. Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 The problem is in your str_replace function. You are always replacing the contents of "file" and storing it in "display." Each time you make a replacement, you store the result in display, and move on to the next one. Sounds good, right? Except, on the next replacement, you open the original file contents again and store the result in display again. Consider this code: <?php if($_POST['submit']){ $book = array( 'FIRST_NAME' => $_POST['authfirst'], 'LAST_NAME' => $_POST['authlast'], 'MI' => $_POST['authmi'], 'TITLE' => $_POST['booktitle'], 'PUBLISHER' => $_POST['publisher'], 'PULICATION_LOCATION' => $_POST['publocation'], 'VOLUME' => $_POST['volume'], 'DATE' => $_POST['date'] ); $file = file_get_contents("templates/displays/MLA_book.tpl"); foreach($book as $key => $value){ $display = str_replace($key, $value, $file); } echo $display; } else { $vars = array( 'ACTION' => $_SERVER['PHP_SELF'] ); $file = file_get_contents("templates/forms/MLA_book.tpl"); foreach($vars as $key => $value){ $file = str_replace($key, $value, $file); } echo $file; } ?> Quote Link to comment Share on other sites More sharing options...
ShoeLace1291 Posted April 23, 2009 Author Share Posted April 23, 2009 I see no difference in the code, but I copied and pasted it any way. Still not working. Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 Ah. I didn't notice you had to references to the same code, and I only changed one. Basically, you should change: $display = str_replace($key, $value, $file); to: $file = str_replace($key, $value, $file); The reason is because you want to process a template variable, save the replaced result in the variable "file", then use that replaced result while you search for the next template key, and so on. Quote Link to comment Share on other sites More sharing options...
ShoeLace1291 Posted April 23, 2009 Author Share Posted April 23, 2009 Ahhhh alright, thanks. Seems so obvious now... lol. Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 No problem. If you could though, please mark the post as solved so people know your issue is resolved! Thanks. Quote Link to comment 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.