Pagination with PHP and switch statement

This is a discussion on "Pagination with PHP and switch statement" within the PHP Forum section. This forum, and the thread "Pagination with PHP and switch statement are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 21st, 2006, 23:11
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
Pagination with PHP and switch statement

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

I use a switch statement on my index.php page to display each of my pages (only some of them are shown to save space)


PHP: Select all

 <?php
 
switch ($_GET['page'])
{
 
case 
"contact":
include(
'contact.php');
break;
case 
"news":include('news.php');break;
default:
include(
'home.php');
}
?>
I have a pagination script to display a set number of record per page. The first page displays ok showing http://www.mysite.com/index.php?page=newsitem but when i click on the next link it defaults back to the default page http://www.mysite.com/index.php?page=home (obviuosly because the switch statement isn't set up to handle it)
The link that appears in the browser window is http://www.mysite.com/index.php?page=newsitem?limit=2&page=2 but the default page is displayed.

How do I sort this problem out? Do I need to use nested switch and if so how do I do it.

The pagination script I am using is from http://www.allsyntax.com/tutorials/P...with-PHP/2.php

I am sure if I didn't use the switch statement it would work but it seems very inefficient (been doing visual basic for the last 2 years at college).

I have seen t done in dozens of websites including how google does it searches and need to know how to do it.

Please help as I'm tearing my hair out here

Last edited by AdRock; Jul 21st, 2006 at 23:51.
Reply With Quote

  #2 (permalink)  
Old Jul 21st, 2006, 23:22
Up'n'Coming Member
Join Date: Jan 2006
Location: East Sussex
Age: 26
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Pagination with PHP and switch statement

Problem could be in the url, instead of;

http://www.mysite.com/index.php?page=newsitem?limit=2&page=2

try

http://www.mysite.com/index.php?page=newsitem&limit=2&page=2

change that second ? for a &
Reply With Quote
  #3 (permalink)  
Old Jul 21st, 2006, 23:41
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
Re: Pagination with PHP and switch statement

I tried that and it seemed to make sense but it still goes back to the default page

I am sure it's to do with the switch statement becuase the link http://www.mysite.com/index.php?page=newsitem&limit=2&page=2
isn't in the case include.

How would I add that to the switch becuase the number of pages can increase?

Thanks for your help

p.s. If you need me to attach my code I will

Last edited by AdRock; Jul 21st, 2006 at 23:43. Reason: adding text
Reply With Quote
  #4 (permalink)  
Old Jul 22nd, 2006, 07:31
Up'n'Coming Member
Join Date: Jan 2006
Location: East Sussex
Age: 26
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Pagination with PHP and switch statement

Hi AdRock,

I've just noticed that i missed something quite obvious last night. The reason that when you use index.php?page=newsitem&limit=2&page=2 it redirects to the default page is that you have page defined twice in the url so your $_GET vars only have two values, limit => 2 and page => 2, as there is no switch check in the switch statement called 2 it loads the default page. I think you need to rename the second page variable to something else and it should solve that problem
Reply With Quote
  #5 (permalink)  
Old Jul 22nd, 2006, 08:06
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
Re: Pagination with PHP and switch statement

Thanks Gee Bee, I did what you suggested and made a change of my own and now it works.

I do have another question you may be able to help with though.

This is the change I made from $limit = 10 to $limit = 2 becuase my news items can be quite long so I only want 2 per page
PHP: Select all

 //set default if: $limit is empty, non numerical, less than 2, greater than 50 
if((!$limit)  || (is_numeric($limit) == false) || ($limit 2) || ($limit 50)) { 
     
$limit 2//default 
How do I edit these hyperlinks or do i not have to?
PHP: Select all

//Results per page: **EDIT LINK PATH** 
echo("   
<a href=?page=newsitem&limit=10&amp;pagenum=1></a> 
<a href=?page=newsitem&limit=25&amp;pagenum=1></a> 
<a href=?page=newsitem&limit=50&amp;pagenum=1></a>"
); 
Thanks for all your help helping me out
Reply With Quote
  #6 (permalink)  
Old Jul 22nd, 2006, 08:13
Up'n'Coming Member
Join Date: Jan 2006
Location: East Sussex
Age: 26
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Pagination with PHP and switch statement

your if statement could be written like this;

if(!isset($limit) || empty($limit) || !is_numeric($limit) || ($limit < 2) || ($limit > 50)) {
$limit = 2; //default

Not going to make a difference to how it performs but is more readable... for me anyway!

the <a> tags look fine but you dont need amp; in them so they could just be

<a href=apage.php?page=newsitem&limit=10&pagenum=1></a>

Last edited by Gee Bee; Jul 22nd, 2006 at 08:19.
Reply With Quote
  #7 (permalink)  
Old Jul 23rd, 2006, 08:30
benbacardi's Avatar
Highly Reputable Member
Join Date: Feb 2004
Location: United Kingdom
Age: 20
Posts: 611
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benbacardi Send a message via Skype™ to benbacardi
Re: Pagination with PHP and switch statement

you do need &amp; if your going 2 make it validate with xhtml 1.0
Reply With Quote
Reply

Tags
pagination, php, switch, statement

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
CSS style switch Heavensword Web Page Design 3 Jul 24th, 2007 15:24
Need small help with pagination ofi PHP Forum 0 Feb 7th, 2007 11:44
Pagination without MySQL Catie PHP Forum 3 Jan 21st, 2007 23:06
RSS feed pagination deesto Web Page Design 0 Dec 24th, 2006 02:31
PHP pagination robertboyle PHP Forum 1 Jul 30th, 2006 18:08


All times are GMT. The time now is 21:14.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43