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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    IF Statement based on SQL variables.

    Hi guys,

    I am a new poster to these forums but was hoping for a little help and guidance with the following problem.

    In my SQL database I have the standard "users" table with information such as username and password. I then also have other information such as "first_name" "last_name" and a text field called "credential". This credential field has (for example) values such as "#3##4##7#".

    So my question is how do I do an if statement that asks, "IF this user has $credential 7 or 8 THEN do something"?

    I'm struggling with this one. Here is an idea of what I am playing with :

    PHP Code:
    <?php
    $credential 
    = array("7","8");
    $validCredential $_SESSION['MM_UserGroup'];
    if(
    array_search($validCredential$credential)>-1){
    ?>
    <p> Text Goes here for user's with credentials 7 or 8 </p>
    <?php
    }
    ?>
    Am I way off basis here?

    Any help or direction is much apreciated!
    Thanks!
    Last edited by nuonical; 02-22-2010 at 06:29 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    <?php
    $credential = array("7","8");
    $validCredential = $_SESSION['MM_UserGroup'];
    if (in_array($validCredential, $credential)){
    ?>
    <p> Text Goes here for user's with credentials 7 or 8 </p>
    <?php
    }
    ?>

    Edit: Do you mean that your credential field literally will contain the following, (or similar), including the hashes text?

    #3##4##7#

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by MattF View Post
    Code:
    <?php
    $credential = array("7","8");
    $validCredential = $_SESSION['MM_UserGroup'];
    if (in_array($validCredential, $credential)){
    ?>
    <p> Text Goes here for user's with credentials 7 or 8 </p>
    <?php
    }
    ?>

    Edit: Do you mean that your credential field literally will contain the following, (or similar), including the hashes text?

    #3##4##7#
    Yes. The field (in phpMyAdmin) will literally have the hashmarks. So if the user has credentials 9,10,12,etc. then the field in phpMyAdmin looks like this : "#9##10##12#", etc. (no quotes).


  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Change this then:

    Code:
    $validCredential = $_SESSION['MM_UserGroup'];
    to, (untested, btw):

    Code:
    $validCredential = preg_replace('/#+/', '#', $_SESSION['MM_UserGroup']);
    $validCredential = explode('#', $validCredential);

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Can't seem to get that to work. I assume it has something to do with the preg_replace() function.

    It isn't throwing an error, however.

    So this is the code I have :

    PHP Code:
    <?php
                        $admin 
    = array("109""110");                    
                        
    $validAdmin preg_replace('/#+/''#'$_SESSION['MM_UserGroup']);                
                        
    $validAdmin explode('#'$validAdmin);
                        if (
    in_array($validAdmin$admin)){
                        
                    
    ?>
                    <p> text for users with $admin = "109" or "110" goes here.</p>
                    <?php
                    
    }
                    
    ?>
    Some of my users have 109 added to their "admin" field, like so:


    I'm struggling with this preg_replace() function. Perhaps I should explicitly define the replacements? For example :
    PHP Code:
    $patterns = array();
    $patterns[0] = '/##/';
    $patterns[1] = '/#/';
    $replacements = array();
    $replacements[1] = ' ';
    $replacements[1] = ' '
    Should I be replacing the #'s with space's? Is that the purpose of using preg_replace in this instance?

    Thanks again. This is a huge help for me and very appreciated.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Give this a whirl:

    Code:
    <?php
    
    $admins = array('109', '110');
    
    $validAdmin = preg_replace(array('/#+/', '/^#+/', '/#+$/'), array('#', '', ''), $_SESSION['MM_UserGroup']);
    $validAdmin = explode('#', $validAdmin);
    
    foreach ($admins as $admin)
    {
            if (in_array($admin, $validAdmin))
            {
                    print($admin."\n");
                    break;
            }
    }
    
    ?>
    Last edited by MattF; 02-23-2010 at 02:24 AM.

  • #7
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Nothing I'm afraid. I'm banging my head against the desk on this one.

    So, here's the thing.. If I try to make it even simpler, to see if the value works,
    it still won't work.

    I've tried this :
    PHP Code:
    <?php
                        $phone 
    = array("555-555-5555");    
                        
    $validPhone $_SESSION['MM_UserGroup'];                                    
                        if(
    array_search($phone$validPhone)){
                        
                    
    ?>
                    <p> If 555 number then you will see this</P>
                    <?php
                    
    }
                    
    ?>
    Now, I know there is a user with this phone number in the "phone" column, but this code won't work.. Which leads me to believe that the "$phone" variable above isn't associate with the 'phone' variable from the "users" section of my database. Perhaps, in this example, I can set the phone variable in my database to a session variable and use that? I can use
    PHP Code:
     if ($_SESSION['username'] == "admin"
    all day long and that works fine. But I believe it is only a session variable because it was posted when the admin logged in.

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $validPhone = $_SESSION['MM_UserGroup'];
    I doubt the MM_UserGroup var contains their telephone number? Also, this is a string:

    Code:
    $phone = array("555-555-5555");
    You would use preg_match or strpos for matching that.

    Post your code as it is at the moment, (incorporating the changes you've made). That sample code I posted in my last post works fine, so you have an error somewhere else. Are you sure MM_UserGroup is set correctly?

  • Users who have thanked MattF for this post:

    nuonical (02-24-2010)

  • #9
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    7
    Thanks
    1
    Thanked 2 Times in 2 Posts

    resolved

    No, I don't believe MM_UserGroup is set correctly at this point.. Is there a way of doing this without using this MM_UserGroup Value? Perhaps by querying the sql database?
    Last edited by nuonical; 02-24-2010 at 07:13 PM. Reason: resolved


  •  

    Tags for this Thread

    Posting Permissions

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