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

Posted on 2011-10-02
Last Modified: 2012-05-12
For example, how do you get "" to point to a new page?
Question by:techcw
    LVL 27

    Assisted Solution

    by:Lukasz Chmielewski
    You can use it like this:
       case 1: include("page1.php"); break;
       case 2: include("page2.php"); break;
       default: include("page1.php"); break;

    Open in new window

    LVL 107

    Assisted Solution

    by:Ray Paseur
    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.

    Best regards, ~Ray
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    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.

    Author Comment

    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?

    Author Comment

    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 />
    $num_of_items = 200;
    $items_per_page = 9;
    $max_links = 10;
    	$page_num = $_GET['page_num'];
    	$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>
    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));
    		$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; '; 
    			echo '<a href="'.pagination_link($id, ($page_counter)).'">'.$page_counter.'</a> &nbsp; ';
    		$page_counter++; $current_page=($page_counter+1);
    	 }while ($max_links > $loop);
    	 echo $next;
    	 echo $last;
    <div align="center">
     pagination($num_of_items, $items_per_page, $id, $page_num, $max_links)

    Open in new window

    LVL 13

    Expert Comment

    by:Hugh McCurdy
    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.)
    LVL 13

    Expert Comment

    by:Hugh McCurdy
    OK, that makes more sense.  Let me think a bit.
    LVL 13

    Accepted Solution

    I didn't read in great depth but hopefully I got the gist.

    Something like this?

    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()

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to count occurrences of each item in an array.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now