如何使自己的while循环就像字preSS循环? [英] How to make my own while Loop just like Wordpress Loop?

查看:185
本文介绍了如何使自己的while循环就像字preSS循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里新新在PHP太..

im new here and new in PHP too..

只是想知道如何让自己灵活的循环,就像在Word preSS ...
注意我不是在谈论字preSS ..我想实现它myown PHP应用程序...

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...

让我们来看看早在WP,还有一个code是这样的:

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

你能弄清楚如何have_post()或the_post()与数据库进行交互,
以便它们可以是环..

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

感谢..

推荐答案

字preSS使用全局变量,这些功能通过迭代循环时修改。例如:

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

我肯定会推荐使用OOP风格编码在什么字preSS做,但是。这将保持一个对象的实例中被全局访问,而不是definied变量。例如:

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

这篇关于如何使自己的while循环就像字preSS循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆