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
226 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

685 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