Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

help with code

  • 17-11-2010 11:12AM
    #1
    Registered Users, Registered Users 2 Posts: 287 ✭✭


    <?
    $CategoryID = $_GET;
    function get_param($param_name)
    {
    $param_value = "";
    if(isset($_POST[$param_name]))
    {$param_value = $_POST[$param_name];}
    else if(isset($_GET[$param_name]))
    {$param_value = $_GET[$param_name];}
    return $param_value;
    }
    ?>

    im using this code someone else help me write but not sure how it works
    can someone give me an idea how it works thanks


Comments

  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    $CategoryID is getting the CATEGORY ID, i assume from the URL

    if paramname is posted [form?] set the paramvalue to the name
    else get it from the url?


    not giving much info here, codes as good as drivel to the reader


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    Keewee6 wrote: »
    <?
    $CategoryID = $_GET;
    function get_param($param_name)
    {
    $param_value = "";
    if(isset($_POST[$param_name]))
    {$param_value = $_POST[$param_name];}
    else if(isset($_GET[$param_name]))
    {$param_value = $_GET[$param_name];}
    return $param_value;
    }
    ?>

    im using this code someone else help me write but not sure how it works
    can someone give me an idea how it works thanks


    the code works i didnt write it though (i got help) and i was wonderin could someone explain it - thanks


  • Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭Placebo


    you cant just paste a snippet, i tried explaining it above

    thats like posting a line from a book and asking what a line means, in what context ?


  • Registered Users, Registered Users 2 Posts: 1,771 ✭✭✭jebuz


    Ok to break it down for you quickly...
    <?
    $CategoryID = $_GET['CategoryID'];
    

    The $_GET function is used to collect values in a form where the method is "get", so in this case it's looking for a "CategoryID" value in a form that was submitted in the previous page. It's then assigning that value to a variable called $CategoryID (variables should begin in lowercase!!)
    function get_param($param_name) {
    

    Easy enough, declaring a function called get_param, and passes in a value $param_name
    $param_value = "";
    
    Declaring a variable to store the result, this will be returned at the end of the function.
    if(isset($_POST[$param_name])) {
      $param_value = $_POST[$param_name];
    }
    

    An "if" block which checks if the parameter $param_name (value of whatever passed into the function) exists, and if so, assign that value to $param_value which will be eventually returned.

    The built-in $_POST function is used to collect values from a form sent where the method is "post".
    else if(isset($_GET[$param_name])) {
      $param_value = $_GET[$param_name];
    }
    

    If the parameter is not found using the $_POST function in the if block, we are trying to see if it exists for the $_GET function (explained at the top). If it is found, again assign the value of the parameter to the $param_value variable.

    "post" and "get" are basically 2 different methods of passing data via a form, get sends the paramters in the URL. See here for more.
    return $param_value;
    }
    ?>
    

    Finally, return the $param_value, if the parameter you are looking for was not passed into the form at all, the value would be "".

    I don't know in what context you are using this because it's basically a function definition and you haven't shown any code where it is being invoked.

    The first line doesn't make much sense either, since you have gone to the trouble of creating a function to get params from a form and not then using it, this should be:
    $CategoryID = get_param('CategoryID');
    


  • Registered Users, Registered Users 2 Posts: 287 ✭✭Keewee6


    thanks


  • Advertisement
  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    jebuz wrote: »
    The $_GET function is used to collect values in a form where the method is "get"......

    .......or where the link just has a parameter inbuilt into it, e.g.

    page.php?CategoryID=22


Advertisement