Hey,
How would i go about creating a news script that will loop efficiently. This is my script so far.
PHP Code:
<?php
class news{
var $posts;
var $postid;
var $post;
var $currentpost;
function checkPosts(){
global $db, $config;
$news = mysql_query("SELECT Id FROM `".$db[prefix]."news` ORDER BY Id DESC LIMIT $config[News]");
$this->posts = mysql_num_rows($news);
$this->postid = 1;
}
function getPosts(){
global $db, $config;
$news = mysql_query("SELECT * FROM `".$db[prefix]."news` ORDER BY Id DESC LIMIT $config[News]");
$i=1;
while($news_rows = mysql_fetch_assoc($news)){
$this->post[$i] = $news_rows;
$i++;
}
}
function selectPost($id){
$this->currentpost = $this->post[$id];
}
function postTitle(){
return $this->currentpost['Name'];
}
function postAuthor(){
return $this->currentpost['Username'] . " ";
}
function postDate(){
$hour = $this->currentpost['Hour'];
$date = formatDate($this->currentpost['Day'], $this->currentpost['Month'], $this->currentpost['Year'], $hour);
$date .= " at ";
$date .= formatTime($hour, $this->currentpost['Minute']);
return $date;
}
function postContent(){
$conten = $this->currentpost['Content'];
$conten = stripslashes($conten);
return $conten;
}
function postCommentslink(){
$newsid = $this->currentpost['Id'];
$data2 = $db[prefix] . "news_comments";
$comments = mysql_num_rows(mysql_query("SELECT Id FROM $data2 WHERE Newsid = '".$this->currentpost[Id]."'"));
$coms = "<a href=\"?p=comments&id=".$this->currentpost['Id']."\">Comments (";
$coms .= $comments . ")</a>";
if($_SESSION['logged'] == true){
$coms .= " | <a href=\"?p=addcomment&id=".$this->currentpost['Id']."\">Add Comment</a>";
}
}
function havePosts(){
if($this->posts >= $this->$postid){
$this->selectPost($this->postid);
$this->postid++;
return true;
} else{
return false;
}
}
}
$news = new news();
$news->checkPosts();
$news->getPosts();
$pagetitle = "Latest News";
?>
And in my news page:
PHP Code:
<h3>Latest News</h3>
<div id="contentwrap">
<?php
while($news->havePosts()){
echo $news->postContent();
}
?>
</div>
This unfortionatly repeats indefinatly, where am i going wrong?