我将整个分页脚本放入函数中,以便我可以多次使用它。代码很长,但只有一部分我遇到了麻烦。
在我调用函数paginate($connection, "categories");
后,我使用
$sql = "SELECT * FROM categories ORDER BY cat_name LIMIT $start, $limit";
etc..
我得到了这些错误。
注意:未定义的变量:start 注意:未定义的变量:限制你 您的SQL语法有错误; 检查对应的手册 您的MySQL服务器版本 在'第1行'附近使用正确的语法
它没有识别我在查询上面调用的函数中的变量。我从来没有把这么多代码放在函数中,所以我不确定我该做什么。我尝试返回那些未定义的变量(在函数中)但是没有用。我也试图避免使用全局变量 - 我听说它们已被弃用了。
以下是实际功能
function paginate($connection, $tableName) {
//Pagination
$targetpage = "http://localhost/website/all_categories.php";
$limit = 4; //sets how many rows to display on each page
//count rows
$sql = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = $connection->query($sql) or die(mysqli_error($connection));
$row = $total_pages->fetch_assoc();
$total_pages = $row['num'];
//if there's no page number, set it to the first page
$stages = 3;
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$start = empty($page) ? $start = 0 : $start = ($page - 1) * $limit;
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
echo $total_pages.' Results';
// pagination
echo $paginate;
}//end function
答案 0 :(得分:1)
(注意 - 这不一定是问题,但是......)
$start = empty($page) ? $start = 0 : $start = ($page - 1) * $limit;
应该是
$start = empty($page) ? 0 : ($page - 1) * $limit;
答案 1 :(得分:0)
编辑:您显然在功能中定义了$start
。如果它在您的SQL语句之前定义,那么它显然是可访问的。但是,如果你没有传递它$start
或$end
,我认为你的分页功能将非常有用。您还可以使用以下语法定义默认值:
function paginate($connection, $tableName, $start=0, $end="ALL") {
原始跟随
功能有自己的范围;你应该传递函数在它之外定义的变量:
function paginate($connection, $tableName, $start, $end) {
唯一的例外是超级全球$GLOBALS
,$_GET
,$_POST
,$_COOKIE
,$_REQUEST
,$_SESSION
,$_FILES
,$_ENV
和$_SERVER
。
您还可以通过超全局数组$GLOBALS
访问任何函数外部定义的函数,或者在函数实现的开头添加global $var;
。但是,除了(可以说)应用程序作用域配置指令之外,您不鼓励这样做。
答案 2 :(得分:0)
您在函数内部设置了$start
和$limit
,但您没有返回这些变量。函数内部的变量有自己的本地scope,因此无法从函数外部访问。
您需要在功能之外需要return the variables。由于您需要多个变量,因此可以在关联数组中返回这些变量。例如,如果将其作为函数的最后一行放置:
return array( 'start' => $start, 'limit' => $limit );
...你可以这样调用这个函数:
$result = paginate($connection, "categories");
...并使用如下结果:
$sql = 'SELECT * FROM categories ORDER BY cat_name LIMIT ' .
$result['start'] . ', ' . $result['limit'];