Jump to content


Photo

Help with displaying pagaination with switch statement


  • Please log in to reply
5 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 21 July 2006 - 08:48 PM

Hello there......I have a huge problem and have no idea how to sort it

I use a switch statement to display each of my pages (only some of them are shown to save space)
<?php

switch ($_GET['page'])
{
	
case "contact":
include('contact.php');
break;

default:
include('home.php');
}
?>

and i have a pagination script to display a set number of record per page.  The first page displays ok but when i click on the next page link it defaults back to the default page (obviuosly because the switch statement isn't set up to handle it)
The link that appears in the browser window is http://www.mysite.co.uk/index.php?page=newsitem?limit=2&page=2 but no page is displayed

How do I sort this problem out?  ???

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 July 2006 - 09:09 PM

I can find two problems just in the URL. You have two '?'s and two 'page's defined. Perhaps change the name of one of the pages and figure out where the double question marks are coming from?
http://www.mysite.co.uk/index.php?page=newsitem?limit=2&page=2
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 21 July 2006 - 09:33 PM

I have looked at www.php.net/switch and have noticed a post on nested switch.

Could that be a solution to the problem and if so how do I implement it.

As for the 2 pages, page=2 is in the pagination script
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#4 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 July 2006 - 10:14 PM

My assumption is the issue is with the two "page"s being declared. Try putting
case '2': print "Need to fix some GET names"
in the switch you included and see if it returns anything.
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#5 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 21 July 2006 - 11:22 PM

Right......here is my index.php switch statement
<?php
	switch ($_GET['page'])
    	{
       			
       	case "contact":
	include('contact.php');
	break;

	case "news":
	include('newsitem.php');
	break;

       	default:
	include('home.php');
    	}
?>
and here is the pagination script i'm using in news.php
<? 
//REMEMBER TO CONNECT TO DATABASE! 

include_once("includes/connection.php");
    @mysql_connect($host, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER"); 
    @mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB"); 
//**EDIT TO YOUR TABLE NAME, ECT. 

$t = mysql_query("SELECT * FROM `news`"); 
  if(!$t) die(mysql_error()); 
    
$a                = mysql_fetch_object($t); 
$total_items      = mysql_num_rows($t); 
$limit            = $_GET['limit']; 
$type             = $_GET['type']; 
$page             = $_GET['page']; 

//set default if: $limit is empty, non numerical, less than 10, greater than 50 
if((!$limit)  || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) { 
     $limit = 2; //default 
} 
//set default if: $page is empty, non numerical, less than zero, greater than total available 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { 
      $page = 1; //default 
} 

//calcuate total pages 
$total_pages     = ceil($total_items / $limit); 
$set_limit          = $page * $limit - ($limit); 

//query: **EDIT TO YOUR TABLE NAME, ECT. 

$q = mysql_query("SELECT * FROM `news` LIMIT $set_limit, $limit"); 
  if(!$q) die(mysql_error()); 
     $err = mysql_num_rows($q); 
       if($err == 0) die("No matches met your criteria."); 

//Results per page: **EDIT LINK PATH** 
echo("   
<a href=?page=newsitem?limit=10&amp;page=1></a> 
<a href=?page=newsitem?limit=25&amp;page=1></a> 
<a href=?page=newsitem?limit=50&amp;page=1></a>"); 

//show data matching query: 
while($code = mysql_fetch_object($q)) { 
     echo("item: ".$code->title."<BR>"); 
} 

$id = urlencode($id); //makes browser friendly 

//prev. page: **EDIT LINK PATH** 

$prev_page = $page - 1; 

if($prev_page >= 1) { 
  echo("<b>&lt;&lt;</b> <a href=?page=newsitem?limit=$limit&amp;page=$prev_page><b>Prev.</b></a>"); 
} 

//Display middle pages: **EDIT LINK PATH** 

for($a = 1; $a <= $total_pages; $a++) 
{ 
   if($a == $page) { 
      echo("<b> $a</b> | "); //no link 
     } else { 
  echo("  <a href=?page=newsitem?limit=$limit&amp;page=$a> $a </a> | "); 
     } 
} 

//next page: **EDIT THIS LINK PATH** 

$next_page = $page + 1; 
if($next_page <= $total_pages) { 
   echo("<a href=?page=newsitem?limit=$limit&amp;page=$next_page><b>Next</b></a> &gt; &gt;"); 
} 

//all done 
?>
if you can make sense of why it's not displaying page=2 etc please let me know
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#6 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 24 July 2006 - 03:58 PM

You are referencing the variable $_GET['page'] in two contexts. Once, in the first block of code you posted, you use it to determine the page type (contact, news, etc). The second time, you use it for your pagaination (and expect a number). I'm assuming you set the name for the first instance of 'page' yourself. I'd recommend changing that to 'type' or another word. That should remove your issue.
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users