Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1571 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

PHP OOP News Script

Thread title: PHP OOP News Script
Closed Thread    
    Thread tools Search this thread Display Modes  
02-07-2007, 04:06 PM
#1
Xi0s is offline Xi0s
Status: Sin Binner
Join date: Dec 2006
Location: Huddersfield, UK
Expertise:
Software:
 
Posts: 384
iTrader: 3 / 83%
 

Xi0s is on a distinguished road

Send a message via MSN to Xi0s

  Old  PHP OOP News Script

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?

02-07-2007, 04:48 PM
#2
prasunsen is offline prasunsen
Status: Junior Member
Join date: Dec 2006
Location:
Expertise:
Software:
 
Posts: 52
iTrader: 0 / 0%
 

prasunsen is on a distinguished road

Send a message via ICQ to prasunsen

  Old

Most probably havePosts() always returns true. Go back thru the variable and check if all are as expected. Use die(), print_r, var_dump or echo if you don't use some debugger

02-08-2007, 01:36 AM
#3
creativejen is offline creativejen
Status: Paladin
Join date: Jul 2006
Location: Sheffield, UK
Expertise: design, front-end markup
Software: Photoshop
 
Posts: 2,353
iTrader: 25 / 96%
 

creativejen is an unknown quantity at this point

Send a message via MSN to creativejen

  Old

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;
}

You may find your error in this. I'm also guessing that $config[news] is preset to a specific number somewhere. I cant really help anymore without seeing the rest of the code.

02-08-2007, 03:29 PM
#4
localhost is offline localhost
localhost's Avatar
Status: Dediport Hosting
Join date: Jul 2006
Location: Berkshire
Expertise: programming, business
Software: Dreamweaver
 
Posts: 1,316
iTrader: 17 / 100%
 

localhost is on a distinguished road

  Old

Maybe ask wildhoney he is bound to know. Are you a advanced OOP php coder?
Anyway, Im not sure what you asking here.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed