Results 1 to 10 of 10

Thread: PHP Variables

  1. #1
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default PHP Variables

    I have been searching the internet about this... Still nothing, but I know its possible:

    I need to display content based on if they have a certain variable...

    I need it to be like this: http://mywebsite.com/downloads/download.php?id=1 would do this: include('files/wallpaper/chat.php');

    So therefore http://mywebsite.com/downloads/download.php?id=2 would do this: include('files/screensaver/download.php');

    and all other requests that don't have a variable in the url would include "nodownload.php"

    Thanks guys

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Something like this should do it. Not sure if the switch default includes empty so the first two lines maybe removable.

    PHP Code:
    <?php
    if (empty($_GET['id']))
            
    header("Location : nodownload.php");
    switch (
    $_GET['id'])) {
        case 
    1:
            include(
    'files/wallpaper/chat.php');
        break;
        case 
    2:
            include(
    'files/screensaver/download.php');
        break;
        default:
        
    header("Location : nodownload.php");
        break;
    }
    ?>
    Corrections to my coding/thoughts welcome.

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Slow down and start with the answer you already have here:
    http://dynamicdrive.com/forums/showthread.php?t=64003

    The rest is just some basic if statements. If that is too complicated, you need to start with some introductory PHP tutorials to understand how PHP works. Or you could hire someone to make his for you.


    Edit: looks like bluewalrus posted as I did. That's a good answer. But still, you should start with the basics of PHP.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by bluewalrus View Post
    Not sure if the switch default includes empty so the first two lines maybe removable.
    the default case does apply to empty/unset variables. However, you'd still get a warning from php, so it's best to leave the empty() check in place.

  5. #5
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Well, I sort of got code from a tutorial, but the problem is, is that it would be: http://website.com/downloads/download.php?1 or http://website.com/downloads/download.php?2

    And thats the bad thing, I want to make it http://website.com/downloads/download.php?id=1

    Code:
    <?php
    //If is defined URL variable 'aboutme'
    if(isset($_GET['spoutclient'])){
    // include page about me
    include('dl/files/spout/spout.php');
    //else if is defined URL variable 'interests'
    }else if(isset($_GET['chatclient'])){
    // include page interests
    include('dl/files/chatclient/chatclient.php');
    // in all other cases include the home page
    } else {
    include('nodownload.php');
    }
    ?>
    That code works for http://website.com/downloads/download.php?2, but I want to add the ?id=2 instead.

  6. #6
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Bump.

  7. #7
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    What tutorial did you use? Are you using a framework, htaccess? The case you presented in your first post should work with the solution presented, please provide more details if it doesn't work.
    Corrections to my coding/thoughts welcome.

  8. #8
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    I don't understand what you're trying to do. Your code doesn't add up to anything that you're trying to convey to us.

    If you want to get the value of a query string, use _GET with the variable of the name set in the query.

    For example:
    PHP Code:
    <?php echo $_GET['id']; ?>
    If you type in the URL page.php?id=2, the page should print 2.
    - Josh

  9. #9
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Okay, there are things like $name='ChatClient' on the includes page, it does not show any text, it just defines the variables, now on the page with the php scripts (the one in my last post) tells it which page to include depending on the url variable. And the includes define the variables.

    And the problem is, I can't find out how to make it http://mysite.com/downloads/download.php?id=chat, instead of ?chat...

  10. #10
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    I just explained how--Nevermind. This thread is clearly hopeless.
    - Josh

Bookmarks

Posting Permissions

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