Forum Moderators: coopster

Message Too Old, No Replies

How to check is a value is present.

         

capulet_x

5:03 am on Apr 25, 2007 (gmt 0)

10+ Year Member



How to check IF a value is present, that is.....

I need a little help and I know that what I'm trying to do is among the more simple functions. As my query illustrates below I am trying to check if a variable is present if it isn't I want to echo to that effect. If the value is present and a second condition is also met I want a different echo returned.

$select = mysql_query("SELECT friend, chat FROM chat WHERE friend = '$friend' AND chat = ''");

if (friend == ""){
echo "text=". "{$friend} is offline. ";
}

if (chat == ""){
echo "text=". "{$friend} is online! ";
}else{
echo "text=". "{$friend} is unavailable. ";
}

The problem is I keep getting "friend" is unavailable when I think I should be getting "friend" is offline (I know, I know it amounts to the same thing but I need to distinguish the two)

capulet_x

6:00 am on Apr 25, 2007 (gmt 0)

10+ Year Member



I tried doing this too at no avail.

if (friend=="$friend" ¦¦ chat == ""){
echo "text=". "{$friend} is online! ";
}else{
echo "text=". "{$friend} is unavailable. ";
}

cameraman

6:06 am on Apr 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Take a look at isset() [us.php.net]
I don't know if you're typing or pasting from your code, but:
if (friend == ""){

should be
if ($friend == ""){

and same with chat

capulet_x

6:20 am on Apr 25, 2007 (gmt 0)

10+ Year Member



"$friend" is also the name of my variable...will that mix things up? Or maybe...is my varible distinguished from the query result by using quotes? I'm looking into isset now.

capulet_x

7:13 am on Apr 25, 2007 (gmt 0)

10+ Year Member



I researched isset and I am getting closer to fixing my problem except for one thing:

if (isset($chat)){
echo "text=". "{$friend} is unavailable. ";
}

Even though there is no value for $chat I still get it's echo statement. Could this have anything to do with how the table row is set in the db? Do I have to have a value of NULL in order to get FALSE as a return for the above statement?

cameraman

11:26 am on Apr 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think I led you down the wrong path - isset() checks to see if the variable is defined. To see if the variable has a value, your statements were correct except for the missing $.

Where do $friend and $chat come from?

The way your query is structured, you'll only get rows that match $friend and have no value in chat. If there's no row with that $friend name OR there is a row but chat is filled in, no rows will be returned.

Are these correct?
Every 'friend' who's online has a row in the table.
If the person is already chatting, the database field 'chat' will contain a value.
If the person is not chatting, the database field 'chat' will be empty.
If the person is not online, no row will be in the table with that friend name

If those four are all correct, see if this makes sense:
$select = mysql_query("SELECT friend, chat FROM chat WHERE friend = '$friend'");
if(mysql_num_rows($select) {
$data = mysql_fetch_assoc($select);
if($data['chat'] == '') {
echo $friend . ' is online! ';
} // EndIf chat is empty
else {
echo $friend . ' is unavailable. ';
} // EndElse chatting
mysql_free_result($select);
} // EndIf have row
else {
echo $friend . ' is offline. ';
} // EndElse don't have row

By dropping chat out of the selection criteria you get a row back if the person is online, then you can check to see whether or not the friend is chatting. If you don't get a row, then it can only be because of one reason - no friend by that name in the table. Yours would work but you'd have to do two queries to determine whether there was no row returned because of no friend or because the friend is chatting.

jatar_k

12:30 pm on Apr 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



also look at
[php.net...]

capulet_x

7:53 am on Apr 27, 2007 (gmt 0)

10+ Year Member



Cameraman and Jatar, I want to thank you both for your input.
Cameraman, it was a problem with the varible not setting correctly. Actually the reference to isset did help me out and thanks for putting that script together I saw it a bit late and had put together something not as elegant but it worked.
Anyway, thanks again.
I'm learning a lot here.