1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Coding help

Discussion in 'HTML, Graphics & Programming' started by Aruffell, 1 Feb 2006.

  1. Aruffell

    Soldato

    Joined: 29 Dec 2004

    Posts: 5,669

    Location: Chatham, Kent

    I have a site (index.php) and have made a table in it that i wish to open other files (1.php/2.php etc...)

    If they don't exist i want it to open main.php.

    I know there is an includes string to use with file_exists but can't for the life of me remember it.

    Can anyone help?

    Andy
     
  2. Sic

    Capodecina

    Joined: 9 Nov 2004

    Posts: 15,365

    Location: SO16

    there's a couple of ways to do it, personally i'd use the php $_GET so that you can differentiate between main and tabled views.

    Code:
    //get the page that you want from the url
    $view = $_GET['view'];
    
    if ($view = "table"){
    //table.php needs to include your formatted includes for each row on the table
    include ("table.php");
    }else{
    include ("main.php");
    }
    
    then to view the table, it will be visible on www.yourdomain.com/index.php?view=table so link to it that way. that'll work, but it's not what you asked for!
     
  3. Beansprout

    Man of Honour

    Joined: 31 Jan 2004

    Posts: 16,316

    Location: Plymouth

    Change:

    Code:
    if ($view = "table"){
    To:
    Code:
    if ($view == "table"){
    :)
     
  4. Sic

    Capodecina

    Joined: 9 Nov 2004

    Posts: 15,365

    Location: SO16

    oops :o
     
  5. Dj_Jestar

    Caporegime

    Joined: 18 Oct 2002

    Posts: 29,065

    Location: Back in East London

    List the pages available to the application in an array:
    Code:
    <?php
    
    $pages = array('1.php', '2.php', '3.php', 'etc.');
    
    ?>
    Then use a page id, similar to what Sic has suggested, which I'll demonstrate as the array key for $pages, and validate it:
    Code:
    <?php
    
    $pages = array('1.php', '2.php', '3.php', 'etc.');
    
    if (array_key_exists($_GET['pageid'], $pages)) {
        //pageid is valid input..
        if (file_exists($pages[$_GET['pageid']])) {
            //page exists
            include_once $pages[$_GET['pageid']];
        } else {
            //page does not exist..
            include_once 'main.php';
        }
    } else {
        //pageid is not valid input..
        die ('Invalid pageid.');
    }
    
    ?>
    This is more secure than stating the filename in the URI.

    HTH :)