...

View Full Version : IF Statement based on SQL variables.



nuonical
02-22-2010, 06:09 PM
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
$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!

MattF
02-22-2010, 06:12 PM
<?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#

nuonical
02-22-2010, 06:35 PM
<?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).

http://www.veggiemeet.com/credentials.JPG

MattF
02-22-2010, 06:41 PM
Change this then:



$validCredential = $_SESSION['MM_UserGroup'];


to, (untested, btw):



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

nuonical
02-23-2010, 01:05 AM
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
$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:
http://www.veggiemeet.com/screen.JPG

I'm struggling with this preg_replace() function. Perhaps I should explicitly define the replacements? For example :


$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.

MattF
02-23-2010, 02:02 AM
Give this a whirl:



<?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;
}
}

?>

nuonical
02-23-2010, 02:34 AM
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
$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

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.

MattF
02-23-2010, 02:55 AM
$validPhone = $_SESSION['MM_UserGroup'];


I doubt the MM_UserGroup var contains their telephone number? Also, this is a string:


$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?

nuonical
02-23-2010, 03:35 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum