Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2003
    Location
    Massachusetts
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Using $_GET without a variable in the URI

    First off, I've only been working with PHP for 3 or 4 weeks.
    So this might be an easy problem to solve, maybe not.

    Here's my problem: EVERY page on my site has ?p= (eg. ?p=games) except for the index page, so that func.php knows that file to include. The problem comes about when ?p= does not appoear in the URL (on the index).

    When people are looking at /index.php or / I want to include news/show_news.php. I have tried all the diff combinations of if, elseif, else, etc. and nothing works.

    Right now when you look at index.php, with no ?p=, it shows no content. I want it to show the news when ?p= does not appear in the URL.

    http://smartass.buildtolearn.com/

    Any suggestions?

    the code w/ the problem in index.php:

    PHP Code:
    if($_GET['p']){
    include(
    'func.php');
    content();
    }
    else{
    include(
    "news/show_news.php");

    func.php:

    PHP Code:
    <p align="center">
    <?php
    function content() {
            if(
    $_GET["p"] == "") { require("links/config.php");}
            if(
    $_GET["p"] == "join") { include("links/join.php");}
            if(
    $_GET["p"] == "lostcode") { include("links/lostcode.php");}
            if(
    $_GET["p"] == "lostid") { include("links/lostid.php");}

    .......

    }
    ?>
    </p>
    Last edited by SmArTaSs; 11-28-2003 at 10:38 PM.
    -Matt

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you want to include the files are redirect to them?


    Anyway,
    PHP Code:
    if($_GET['p']){
    include(
    'func.php');
    content();
    }
    else{
    include(
    "[b]./[/b]news/show_news.php");

    (note the ./ in front of the adress)
    Would probably work (if that file existsn that is ... )

    Instead of
    PHP Code:
    if($_GET["p"] == "") { require("links/config.php");}
            if(
    $_GET["p"] == "join") { include("links/join.php");}
            if(
    $_GET["p"] == "lostcode") { include("links/lostcode.php");}
            if(
    $_GET["p"] == "lostid") { include("links/lostid.php");} 
    You'd probably better use a switch
    PHP Code:
    switch ($_GET['p'] ) {
        case 
    'join':
            include(
    './links/join.php');        
            break;
        case 
    'lostcode':
            include(
    './links/join.php');
            break;
    ....
        default:
            require(
    './links/config.php');

    which will be quicker because in your code, the querystring will be compared to each comparison,, where in the switch, the comparionons will be stopped after a match (because of the break). So if you order the cases and put the most occuring on top, you'll get some performance gain.

    or you could use
    PHP Code:
    if($_GET['p'] == '') { 
      require(
    './links/config.php');
    } else {
       include(
    './links/' .  $_GET['p']  . '.php');

    will be fastest.

  • #3
    New Coder
    Join Date
    Jun 2003
    Location
    Massachusetts
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, i took your advice and the content works fine, but the news still wont show
    -Matt

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you don't get an error, then the files content is included inside your PHP file.

    But include just inserts the show_news.php code inside the page where you call it from. If the code from show_news.php doesn't generate any output, then you wount see anything.

    so add something like

    echo 'OK';

    at the first line of your show_news.php (after the php-tag of course)

  • #5
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i would do this

    $page= ((isset($_GET['p'])) && ($_GET['p']!=""))? $_GET['p'] : "config";
    // (if thre is a ?p= and it contains something... ) then we get p.. else page = config...

    if ($page=="config") require ......
    else if ($page==.... ) all the includes

    this will check for the presence of ?p and act accordingly.. (hope this helps)

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help

    Hi guys i tried this:

    <?php
    switch (isset($_GET['login'])) {

    case 'error':
    print ('Login Error');
    break;
    case 'failed':
    print ('Invalid Login');
    break;
    default:
    print ('<br>');
    }
    ?>

    But it returns always the first case the 'Login Error' even the login is failed

    Anyone can help me with that,
    Thanks in advance

  • #7
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    isset() returns either true or false, so using switch isn't really appropriate...

  • #8
    New Coder
    Join Date
    May 2006
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ohh OK, so i cant use it here

    Thanks

  • #9
    Regular Coder goughy000's Avatar
    Join Date
    Nov 2005
    Location
    England
    Posts
    415
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    // If not set or p= nothing..
    if(!isset($_GET['p'] || $_GET['p'] == ""){
       
    // include news page
       
    include_once("path/to/news");
    }else{
       
    content();

    PHP Freelancing!
    ByteForums!
    Scriptlance.comNeed a job done quick? post it on scriptlance! OR! your a programmer? sign up and get paid for these jobs!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •