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
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

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…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

734 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