Solved

how to includes different files by clicking on a forword button and backword button in a php page?

Posted on 2008-06-25
7
212 Views
Last Modified: 2008-06-26
hello:
in php page, there is some conent and in this page, i also need to includes file:
but i have a list of files and i put the file names in an array: (please see the code )

i want to put two buttons in the page, one for forward, the other for backward. each time when user click on button, it will include a different file.

any suggestion for how to implement this? a simple code will be greatly appreciated! thanks a lot!

<?php
$pages = array("content/home_01.php","content/home_02.php","content/home_03.php","content/home_04.php","content/home_05.php");
$page = $pages[0];?>
<?php includes ('./folder/file1.php') ?>.

Open in new window

0
Comment
Question by:lilyyan
  • 5
  • 2
7 Comments
 
LVL 5

Assisted Solution

by:codeQuantum
codeQuantum earned 400 total points
ID: 21869920
Suppose your file is called index.php... you can use a GET parameter to indicate that you want the next page or the previous. Then use that parameter to decide which file to include.

To use the GET parameter, use this in your link :

<a href="index.php?action=next">next</a>
<a href="index.php?action=back">next</a>

Then in your PHP code you can do this :


if ($_GET['action'] == 'next') include($theRightIncludePathFromArrayForNext);
else include($theRightIncludePathFromArrayForBack);

Open in new window

0
 
LVL 5

Assisted Solution

by:codeQuantum
codeQuantum earned 400 total points
ID: 21869931
Sorry the links should be :

<a href="index.php?action=next">next</a>
<a href="index.php?action=back">back</a>
0
 

Author Comment

by:lilyyan
ID: 21870391
thanks you so much! i'm trying your code...

would you please more specific on how iterate the array? i'm kind of confused.

looks like every time i need plus 1 for foward and minus 1 for backword... i don't know how to do this..
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 5

Accepted Solution

by:
codeQuantum earned 400 total points
ID: 21871004
There are many ways to do it. Here is one building on what we have seen so far.
//Links in your home page :
 
<?php if (isset($_GET["p"])) $position = $_GET["p"]; else $position = 0; ?>
<a href="index.php?p=<?php  echo $position; ?>&action=next">next</a>
<a href="index.php?p=<?php  echo $position; ?>&action=back">back</a>
 
//$position, or P specifies your position. Ie which URL you are including from your array. If p =1 then you are using $pages[1].
 
// Your include
 
if (isset($_GET["action"]))
{
    if ($_GET["action"] == "back") 
    { 
        if ($position == 0) $position++; // we can't go back from the first page
        include($pages[$position-1]);
    }
    
    if ($_GET["action"] == "next")
    {
        if ($position == sizeof($pages)) $position--; // we can't go past the last page
        include($pages[$position+1]);
    }
} else include($pages[0]);

Open in new window

0
 
LVL 5

Assisted Solution

by:codeQuantum
codeQuantum earned 400 total points
ID: 21871015
A little mistake in my previous code, replace line number 21 with the following :
        if ($position == (sizeof($pages)-1)) $position--; // we can't go past the last page

Open in new window

0
 

Author Comment

by:lilyyan
ID: 21877168
COOL!!. THANKS VERY MUCH!
0
 
LVL 5

Expert Comment

by:codeQuantum
ID: 21877236
You are welcome. :)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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