midnit Posted June 5, 2012 Share Posted June 5, 2012 Hi there guys, I'm trying to output a database query to an html template using php, basically i've got a while loop that replaces the template place-holders with the database values retrieved on the $values i then use the variable $out to replace the place-holders in the template by the values stored in $values but its not working and i cant seem to understand what am i doing wrong hopefully a pair of fresh eyes will. hopefully someone can help understand a bit better how to do this correctly, here is the code that i have: authors.php <?php require_once 'file_includes/config.php'; $link = mysqli_connect( $config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']); if (mysqli_connect_errno()) { exit('Sorry, there has been an error. Please try again later.'); } $sql = 'SELECT firstname, lastname FROM author'; $result = mysqli_query($link, $sql); /* check query */ if($result === false) { exit(mysqli_error($link)); } /* get the results */ $tlp = file_get_contents('test.html'); $values = '[+name+]'; $out = str_replace('[+name+]', $values, $tlp); while ($row = mysqli_fetch_assoc($result)) { $output .= str_replace( array('[+name+]'), array($row['firstname'], $values); } mysqli_free_result($result); mysqli_close($link); echo $out; ?> and the template test.html <html> <head> </head> <body> <p>[+name+]</p> </body> </html> I just want to get the while loop values from the query to replace the template place-holders, can anyone help me trying to understand what am i doing wrong Quote Link to comment https://forums.phpfreaks.com/topic/263698-how-to-output-query-to-html-template-place-holders/ Share on other sites More sharing options...
trq Posted June 5, 2012 Share Posted June 5, 2012 Is there a particular reason you think this is a better idea than using php itself? eg; <p><?php echo $name; ?></p> Quote Link to comment https://forums.phpfreaks.com/topic/263698-how-to-output-query-to-html-template-place-holders/#findComment-1351366 Share on other sites More sharing options...
midnit Posted June 5, 2012 Author Share Posted June 5, 2012 Is there a particular reason you think this is a better idea than using php itself? eg; <p><?php echo $name; ?></p> I know what you mean, but this is one of the exercises of my uni (not assignment) and we were told to do it this way : (, i know how to do it without the while loop eg: $title = 'Template example'; $heading = 'Template Heading'; $content = '<p>Hello World!</p>'; $out = str_replace('[+title+]', $title, $tlp); $out = str_replace('[+heading+]', $heading, $output); $out = str_replace('[+content+]', $content, $output); echo $out; but for some reason i cant get the same result with the while loop..any ideas, please Quote Link to comment https://forums.phpfreaks.com/topic/263698-how-to-output-query-to-html-template-place-holders/#findComment-1351372 Share on other sites More sharing options...
trq Posted June 5, 2012 Share Posted June 5, 2012 All you would need to do is loop through your data and store the results. while ($row = mysqli_fetch_assoc($result)) { $output[] = $row['firstname']; } Then, use that to do your replace. $out = str_replace('[+name+]', implode(' ', $output) , $tlp); Of course, this will mean that all names within be within the single set of <p></p> tags though. Quote Link to comment https://forums.phpfreaks.com/topic/263698-how-to-output-query-to-html-template-place-holders/#findComment-1351378 Share on other sites More sharing options...
midnit Posted June 5, 2012 Author Share Posted June 5, 2012 hmmm that's sort of what i'm looking for : | not quite, since it stores all the names in the same line...nevertheless this is what i came up with: $tpl = file_get_contents('test.html'); while ($row = mysqli_fetch_assoc($result)) { $title = $row['title']; // stores the title in the variable $title $fname = $row['firstname']; // stores the firstname value in the variable $fname $sname = $row['lastname']; // stores the lastname value in the variable $sname $output = ($title , $fname, $sname); //stores the 3variables in $output } $keys[] = '[+title+]'; $keys[] = '[+name+]'; $keys[] = '[+sname+]'; $out = str_replace($keys, implode(' ', $output), $tpl); echo $out; test.php <html> <head> </head> <body> <h2>[+title+]</h2> <p>[+name+]</p> <p>[+sname+]</p> </body> </html> and i still can't get it to work and the worst is, im reading the code and it makes sense what im doing or how i am reading so its really frustrating that its not working. Quote Link to comment https://forums.phpfreaks.com/topic/263698-how-to-output-query-to-html-template-place-holders/#findComment-1351427 Share on other sites More sharing options...
trq Posted June 5, 2012 Share Posted June 5, 2012 It makes little sense. I suggest you ask your teacher considering this is for a uni project. Quote Link to comment https://forums.phpfreaks.com/topic/263698-how-to-output-query-to-html-template-place-holders/#findComment-1351512 Share on other sites More sharing options...
midnit Posted June 6, 2012 Author Share Posted June 6, 2012 Hi Thorpe, Sorry for my late reply just been very busy with all the exams lately, anyway just to let you know yesterday while i was playing around with PHP i managed to get the results i was looking for here is the while loop code: $tpl = file_get_contents('templates/test.html'); // gets the contents of test.html while ($row = mysqli_fetch_assoc($result)) { $output .= str_replace( array('[+title+]', '[+name+]', '[+sname+]'), // replace this array($row['title'], $row['firstname'], $row['lastname']), // for this $tpl); // inside the $tpl } echo $output; // and i was doing this: // while ($row = mysqli_fetch_assoc($result)) //{ // $output .= str_replace( // array('[+title+]', '[+name+]', '[+sname+]'), // replace this // array($row['title'], $row['firstname'], $row['lastname'], // for this // $tpl)); // inside the $tpl, did not work because $tpl is inside the array!!!! //} Anyway i just wanted to say thank you for your idea, its always good to see solutions from another perspective . Regards, Pedro Quote Link to comment https://forums.phpfreaks.com/topic/263698-how-to-output-query-to-html-template-place-holders/#findComment-1351738 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.