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 4 of 4

Thread: Arrays

  1. #1
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts

    Arrays

    Quick question which I'm sure has a quick answer

    Here is my code:

    PHP Code:
    // ADMIN LOG - LIST CHANGES    
    $log_entry = array("alias","email","matcheswon","matcheslost","tourneyswon","tourneyslost","tourneysentered","tourneys_1st","tourneys_hosted");

    foreach(
    $log_entry as $recall) {
         if (
    $_POST[$recall] != $uinfo[$recall]) { 
              
    admin_log("change $recall",$uinfo[id],$_COOKIE[a_uid],"Change $recall from $uinfo[$recall] to $_POST[$recall]",$date); 
         }

    Okay - problem is.. once the foreach reaches "matcheswon", ",matcheslost" etc... I want this it to appear as "Matches Won" and "Matches Lost" after the word Change.

    The solution I have is this.. but I'm sure there is a better way:

    PHP Code:
    // ADMIN LOG - LIST CHANGES    
    $log_entry = array("alias","email","matcheswon","matcheslost","tourneyswon","tourneyslost","tourneysentered","tourneys_1st","tourneys_hosted");

    foreach(
    $log_entry as $recall) {
         if (
    $recall == "matcheswon") {
         
    $sub "Matches Won";
         }
         if (
    $_POST[$recall] != $uinfo[$recall]) { 
              
    admin_log("change $recall",$uinfo[id],$_COOKIE[a_uid],"Change $sub from $uinfo[$recall] to $_POST[$recall]",$date); 
         }

    Doing it this way would mean I'd have to put in this secion:
    if ($recall == "matcheswon") {
    $sub = "Matches Won";
    }

    Once each, for "matcheswon", "matcheslost", "tourneyswon", "tourneyslost", "tourneysentered", "tourneys_1st", "tourneys_hosted"

    Can anyone suggest a better way?

    Thanks in advance!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yes, create an associative array with your 'search' I guess it would be:
    PHP Code:
     // ADMIN LOG - LIST CHANGES     
    $log_entry = array(
         
    'alias' => 'alias',
         
    'email' => 'email',
         
    'matcheswon' => 'Matches Won',
         
    'matcheslost' => 'Matches Lost',
         
    'tourneyswon' => 'Tourneys Won',
         
    'tourneyslost' => 'Tourneys Lost',
         
    'tourneysentered' => 'Tourneys Entered',
         
    'tourneys_1st' => 'Tourneys 1st',
         
    'tourneys_hosted' => 'Tourneys Hosted'
    ); 

    foreach(
    $log_entry as $recall_key => $recall_val) { 
         if (
    $_POST[$recall_key] != $uinfo[$recall_key]) {  
              
    admin_log("change $recall_key",$uinfo[id],$_COOKIE[a_uid],"Change $recall_val from $uinfo[$recall_key] to $_POST[$recall_key]",$date);  
         } 

    Unstested for your purposes.
    You can also generate this array dynamically from a database if you put it together correctly, and if you desire, it can be altered to drop the secondary values off of unnessessary elements, such as the email, where the key = value (Bit of modification for that).
    But I think this is probably the easiest way.

    Oh, BTW, $_COOKIES are your enemy. Use $_SESSION instead.

    Edit:
    Ugh, I know I got some of these recall_key and recall_val's in the wrong places, but just play with it and it will work out ok. I recommend not storing database entries or text entries until your output desired has been reached.
    Last edited by Fou-Lu; 07-11-2005 at 07:29 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    Thank you! I knew there had to be an easier way. Works perfectly.. and I actually understand why, so will come in handy for future.

    RE: $_COOKIE:

    Are you saying this because some people have cookies disabled? As I have listed on the site when people sign up saying they must have cookies enabled. I don't quite get why people do disable them.

    And if I was to use $_SESSION in replace, are you refering to where I have to put session_start() at the start of each page?

    I'm not too knowledgable on sessions

    Thanks in advance!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yessir, thats correct.
    My reason for cookies is soley due to the fact they are stored client side, meaning that they can be altered in any way the client sees fit.
    Sessions are simple, you just need to make sure of a couple of things:
    If you can access your php.ini, make sure that these directives are set:
    session.use_cookies = 1;
    session.use_only_cookies = 0;
    sessions.use_trans_sid = 1;
    Or with script execution:
    ini_set('session.use_cookies', 1);
    ini_set('session.use_only_cookies', 0);
    ini_set('session.use_trans_sid', 1);
    Now, I give you these configurations because they do not force your clients to have their cookies set to on. If you would like to do that, set your use_only_cookies = 1, and your use_trans_sid = 0.
    Then use session_start() at the top of your page. I would recommend grabbing and storing your users ipadress as well as their user_agent for validation.
    If you need an example, it will take me a little while to throw it together for you, I haven't used sessions for a long time myself, I rely on database driven sessions instead.
    I recall php.ca has an excellent link on their site from the session main page though with security techniques...


  •  

    Posting Permissions

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