views:

431

answers:

5

Hi all programmer... im new here and new in PHP too..

Just wondering how to make my own flexible loop just like in Wordpress... Note im not talking about wordpress.. I want to implement it on myown PHP application...

let's look back in WP, there is a code something like this:

while (have_post() : thepost())// .. bla bla...

echo the_title();
echo the_content();

endwhile; // this is just an ilustration

Could you figure out how have_post() or the_post() interact with database, so that they can be loop..

thanks..

+1  A: 

You could implement the Iterator interface.

Zed
Replace “need to” with “could”.
Gumbo
Consider it done.
Zed
+3  A: 

WordPress uses global variables that these functions modify when iterating through the loop. e.g.:

var $posts = null;
var $post = null;
var $post_count = 0;
var $post_index = 0;

function have_post() {
    global $posts, $post_count, $post_index;

    $post_index = 0;

    // do a database call to retrieve the posts.
    $posts = mysql_query('select * from posts where ...');

    if ($posts) {
        $post_count = count($posts);
        return true;
    } else {
        $post_count = 0;
        return false;
    }
}

function thepost() {
    global $posts, $post, $post_count, $post_index;

    // make sure all the posts haven't already been looped through
    if ($post_index > $post_count) {
        return false;
    }

    // retrieve the post data for the current index
    $post = $posts[$post_index];

    // increment the index for the next time this method is called
    $post_index++;

    return $post;
}

function the_title() {
    global $post;
    return $post['title'];
}

function the_content() {
    global $post;
    return $post['content'];
}

I would definitely recommend using OOP style coding over what WordPress does, however. This will keep variables definied within an instance of an object instead of being globally accessible. e.g.:

class Post {
    function __construct($title, $content) {
        $this->title = $title;
        $this->content = $content;
    }

    function getTitle() {
        return $title;
    }

    function getContent() {
        return $content;
    }
}

class Posts {
    var $postCount = 0;
    var $posts = null;

    function __construct($conditions) {
        $rs = mysql_query('select * from posts where $conditions...');

        if ($rs) {
            $this->postCount = count($rs);
            $this->posts = array();

            foreach ($rs as $row) {
                $this->posts[] = new Post($row['title'], $row['content']);
            }
        }
    }

    function getPostCount() {
        return $this->postCount;
    }

    function getPost($index) {
        return $this->posts[$index];
    }
}
Matt Huggins
A: 

Thanks Matt Huggins for your answer.. i hve little understand with it.. and i'll try soon..

and for Zed, i say thankyou too.. next time i will learn about Iterator, that sounds new to me :)

Takien
A: 

I'm back.. I'm sorry but I still confuse with your code Matt, because my try is not work as expected... could you gimme more explanation or example maybe.. thanks.

takien
Which method did you try -- iterating or class-based? What is the problem you encountered?
Matt Huggins
+1  A: 

Go to this problem again :D

finally I got the solution,

@Matt Huggins, i have made some revision to your code, and now it's work...

$posts = $wpdb->get_results('SELECT * FROM my_table',OBJECT);
$post = null;
$post_count = 0;
$post_index = 0;

function have_post() {
    global $posts, $post_count, $post_index;

    if ($posts && ($post_index <= $post_count)){
        $post_count = count($posts);
        return true;
    }
    else {
        $post_count = 0;
        return false;
    }
}

function the_post() {
    global $posts, $post, $post_count, $post_index;

    // make sure all the posts haven't already been looped through
    if ($post_index > $post_count) {
        return false;
    }

    // retrieve the post data for the current index
    $post = $posts[$post_index+1];

    // increment the index for the next time this method is called
    $post_index++;
    return $post;

}

function the_title() {
    global $post;
    return $post->Title;
}

function the_content() {
    global $post;
    return $post->Content;
}

//and the output

if(have_post()) : while(have_post()) : the_post();
echo '<h2>'.the_title().'</h2>';
echo '<p>'.the_content().'</p>';
endwhile; endif;

Many thanks... :)

takien