News System

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

News System

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The template should be told what to replace after the loop has completed.
aspekt9
Forum Commoner
Posts: 43
Joined: Wed Dec 06, 2006 5:03 pm

Post by aspekt9 »

Works great now, thank you
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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,
));
?>
Post Reply