Page 1 of 1

News System

Posted: Thu Mar 08, 2007 7:38 am
by aspekt9
I have a function that retrieves news from my database to output it. I use a small template system that works pretty well and I'm having problems displaying the news in it. Here's my news function:

Code: Select all

function displayNews($all = 0) {
    global $db, $max_items, $index;
    
    /* query for news items */
    if ($all == 0) {
        $query = ("SELECT title,author,topic,content,DATE_FORMAT(date, '%Y-%m-%d') as date FROM news ORDER BY date DESC");
    } else {
        $query = ("SELECT title,author,topic,content,DATE_FORMAT(date, '%Y-%m-%d') as date FROM news ORDER BY date DESC");
    }
    $result = mysql_query($query)or die (mysql_error());
    while ($row = mysql_fetch_array($result)) {

        $date = $row['date'];
		$author = $row['author'];
		$topic = $row['topic'];
        $title = htmlentities ($row['title']);
        $news = nl2br (strip_tags ($row['content']));
		$display .= "<h1>$title</h1>";
		$display .= "<h2>$date</h2>";
		$display .= "<h2>By: $author</h2>";
		$display .= "<h3>Topic: $topic</h3>";
		$display .= "$news";
		$display .= "<h4></h4>";

		$index->replace_tags(array(
			"errormsg" => "$msg",
			"news" => "$display",
			));
		}
}
displayNews();

Code: Select all

$index->replace_tags(array(
			"errormsg" => "$msg",
			"news" => "$display",
			));
Replaces {news} in the template file with $display. The problem is, it only displays the first row in the table. If I echo every line instead of $display and just comment out the whole $index->replace_tags block, it displays each row as it should it's just not in the template how I want. How can I get it to output every row to my template?

Posted: Thu Mar 08, 2007 8:37 am
by feyd
The template should be told what to replace after the loop has completed.

Posted: Thu Mar 08, 2007 9:35 am
by aspekt9
Works great now, thank you

Posted: Thu Mar 08, 2007 2:41 pm
by RobertGonzalez
For clarity (and maybe performance), you may want to change this...

Code: Select all

<?php
$index->replace_tags(array(
        "errormsg" => "$msg",
        "news" => "$display",
));
?>

Code: Select all

<?php
$index->replace_tags(array(
        "errormsg" => $msg,
        "news" => $display,
));
?>