Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

sprintf PHP

  • 23-09-2009 8:50am
    #1
    Registered Users, Registered Users 2 Posts: 8,070 ✭✭✭


    [PHP]if (isset($_GET)) {
    $id = sprintf('%d',$_GET);
    } else {
    $id = 0;
    }

    $addcomparison = 0;
    $access = accessLevel('statsoverview');
    #print "Access Level: $access";
    switch ($access) {
    case 'owner':
    $subselect = ' and owner = ' . $_SESSION["logindetails"]["id"];
    if ($id) {
    $allow = Sql_Fetch_Row_query(sprintf('select owner from %s where id = %d %s',$GLOBALS,$id,$subselect));
    if ($allow[0] != $_SESSION["logindetails"]["id"]) {
    print $GLOBALS->get('You do not have access to this page');
    return;
    }
    }
    $addcomparison = 1;
    break;
    case 'all':
    $subselect = '';
    break;
    case 'none':
    default:
    $subselect = ' where id = 0';
    print $GLOBALS->get('You do not have access to this page');
    return;
    break;
    }[/PHP]


    Dont get bits of this code,
    $id = sprintf('%d',$_GET); //setting $id as a digit, from a session etc?

    $allow = Sql_Fetch_Row_query(sprintf('select owner from %s where id = %d %s',$GLOBALS,$id,$subselect)); //Whats going on here, select owner from %s, WTF is %s? where id = %d AND %s ? and whats this doing ,$GLOBALS,$id,$subselect


    Thanks :confused:


    doesnt matter, found a good explanation here,
    http://www.w3schools.com/PHP/func_string_sprintf.asp

    although why use such a method?
    still dont get 'where id = %d %s'


Comments

  • Registered Users, Registered Users 2 Posts: 2,699 ✭✭✭samhail


    the GET function retrieves info from the GET request from a form
    http://www.w3schools.com/PHP/php_get.asp

    2nd query is basic c printf stuff dude :)...
    The % variables (as such) are replaced with the paramters after the string.

    The %s refers to $GLOBALS
    The %d refers to $id
    The last %s refers to $subselect
    sprintf('select owner from %s where id = %d %s'
    ,$GLOBALS,
    $id,
    $subselect));


    so in effect its going to print out:
    'select owner from $GLOBALS where id = $id $subselect


  • Registered Users, Registered Users 2 Posts: 2,699 ✭✭✭samhail


    Placebo wrote: »

    doesnt matter, found a good explanation here,
    http://www.w3schools.com/PHP/func_string_sprintf.asp

    although why use such a method?
    still dont get 'where id = %d %s'

    thats the method they use :) c++ printing is slightly nicer.

    Its going to produce the select statement searching for if = $GLOBALS... what ever that comes out to


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


    thanks guy, i get all that, whats the point of doing it this way ?


  • Registered Users, Registered Users 2 Posts: 2,699 ✭✭✭samhail


    errr i dont know :)
    thats the way they do it ?


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Once you get used to how to read it, printf can actually be a much cleaner way of writing long strings with many variables. You can get the entire string as you would read it, without needing to break the flow with concatenations (which in my experience nearly always results in a game of "find the missing or misplaced quote mark").


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


    +1 on aidan's post; much easier to change the code afterwards as long as there aren't too many % placeholders


Advertisement