1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Switching includes files - had some initial success but now can't duplicate

Discussion in 'PHP' started by Lisa, Apr 3, 2005.

  1. #1
    I'm not a programmer and I'm wondering if anyone can help with this. I set up a basic php page with an includes file, some code to switch the includes file based on the variable changing and two links that contain new values for the variable. I could swear I had this working yesterday and I don't know what I've done wrong but now I'm getting all sorts of weird results and the includes file won't switch out. Here's what the page looks like:

    <?
    $view = "";

    ?>



    <html>
    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">



    </head>

    <body>
    <? include "main.php";?>
    <? switch($view)
    {
    case "Happy":
    include "file1.php";
    break;

    case "Sad":
    include "file2.php";
    break;

    default: include "main.php";

    }
    ?>

    <p><a href="index.php?view=Happy">file1</a>
    </p>
    <p><a href="index.php?view=Sad">file2</a></p>
    </p>


    <p>&nbsp;</p>
    </body>
    </html>
     
    Lisa, Apr 3, 2005 IP
  2. mushroom

    mushroom Peon

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    To start with include is a funtion requring the use of " () "s
    Like " include ("file1.php"); " use without () is not recomended.
     
    mushroom, Apr 3, 2005 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #3
    Mushroom is right about the function but the brackets aren't required, so sadly that wasn't the solution.

    At the top of your script you set view = "" which means it'll always do the default.

    Now, I wanted to hack about with your script so I created a function called safe_include which tests to see if the file is there, which it should always be, but for testing I needed it. You can use the normal include.
    I also changed the name of the file in the links at the bottom and used the global $PHP_SELF because my test script had a different name.

    <?
    //$view = "";
    // go for a safer option, especially when using includes
    if ( isset( $_GET['view'])) $view = $_GET['view'];
    else $view = '';
    function safe_include($file)
    {
     if ( file_exists($file)) include $file;
     else echo "<p><b>{$file} would be here</b></p>";
    }
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <body>
    <?php
    safe_include('main.php');
     switch($view)
    {
     case "Happy":
      safe_include('file1.php');
      break;
     case "Sad":
      safe_include( 'file2.php');
      break;
     default: 
      safe_include( 'default.php');
      break;
    } 
    
    echo "<p><a href='{$PHP_SELF}?view=Happy'>file1</a></p>
    <p><a href='{$PHP_SELF}?view=Sad'>file2</a></p>
    <p><a href='{$PHP_SELF}?'>No View</a></p>";
    ?>
    </body>
    </html>
    Code (markup):
    Sarah
     
    sarahk, Apr 4, 2005 IP
  4. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #4
    A quote from http://ca3.php.net/manual/en/function.include.php:
    Search for words from this quote for an example where specifying parenthesis will actually result in incorrect behavior.

    J.D.
     
    J.D., Apr 4, 2005 IP
  5. Lisa

    Lisa Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sarahk--thanks so much for your efforts, it works wonderfully well and without fail! I really appreciate it. And thanks to everyone for the input.
     
    Lisa, Apr 5, 2005 IP