Jump to content

how to output query to html template place-holders??


midnit

Recommended Posts

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :D 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 :D.

 

Regards,

Pedro

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.