Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-05-2012, 01:58 AM   PM User | #1
midnite
New Coder

 
Join Date: Jun 2011
Posts: 18
Thanks: 2
Thanked 0 Times in 0 Posts
midnite is an unknown quantity at this point
how to output query to html template place-holders??

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 info retrieved on the $values array i then replace another place-holders stored in $keys by the $values to the template 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:

page2.php
PHP Code:
<?php

require_once 'includes/config.inc.php';

require_once 
'includes/functions.inc.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
    book.title,
    book.isbn,
    book.published,
    book.price,
    GROUP_CONCAT(CONCAT(firstname,' ',lastname) ORDER BY lastname ASC SEPARATOR ', ') AS authors
FROM
    ( book JOIN book_author ON (book.id = book_author.book) )
JOIN
    author ON (book_author.author = author.id)
GROUP BY
    book.title
ORDER BY
    book.title;
"
;

$result mysqli_query($link,$sql);

if (
$result === false) {
    exit(
'Sorry, there has been an error. Please try again later.');
}

// Check if the query returned anything
if (mysqli_num_rows($result) == 0) {
    
$output .= '<p class="alert">Sorry, we have no books to display.</p>';
} else {
    
    
$tpl file_get_contents('templates/page.tpl.html');
 

    
$values[] = '<h2>[+title+]</h2>';
    
$values[] = '<p><strong>ISBN:</strong>[+isbn+]</p>';
    
$values[] = '<p><strong>Published:</strong> '.date('j F Y',strtotime('[+published+]')).'</p>';
    
$values[] = '<p><strong>Price:</strong> '.toGBP('[+price+]').'</p>';
    
$values[] = '<p><strong>Authors:</strong> '.htmlentities(toList('[+authors+]')).'</p>';

    
$keys[] = '[+title+]';
    
$keys[] = '[+heading+]';
    
$keys[] = '[+content+]';

    return 
$out;

    while (
$row mysqli_fetch_assoc($result)) {

        
$output .= str_replace(
            array(
'[+title+]''[+isbn+]''[+published+]''[+price+]''[+authors+]'),
            array(
$row['title'], $row['isbn'], $row['published'], $row['price'], $row['authors'], $values);

    }
}

$output1 str_replace($keys$values$tpl);

mysqli_free_result($result);

mysqli_close($link);

echo 
$output1;

?>
functions.in.php

PHP Code:
<?php

function toList($data) {

    
$last count($data)-1;
    
$list  '';

    for (
$i=0$i <= $last$i++) {
        
$list .= $data[$i];
        if (
$i == ($last-1)) {
            
$list .= ' and ';
        } elseif (
$i $last) {
            
$list .= ', ';
        }
    }

    return 
$list;

}

function 
toGBP($money) {

    
$money round($money,2); // round to 2 decimal places.
    
$formatted sprintf('%01.2f',$money); // Ensure 2 decimal places are always written

    
return '&pound;'.$formatted;

}
?>
and page.tpl.html

PHP Code:
<html>
    <
head>
        <
title>[+title+]</title>
    </
head>
    <
body>
        <
h1>[+heading+]</h1>
        [+
content+]
    </
body>
</
html
Thank you in advance for your help guys.
midnite is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:28 AM.


Advertisement
Log in to turn off these ads.