I'm currently trying to make some pagination work, but without any success.
The first query I'm executing is to grab all the data needed: so first result should be on the first page, second result on the second and so on... But I'm always getting the first result. Although I copied the query into phpmyadmin to manualy set the query and there it showed me the right results.
Here's the code where everything is happening. I'm stuck why it won't work.
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$limit = 1;
$start = 0;
$query = $db->prepare("SELECT * FROM `users` LIMIT $limit OFFSET $start");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_OBJ);
$count = count($result);
$query2 = $db->prepare("SELECT * FROM `users`");
$query2->execute();
$result2 = $query2->fetchALL(PDO::FETCH_OBJ);
$count2 = count($result2);
// Pagination
$total = ceil($count2 / $limit);
if ($page > 1) {
$start = ($page - 1) * $limit;
}
if ($page != $total) {
$next_page = '<li>»</li>';
} else {
$next_page = '<li class="disabled">»</li>';
}
if ($page > 1) {
$previous_page = '<li>«</li>';
} else {
$previous_page = '<li class="disabled">«</li>';
}
What am I doing wrong?
You are doing nothing with $page. $start is always 0 in the query.
You're going to need to define how many records you want per page, then multiply that by $page -1, e.g.
$start = ($page - 1) * $records_per_page;
Related
I need some help, I want to make an next / previous page but I have some problems...
This is the PHP Code, how I try to make it. The problem is that it doesn't show 3 data on the first page and on the next page it puts the same data again an not the other one.
Code:
$statement = $connect->prepare("SELECT COUNT(*) AS anzahl FROM `accounts`");
$statement->execute();
$row = $statement->fetch();
$max_data = $row['anzahl'];
$page = 1;
if(isset($_GET['page'])) {
$page = intval($_GET['page']);
}
$max_info = 3;
$start = ($page - 1) * $max_info;
$number_page = ceil($max_data / floatval($max_info));
for($a = 1; $a <= $anzahl_seiten; $a++) {
if($page == $a){
echo " <b>$a</b> ";
}
else {
echo " <a href='acp.php?page=list_all_player&seite=$a'>$a</a> ";
}
}
I have a website that I'm building on my localhost using the MVC design pattern. I have button on click that will display all the genres in TheMovieDatabase which I got using the TheMovieDatabaseAPI. On click of a genre I get data for Page 1 with 20 results. But, I couldn't figure out how to request the results for page 2. These requests are made using PHP.
I am assuming you are taking about pagination using, I am also assuming you are using mySQL as database, I am not sure which framework you are using,
Here is a function i use, you can build around this
function get_movie($page = 1){
global $database_connection;
$start = 0; //Don't modify this
$limit = 10; //set this to 20 to display 20 movies per page
$start = ($page - 1)* $limit;
//get the movie from the table
$sql = "SELECT genre FROM movie_table LIMIT $start, $limit";
$result = mysqli_query($database_connection, $sql);
while ($row = mysqli_fetch_array($result)){
echo "<div>$row['genre']</div>";
}
$movie_rows = mysqli_num_rows (mysqli_query($database_connection ,"SELECT * FROM movie_table"));
$total = ceil($testimony_rows / $limit);
if (isset($page)){
echo "<div>";
echo "<ul class='pager'>";
if($page > 1) {
echo "<a href='?page=".($page - 1)."' style='float: left;' class=' w3-sand w3-btn w3-margin w3-round'>Previous</a>";
}
for($i = 1; $i <= $total; $i++) {
if($i == $page) { echo "<li class='active current'>".$i."</li>"; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>"; }
}
if($page != $total) {
echo "<a href='?page=".($page + 1)."' class='w3-btn w3-margin w3-sand w3-round'>Next</a>";
}
echo "</ul></div>";
}
}
you can use limit and offset to get the result for page 2.
Example :- if you are using query builder to get the results
$limit = 20;
$offset = 21;
$queryBuilder->select('d')
->from(<table name>, 'd')
->setMaxResults($limit)
->setFirstResult($offset)
->setParameter('limit', $limit)
->setParameter('offset', $offset);
If you are using raw query :
SELECT * FROM table LIMIT 20 OFFSET 21
Else you can use paginator too
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query);
$totalItems = count($paginator);
$pagesCount = ceil($totalItems / $pageSize);
// now get one page's items:
$paginator
->getQuery()
->setFirstResult($pageSize * ($currentPage-1)) // set the offset
->setMaxResults($pageSize); // set the limit
foreach ($paginator as $pageItem) {
echo "<li>" . $pageItem->getName() . "</li>";
}
I am trying to set up a jqgrid and I am having difficulty in constructing the controller that generates the json data to populate the grid. I am using codeigniter 2.0+ and I am not sure how to build the query for php in codeigniter.
I followed this guid under "Loading Data -> JSON Data" for the jqgrid. I also consulted codeigniter docs on data selection. The thing is I am not sure how to write the second query to sort and limit according to the jqgrid paramiters. Here is my controller.
public function applicantdata(){
$page = $this->input->get('page');// get the requested page
$limit = $this->input->get('rows');// get how many rows we want to have into the grid
$sidx = $this->input->get('sidx');// get index row - i.e. user click to sort
$sord = $this->input->get('sord');// get the direction
if(!$sidx){ $sidx =1; }
$this->db->select('*');
$this->db->from('applicant');
$this->db->join('transaction', 'transaction.applicant_id = applicant.id');
$query = $this->db->get();
$count = $query->num_rows();
$limit = 10;
if( $count > 0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages){ $page=$total_pages; }
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
//NOT SURE HOW TO DO THIS IN CODEIGNITER ENVIRONMENT
//$SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id ORDER BY $sidx $sord LIMIT $start , $limit";
//$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$result = $query->result_array();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
foreach ($result as $myrow){
$responce->rows[$i]['id']=$myrow['id'];
$responce->rows[$i]['cell']=array($myrow['id'],$myrow['firstname'],$myrow['lastname'],$myrow['amount'],$myrow['status']);
$i++;
}
echo json_encode($responce);
}
With the above code, the grid is populating and its is working to a greater extent. Only thing is the pagination stuff not working properly in that the data on page one shows when I move to page 2, 3, etc.
Her is the final working thing. For anyone who may need it.
public function applicantdata(){
$page = $this->input->get('page');// get the requested page
$limit = $this->input->get('rows');// get how many rows we want to have into the grid
$sidx = $this->input->get('sidx');// get index row - i.e. user click to sort
$sord = $this->input->get('sord');// get the direction
if(!$sidx){ $sidx =1; }
$this->db->select('firstname');
$this->db->from('applicant');
$this->db->join('transaction', 'transaction.applicant_id = applicant.id');
$query = $this->db->get();
$count = $query->num_rows();
if( $count > 0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages){ $page=$total_pages; }
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
$this->db->select('*');
$this->db->from('applicant');
$this->db->join('transaction', 'transaction.applicant_id = applicant.id');
$this->db->order_by("applicant.id", $sord);
$this->db->limit($limit, $start);
$query2 = $this->db->get();
$result = $query2->result_array();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
foreach ($result as $myrow){
$responce->rows[$i]['id']=$myrow['id'];
$responce->rows[$i]['cell']=array($myrow['id'],$myrow['firstname'],$myrow['lastname'],$myrow['amount'],$myrow['status']);
$i++;
}
echo json_encode($responce);
}
I'm trying to make a website with movies in it, everything is fine but i have just 1 little problem,when ever i make a website, i do all work in my local computer test it then I upload the web, the code below is for paging with query it works fine in WAMP (locally). But when I upload the paging code to my web server it says NOT EXIST.
it shows the else part,whats the problem?
<?php
$per_page = 35;
$page = 1;
if (isset($_GET['page']))
{
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
$con= mysql_connect("localhost","sarya_asad","Thisisfor123");
mysql_select_db('saryaal_com_movies',$con);
$current_items = mysql_query( "SELECT * FROM `english` LIMIT $start_from, $per_page");
if( mysql_num_rows($current_items) > 0)
{
while($item = mysql_fetch_assoc($current_items))
{
?>
<tr>
<td> <strong><a href="english/english-preview.php?id=<?php echo$item['id']?>" ><?php echo $item['title'] ;?></a> </strong></td>
<td> <strong> <?php echo $item['year'] ;?> </strong></td>
<td> <strong> <?php echo $item['quality'] ;?> </strong> </td>
</tr>
<tr><td>
<?php
}
}
else
{
echo 'this page does not exists';
}
$total_rows = mysql_query("SELECT COUNT(*) FROM `english`");
$total_rows = mysql_fetch_row($total_rows);
$total_rows = $total_rows[0];
$total_pages = $total_rows / $per_page;
$total_pages = ceil($total_pages); # 19/5 = 3.8 ~=~ 4
for($i = 1; $i <= $total_pages; ++$i)
{
echo "<a href='temp2.php?page=$i' class='pagNumActive'>$i</a> ";
}
?>
<?php if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
$sql=mysql_query("select * from job where AND status='Active'");
}
$per_page = 5;
$page = 1;
if (isset($_GET['page'])) {
$page = intval($_GET['page']);
if($page < 1) $page = 1;
}
$start_from = ($page - 1) * $per_page;
if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="")
{
$current_items=mysql_query("select * from job LIMIT $start_from, $per_page"); } $start_from, $per_page");
if( mysql_num_rows($current_items)>0) { while($arr=mysql_fetch_array($current_items)) {
?>
<?php include("include/result.php") ?>// result u want to display
<?php
}
} else {
echo 'Data does not exists'; }
if($Strkeyword=="" AND $StrLoc=="" AND $StrMinsal=="-1" AND $StrMaxsal=="-1" AND $StrMax_exp=="maximum" AND $Strcategory=="" AND $Strjobtype=="") {
$total_rows=mysql_query("select COUNT(*) from job where AND status='Active'");
}
$total_rows = mysql_query("SELECT COUNT(*) FROM job");
$total_rows = mysql_fetch_row($total_rows);
$total_rows = $total_rows[0];
$total_pages = $total_rows / $per_page;
$total_pages = ceil($total_pages);
# 19/5 = 3.8 ~=~ 4
echo "<div style='margin-left:280px;'>";
echo "Page : ";
for($i = 1; $i <= $total_pages; $i++) {
echo "[<a style='text-decoration:none' href='search_result.php?page=$i' class='pagNumActive'>$i</a> ]";
}
echo "</div>";
?>
This is your problem:
$con= mysql_connect("localhost","sarya_asad","Thisisfor123");
mysql_select_db('saryaal_com_movies',$con);
You need to change the host details. localhost is the local server on your machine.
change this
$start_from = ($page - 1) * $per_page;
to
$start_from = ($page) * $per_page;
you might want to use something like:
$per_page = 35;
$start_from = $page * $per_page - $per_page;
Try this logic for pagination:
Your goal is to offset by the number of images you want to display on each page.
So let's say you want to display 6 movie thumbnails on a page... You'd have:
$videos_per_page = 6
$pageNumber = (isset($_GET['page']) ? ($_GET['page']) : 1);
And your offset would be:
$offset = $videos_per_page * $pageNumber
(6 videos * page 0... so you're offsetting 0 videos. That's good because you want to display the first 6 videos in your data structure on page 0).
So now that you have your offset value... You need to set your array pointer to the correct spot... Loop through your database rows storing your movies and move your pointer by your offset value... Store that in $videos_to_offset...
while ($videos_per_page < $offset && ($row = $Query_Result->fetch_assoc())) {
$videos_to_offset++;
}
Now you can loop through your database rows, outputting your videos from wherever your offset array pointer left off:
$video_counter = 0;
while ($video_counter < $videos_per_page && ($row = $Query_Result->fetch_assoc())) {
echo $row['videopath'];
$video_counter++;
}
Something like that.
This should be a fairly straight forward question. I am getting a Warning: mysql_num_rows() expects parameter 1 to be resource, boolean error and I can't figure out the correct syntax. Using mysqlerror I am getting You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-5, 5' .
Any help would be great. The code is part of a pagination that I am adding.
Snippet of affected code
<?php
//Number of items to display per page
$perpage = 5;
if(isset($_GET["page"]))
{
$page = intval($_GET["page"]);
}
else
{
$page = 1;
}
$calc = $perpage * $page;
$start = $calc - $perpage;
$result = mysql_query("select * from products Limit $start, $perpage");
$rows = mysql_num_rows($result);
echo mysql_error();
if($rows)
{
$i = 0;
while($post = mysql_fetch_array($result))
{
?>
$start cannot be negative (and it is). You should make sure, that $page is not less than 1. This should fix your problem:
<?php
//Number of items to display per page
$perpage = 5;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
if ($page < 1)
{
$page = 1;
}
$calc = $perpage * $page;
$start = $calc - $perpage;
$result = mysql_query("select * from products Limit $start, $perpage");
$rows = mysql_num_rows($result);
echo mysql_error();
if($rows)
{
$i = 0;
while($post = mysql_fetch_array($result))
{
?>
when you're using LIMIT the value of the starting point should be greater or equal to zero.
LIMIT