2

我是新来的,在 PHP 中也是新的..

只是想知道如何像在 Wordpress 中一样制作我自己的灵活循环...注意我不是在谈论 wordpress..我想在我自己的 PHP 应用程序上实现它...

让我们回顾一下WP,有一段代码是这样的:

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

echo the_title();
echo the_content();

endwhile; // this is just an ilustration

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

谢谢..

4

4 回答 4

8

WordPress 使用这些函数在遍历循环时修改的全局变量。例如:

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 样式编码而不是 WordPress 所做的事情。这将使变量在对象的实例中定义,而不是全局可访问的。例如:

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];
    }
}
于 2009-10-04T20:14:16.313 回答
3

您可以实现Iterator接口。

于 2009-10-04T11:27:38.053 回答
1

这个想法是使用全局范围变量,然后函数将修改这些变量:

// first define the global variables:
$post_index = 0;
$posts_count = 0;
$post = null;
$posts = null;
$connection = null;

$connection = new PDO('mysql:host=127.0.0.1;dbname=YOUR_DB', 'YOUR_DB_USERNAME', 'YOUR_DB_USER_PASSWORD', array(PDO::ATTR_FETCH_MODE => PDO::FETCH_ASSOC));

$query = $connection->query('SELECT * FROM posts');
$posts = $query->fetchAll();

$posts_count = count($posts); // or = $query->rowCount()

// now you create the functions that will deal with and change those variables

function have_posts()
{
    global $post_index, $posts_count, $posts, $post;
    // if posts found and if post_index < posts_count return true else return false
    if($posts && $post_index < $post_count)
        return true;

    else
        return false;
}

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

    if($post_index > $post_count)
        return false;
    // fetch the post of the current index
    $post = $posts[$post_index];

    // increment the $post_index so that the next call to this function grapes the next post
    $post_index++;
}

function get_ID()
{
    global $post;
    return $post['ID'];
}


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

我希望有帮助。

于 2015-04-16T01:42:24.610 回答
0

再次解决这个问题:D

最后我得到了解决方案,

@Matt Huggins,我对您的代码进行了一些修改,现在可以使用了...

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

非常感谢... :)

于 2010-02-26T20:22:44.080 回答