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
218 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
does post require a form or curl to be post 4 55
Cookie not unsetting 7 27
What does this formatting equate to? 7 18
Help with PHP 13 27
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.
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 …

856 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