Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php my sql分页帮助需要链接和推进下一个偏移量_Php_Mysql_Pagination - Fatal编程技术网

php my sql分页帮助需要链接和推进下一个偏移量

php my sql分页帮助需要链接和推进下一个偏移量,php,mysql,pagination,Php,Mysql,Pagination,您好,我正在尝试对一个php mysql数据库进行分页,但我不知道是什么使结果移动到下一页,它被卡在记录行0处,我要做什么更改才能使其移动到下一组结果?这是连接到nav链接的偏移量吗?它是否需要调用mysql查询?这是怎么回事 //pagination //find out how many rows are in the table $sql = "SELECT * FROM hotels_database"; $result = mysql_query($

您好,我正在尝试对一个php mysql数据库进行分页,但我不知道是什么使结果移动到下一页,它被卡在记录行0处,我要做什么更改才能使其移动到下一组结果?这是连接到nav链接的偏移量吗?它是否需要调用mysql查询?这是怎么回事

    //pagination
    //find out how many rows are in the table 

    $sql = "SELECT * FROM hotels_database";
    $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);
    $r = mysql_fetch_row($result);
    $numrows = $r[0];


    // number of rows to show per page
    $rowsperpage = 2;
    // find out total pages
    $totalpages = ceil($numrows / $rowsperpage);

    // get the current page or set a default
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
    // cast var as int
    $currentpage = preg_replace('#[^0-9]#i','', $_GET['currentpage']);
    } else {
    // default page num
    $currentpage = 1;
    } // end if



    // if current page is greater than total pages...
    if ($currentpage > $totalpages) {
    // set current page to last page
    $currentpage = $totalpages;
    } // end if
    // if current page is less than first page...
    if ($currentpage < 1) {
    // set current page to first page
    $currentpage = 1;
    } // end if

    // the offset of the list, based on current page 
    $offset = ($currentpage - 1) * $rowsperpage;

    // get the info from the db 
    $sql = "SELECT * FROM hotels_database LIMIT $offset, $rowsperpage";
    $result = mysql_query($sql, $con) or trigger_error("SQL", E_USER_ERROR);

    // while there are rows to be fetched...
    while ($list = mysql_fetch_assoc($result)) {
    // echo data


    for ($stars=1;$stars<=floor($list['stars']);$stars++){
    $star_string .= "<img src='images/star.jpg'/>";

    }

    if (($list['stars']-floor($list['stars']))> 0){
    $star_string .= "<img src='images/half_star.jpg'/>";
    }

    $hotels_database .= "<div style='display:table-row;height:70px;'><div style='width:60px;height:60px;display:table-cell;float:left;'><img src='".$list['pic']."' width='60px'/></div>"."<div style='width:280px;display:table-cell;vertical-align:top;text-align:left;padding-left:5px;font-size:12px;'><a href='#' class='database_link'>".$list['hotel']."</a>".$star_string."<br/><span style='font-size:10px;'>".$list['addy']."</span><br/>Guest score: <span style='color:red;font-size:14px;'>".$list['score']."</span> out of 10"."<br/>"."<li class='database_arrow'>Display Amenities & More Info</li>"."</div>"."<div style='width:78px;height:60px;display:table-cell;vertical-align:top;'><span style='font-family:arial;color:green;font-size:15px;'>$".$list['price']."</span><br/>per night<div style='border-radius:10px;background-color:#1284d3;height:25px;width:70px;left:10px;top:10px;position:relative;border:1px black solid;color:white;font-family:arial;line-height:8px;'><br/>select</div><br/></div>"."</div><hr style='color:#c8ff78;'/>";

    $star_string="";
    } // end while

    /******  build the pagination links ******/
    // range of num links to show



    //pagination
    /*     

是的,所以我不知道如何转到下一个偏移量在这种情况下我使用什么href?为什么当前页面查询字符串没有显示在我的地址栏中?请看一看,

,因为D.N说代码会有帮助,但一般来说它是有用的

ifisset$\u获取['offset']{

$offset=$_get['offset']

}

否则{

$offset=0

}

然后在SQL调用中添加:


'无论什么调用都是'limit$limit offset$offset

您应该显示一些PHP代码,但本质上,您需要将起始值页或记录传递回PHP。您可能需要查看链接的偏移量是否正确。可能发生的情况是,for循环在吐出链接时没有推进偏移量,所以他们都说?offset=0不是link1?offset=0,link2?offset=1等等。这不是MySQL的首选方法,而是限制$offset,$number\u行数\u到\u fetch。我并没有试图获得该过程的完整描述或如何保护它。这样做会更好,但这是最常见的,我猜他刚刚开始的时候,我猜他正在努力。但是,是的,这不是首选方法。我刚刚阅读了一些关于SQL的文献,似乎SQL:2008是最新的SQL标准,您的版本是首选方法。