• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

How do you use "index.php?" to load a new page?

For example, how do you get "http://www.mysite.com/index.php?page_number=2" to point to a new page?
0
techcw
Asked:
techcw
3 Solutions
 
Lukasz ChmielewskiCommented:
You can use it like this:
<?php
switch($_GET['page_number']){
   case 1: include("page1.php"); break;
   case 2: include("page2.php"); break;
   default: include("page1.php"); break;
}
?>

Open in new window

0
 
Ray PaseurCommented:
Your script would find this variable, created by the GET method request, using information in the URL.

$_GET["page_number"] == "2"

You would use that variable to decide what new page to load.  You might use include() to load the page.  Or you might use header() to redirect the client browser.

If you're new to PHP programming you might like this book.
http://www.sitepoint.com/books/phpmysql4/

Best regards, ~Ray
0
 
Hugh McCurdyCommented:
I'm not sure what you mean by "point to a new page."

I can think of 3 definitions.

Link to a new page
Include a new page
Load a new page

And you could me something else.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
techcwAuthor Commented:
For example, I am trying to load the next page by using "index.php?page_num=2"

Instead of loading the next page, the original index page is loaded instead with the "index.php?page_num=2" part displaying in the browser.

The script I'm using uses the GET method. Do I name the next page something specific?
0
 
techcwAuthor Commented:
Here is the script I'm trying to use. Its intent is to paginate without needing a database.

 
<h1 style="float:left">Pagination Function</h1>
<div style="clear:left;"></div>
<hr />
<?php
##################################
# YOU CAN CONFIGURE THIS #
$num_of_items = 200;
$items_per_page = 9;
$max_links = 10;
# END CONFIGURATION #
#################################
if($_GET['page_num']){
	$page_num = $_GET['page_num'];
}else{
	$page_num = 1;
}
?>
<h1>This Is Page: <span style="color:#F00;"># <? echo $page_num; ?></span></h1>
<strong>example:</strong> <br>
<strong>number of items</strong> = <? echo $num_of_items; ?><br>
<strong>number of items per page</strong> = <? echo $items_per_page; ?><br>
<strong>number of links per page</strong> = <? echo $max_links; ?><br>
<?php
 ###### START PAGINATION FUNCTIONS ###### 
function pagination_link($id, $page_num){
	return $_SERVER['PHP_SELF'].'?page_num='.$page_num;
}
function pagination($num_of_items, $items_per_page, $id, $page_num, $max_links){
 	$total_pages = ceil($num_of_items/$items_per_page);
 	if($page_num) {
 		if($page_num >1){ 
 			$prev = ' &nbsp; <a href="'.pagination_link($id, ($page_num -1 )).'">&lt; PREV</a> &nbsp; '; 
 			$first = '<a href="'.$_SERVER['PHP_SELF'].'">&lt;&lt; First Page</a>'; 
 		}
 	}
	 if($page_num <$total_pages){ 
		$next = ' &nbsp; <a href="'.pagination_link($id, ($page_num+1)).'">NEXT &gt;</a> &nbsp; '; 
		$last = ' &nbsp; <a href="'.pagination_link($id, $total_pages).'">Last Page &gt;&gt;</a> &nbsp; ';
	 }
	 echo $first;
	 echo $prev;
	 $loop = 0;
	 if($page_num >= $max_links) {
		$page_counter = ceil($page_num - ($max_links-1));
	 }else{
		$page_counter = 1;
	 }
	 if($total_pages < $max_links){
		$max_links = $total_pages;
	 }
	 do { 
		if($page_counter == $page_num) {
			echo ' &nbsp; <strong>'.$page_counter.'</strong> &nbsp; '; 
		}else{
			echo '<a href="'.pagination_link($id, ($page_counter)).'">'.$page_counter.'</a> &nbsp; ';
		} 
		$page_counter++; $current_page=($page_counter+1);
		$loop++;
	 }while ($max_links > $loop);
	 echo $next;
	 echo $last;
}

?>
<div align="center">
<?php
 pagination($num_of_items, $items_per_page, $id, $page_num, $max_links)
 ?> 
</div>

Open in new window

0
 
Hugh McCurdyCommented:
OK, still confused by what you mean by load.

1. You want to include the contents of the loaded page within index.php.  You could use this to load a navigation bar, for instance.
2. You don't want index.php to display anything at all.  That is, you want to see (for example) main_catalog.php instead of index.php

In case 1, both Roads_Roads and Ray_Passeur offer good advice (use include).  Ray also noticed that "load" isn't a precise term and suggested you could use header.  Using header is the solution to the 2nd type of load I mentioned.

If you use header, you should output anything first because it won't work.  If you want to use header and you are getting a message like "headers already sent" you can use ob_start() to prevent output until you've processed $_GET.

Finally, I'm still not confident that you understand what we are talking about.  If not, please try to give more information.  (At least choose #1 or #2 or "none of the above" to help us focus on your need.)
0
 
Hugh McCurdyCommented:
OK, that makes more sense.  Let me think a bit.
0
 
Hugh McCurdyCommented:
I didn't read in great depth but hopefully I got the gist.

Something like this?  
http://www.berenshoes.com/men/c/D_1001_4/sb/Born%20On%20Date/sd/DESC/filter/100000001194eq100000001260or100000001242or100000001255/

Hopefully my selections are in the URL.  If not, just keep expanding the types of shoes you want and eventually page #'s show up.  In any event, is that what you mean about pagination?

What they did is different that what you want.  In your case, if that's what you want for pagination, I'd use header()

http://php.net/manual/en/function.header.php
http://www.w3schools.com/php/func_http_header.asp
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now