#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Beginner's stuff - GET parameter mode


    First of all I want to say that Iím total beginner in php and so, this question may sound somewhat weird. But I need to write some php code for some school task. So, I hope you could help.
    I have a so-called controller file: controller.php. And two template files Ė gallery.tpl and bigpic.tpl. First of them shows all small pictures, the second one is for big picture (one at a time on a page). I need to change the url so that, if I look at some big picture, the url will be something like controller.php?mode=picture&id=1. (I already managed to do this). But if I look at the gallery page, the url should be controller.php?mode=gallery.
    How to do this? Should I put some link to controller.php file?
    Iíve all the necessary code and also a switch statement:

    PHP Code:
    $mode="something"
    if (isset(
    $_GET['mode']) && $_GET['mode']!=""){
      
    $mode=$_GET['mode'];
    }

    switch(
    $mode){                
     case 
    "gallery":
     require(
    "gallery.tpl");
     break;
     
     case 
    "picture":
     require(
    "bigpic.tpl");
     break;
     } 
    But still, itís not enough yet. How can I tell the controller.php that the mode of the main (gallery) page Ė homepage - shoud be Ągalleryď. If I add gallery.tpl as a default value to switch statement, then it wonít set this mode the way I want it.

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    First, please enclose your code in [ PHP ] tags. See the sticky at the top of this forum.

    Second, you did not post your controller.php so it is not clear what it does. However, if you want to get the proper mode value, then yes you need to include a link with that URL somewhere/somehow.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    You could just make the DEFAULT mode "gallery". Either default it to "gallery" instead of "something," or have the switch have a default case for gallery mode.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    You could just make the DEFAULT mode "gallery". Either default it to "gallery" instead of "something," or have the switch have a default case for gallery mode.
    Thanks!
    I actually tried to change the word "something" to "gallery"
    PHP Code:
    $mode="gallery"
    , and it worked at first, but when I reloaded the browser, it didn't show the right url any more the second time.

    How could I exactly write that default sentence at the end of switch statement what you talked about?
    I tried to add this sentence at the end of my switch statement, but it didn't work:

    PHP Code:
    default: 
    mode="gallery"
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    I'm not sure what the actual question is. Are you asking:

    A) "I want www.mysite.com/controller.php to show the gallery view."

    Answer: Use a default.

    B) "I want www.myslte.com/controller.php?mode=gallery to be the only URL that anyone uses."

    Answer: Put this at the top of the controller, before anything else:
    PHP Code:
    if ( !isset($_GET['mode']) ) {
      
    header('Location: controller.php?mode=gallery');
      die();

    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Sorry for not being clear enough! I actually need it this way that if I look at the the page, where all the small pictures are (Gallery page - which is also like a main page or homepage. The template for this view is gallery.tpl), then the url in the address bar would be: controller.php?mode=gallery
    But it's important that the page would be also available if you simply search it: controller.php. So, there should be some redirecting I think.

    So, when you write to the address bar: ..../controller.php, when you see that gallery of small pictures (gallery.tpl) and the url on the address bar changes and shows controller.php?mode=gallery

    I hope it was more clear.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Solution B.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by ManiacDan
    Solution B.
    I tried this one, but if I search ...../controller.php, then I'll get an error and won't see the page at all.

    I think I should still use this switch statement and then do something else (because the whole task for school is associated with the GET parameter 'mode')

    PHP Code:
    switch($mode){                 
     case 
    "gallery"
     require(
    "gallery.tpl"); 
     break; 
      
     case 
    "picture"
     require(
    "bigpic.tpl"); 
     break; 
     } 
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    "get an error"?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    "Not found" type of error.
    The requested URL ....../controller.php was not found on this server.

    It happens when I use this "solution B" code and search the address ...../controller.php in the browser.

    controller.php and controller.php?mode=gallery should be somehow associated with each other, using GET parameter mode. But unfortunately I don't have enough knowledge of PHP yet to realize it.
  20. #11
  21. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    I'm confused because the solution I gave you was exactly what you're asking for. Have you EVER been able to hit controller.php?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  22. #12
  23. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Originally Posted by phpchick
    "Not found" type of error.
    The requested URL ....../controller.php was not found on this server.

    It happens when I use this "solution B" code and search the address ...../controller.php in the browser.
    You have "controller.php?mode=gallery" working, right? What is the exact URL you have for that page?
    Now try with just "controller.php". That gives you the error you mentioned above? What is the exact URL you have for that page?
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    I got it done somehow. But thank you both for your help!
  26. #14
  27. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Do you know what the problem was?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    No, unfortunately not. I just passed my test, but I don't have a lot more information about it.

IMN logo majestic logo threadwatch logo seochat tools logo