0

我有显示帖子的主(用户可见)文件,我需要设置分页。

如果我在同一个文件中获取数据库会很容易(但我想避免这种情况),这就是为什么我创建了一个单独的(用户隐藏的)文件,其中包含然后从主文件(blog.php)调用的类':

BLOG.php(简体):

<?php
require 'core.php';

$posts_b = new Posts_b();
$posts_bx = $posts_b->fetchPosts_b();

foreach($posts_hx as $posts_hy){

   echo $posts_hy['title'];
}
?>

core.php(简化);

class Posts_b extends Core {
public function fetchPosts_b(){

    $this->query ("SELECT posts_id, title FROM posts"); 

//return
return $this->rows();

    }
}

这就像一个魅力,但现在我需要在查询中进行计数,它工作正常,并且给了我一个变量 $pages=5(在类 posts_b 中处理 - 在文件 core.php 中),

core.php(简化-带变量);

class Posts_b extends Core {
public function fetchPosts_b(){

    $this->query ("SELECT posts_id, title FROM posts"); 
    $pages=5;   

//return
return $this->rows();

    }
}

现在我需要一种将这个变量值返回给 blog.php 的方法(我返回 rows() 的方式)

请帮助,任何人,

谢谢...

4

1 回答 1

1

一个函数只能有一个返回值。

不过有一些方法可以解决这个问题。你可以让你的返回值是一个包含所有你想要的值的数组。例如:

return array("pages"=>$pages, "rows"=>$this->rows());

然后在你的代码中

require 'core.php';

$posts_b = new Posts_b();
$posts_bx = $posts_b->fetchPosts_b();
$pages = $posts_bx["pages"];
foreach($posts_hx["rows"] as $posts_hy){

   echo $posts_hy['title'];
}
?>

或者您可以调整作为参考提供的输入参数

public function fetchPosts_b(&$numRows){

  $this->query ("SELECT posts_id, title FROM posts"); 

  //return
  return $this->rows();

}

在您的代码中

require 'core.php';

$posts_b = new Posts_b();
$pages = 0;
$posts_bx = $posts_b->fetchPosts_b(&$pages);

foreach($posts_hx["rows"] as $posts_hy){

   echo $posts_hy['title'];
}
?>

或者您可以选择在 fetchPosts_b 方法之外找出您的分页。

$posts_bx = $posts_b->fetchPosts_b();
$pages = floor(count($posts_bx)/50);
于 2013-08-02T19:56:25.430 回答