Jump to content

another pagination problem


deansaddigh

Recommended Posts

Can some one see if  they can spot any mistakes in my pagination for results page.

 

It only seems to be bringing back 3 results which is incorrect there are more.

 

basically im trying to display my results over several pages.

 

Heres my code

 

 // how many rows to show per page - 18 Would equal 3 rows of 6 photos. Below code within while loop specifies the amount of photos per row in the table.
				$rowsPerPage = 3;

				// by default we show first page
				$pageNum = 1;

				// if $_GET['page'] defined, use it as page number
				if(isset($_GET['page']))
				{
				    $pageNum = $_GET['page'];
				}

				// counting the offset
				$offset = ($pageNum - 1) * $rowsPerPage;	


	 $query = "SELECT course.course_id, course.name AS course_name, course.level, course.price,   course.info, course_type.type, school.school_id AS school_id, school.name AS school_name, school.street, school.town, school.city, school.county, school.region, school.postcode, school.country, school.school_facts, school.general_info, school.school_facilities
            FROM school_course
          
	    JOIN course
            ON school_course.course_id = course.course_id
          
	    JOIN school
            ON school_course.school_id = school.school_id

		JOIN course_coursetype
		ON course.course_id = course_coursetype.course_id

		JOIN course_type
            ON course_coursetype.course_typeid = course_type.course_typeid

		WHERE region = '$region' AND course_type.course_typeid ='$coursetype' AND town='$town' AND country='$country'
            ORDER BY school_name ASC" . " LIMIT $offset, $rowsPerPage";
            //Use this query below         
            $result = mysql_query($query, $conn)
               or die ("Unable to perform query: " . mysql_error());   


//echo out all details i want


//rest of pagination
//******Joes Pagination continued

			// how many rows we have in database


				$numrows = $row['numrows'];

				// how many pages we have when using paging?
				$maxPage = ceil($numrows/$rowsPerPage);

				// print the link to access each page
				$self = $_SERVER['PHP_SELF'];
			$nav  = '';

				for($page = 1; $page <= $maxPage; $page++)
				{
				   if ($page == $pageNum)
			   {
				      $nav .= " $page "; // no need to create a link to current page
				  }
				   else
				   {
				      $nav .= " <a href=\"$self?page=$page\">$page</a> ";
				  }
}

				// creating previous and next link
				// plus the link to go straight to
				// the first and last page

				if ($pageNum > 1)
				{
				   $page  = $pageNum - 1;
				   $prev  = " <a href=\"$self?page=$page\">« Prev</a> ";

				   $first = " <a href=\"$self?page=1\">[First Page]</a> ";
				}
				else
				{
				   $prev  = ' '; // we're on page one, don't print previous link
				   $first = ' '; // nor the first page link
				}

				if ($pageNum < $maxPage)
				{
				   $page = $pageNum + 1;
				   $next = " <a href=\"$self?page=$page\">Next »</a> ";

				   $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
				}
				else
				{
				   $next = ' '; // we're on the last page, don't print next link
				   $last = ' '; // nor the last page link
				}


				echo '<br/>'; 
				echo '<br/>';

				//If there is only one page then do nothing. 
				if ($maxPage ==1)
				{

				}
				//Else show user page information and links to browse multiple pages. 
				else 
				{
					// print the navigation link
					echo '<p class ="pagination">';
					echo $first . $prev . $nav . $next . $last;	
					echo '</p>';
				}



 

Thanks in advance ;)

Link to comment
Share on other sites

Found this code in my old projects, you may have to edit a little bit... but it's good

 

//max displayed per page
$per_page = 8;

//get start variable
$start = $_GET['start'];

//count records
$record_count = mysql_num_rows(mysql_query("SELECT * FROM users"));

//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal

if (!$start)
   $start = 0;
   
//display data
$get = mysql_query("SELECT * FROM users ORDER BY id DESC LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))

{
//echo out database data
//example
//echo out all users
echo "The user ". $row['user_name']  ." is registered!";
}

//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;

//show prev button
if (!($start<=0))
       echo "<a href='index.php?start=$prev'>Prev</a> ";

//show page numbers

//set variable for first page
$i=1;

for ($x=0;$x<$record_count;$x=$x+$per_page)
{
if ($start!=$x)
    echo " <a href='index.php?start=$x'>$i</a> ";
else
    echo " <a href='index.php?start=$x'><b>$i</b></a> ";
$i++;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.