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

ただし、WordPress よりも OOP スタイルのコーディングを使用することを強くお勧めします。これにより、グローバルにアクセスできる代わりに、オブジェクトのインスタンス内で定義された変数が保持されます。例えば:

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 に答える
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 に答える