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
192 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
IT, Stop Being Called Into Every Meeting

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!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.

760 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

20 Experts available now in Live!

Get 1:1 Help Now