我希望得到数组结果的分页。在数组形式中,我想逐个处理查询。在这种情况下,我该如何处理限制?我需要遍历循环并应用分页。
对于单个查询,没关系:
$sql = "SELECT * FROM listing WHERE ORG_NAME_ENGLISH='$search' ORDER BY `Sub-type` DESC LIMIT $start, $limit";
$result = mysql_query($sql);
对于数组中的记录,如何处理分页?
$total_pages=count($oidArr);
$targetpage = "paginate.php"; //your file name (the name of this file)
$limit = 7; //how many items to show per page
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
foreach($oidArr as $orgid)
{
echo $start;
echo $limit;
/* Get data. */
$sql = "SELECT * FROM `listing` WHERE `oID`='$orgid' ORDER BY `Sub-type` DESC LIMIT $start,$limit";
$result = mysql_query($sql);
}
答案 0 :(得分:1)
public function getDataPerPageByOrgId($orgId, $page, $limit)
{
// count the all the rows data that meet the requirement
$total_pages = mysql_query("SELECT count(*) from listing WHERE `oID`='$orgId' ");
// from where should we load data
$offset = ($page - 1) * $limit;
// Get data.
$sql = "SELECT * FROM `listing` WHERE `oID`='$orgid' ORDER BY `Sub-type` DESC LIMIT $offset ,$limit";
$result = mysql_query($sql);
$data=[$result, $total_pages];
return $data;
}
此方法返回一个多维数组,其中包含显示所需的所有数据。 $data[0]
包含您可以循环显示的数据,$data[1]
您可以在数据列表末尾显示为链接的页数为1-2- 3 -4-5
如果你的数据有问题。只需尝试print_r($data)
检查您的结果是否真正符合您的预期,以及如何进入您的阵列。
答案 1 :(得分:0)