Current location: Hot Scripts Forums » Programming Languages » PHP » problem using if statment


problem using if statment

Reply
  #1 (permalink)  
Old 04-29-07, 09:44 AM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Talking problem using if statment

Hi all. i am making 2 diffrent calles to a php script and inside that php script i use if statment to do diffrent things. Unfortently the way i coded the if statment the second else if statment never get executed. It always get ignore and first else if get excuted instead!!

I want for each diffrent calls ONLY one of else if statments get excuted. could any one tell me what i am doing wrong here.Thanks



First type of call for first else if:
Code:
http://localhost/script.php?cmd=file&file=visitorWantsToChat&site=20202020&channel=web&d=1177857195924&referrer=%28engage%29%20http%3A//localhost/1.html
Second type of call for second else if:
Code:
http://localhost/script.php?cmd=file&file=visitorWantsToChat&Pushed=true&site=20202020&channel=web&d=1177857103511&referrer=http%3A//localhost/1.html
script.php
PHP Code:

<?


$cmd   
$_GET['cmd'];
$file   $_GET['file'];
$Pushed   $_GET['Pushed'];

if (
$cmd=="dive")
{
// do this1
}
else if (
$cmd=="file" && $file=="visitorWantsToChat")
{
//do this2

}
else if (
$cmd=="file" && $file=="visitorWantsToChat" && $Pushed=="true")
{


//do this3

}

?>

Last edited by method; 04-29-07 at 09:59 AM.
Reply With Quote
  #2 (permalink)  
Old 04-29-07, 09:49 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It's a logical error. Try changing the order, like this:
PHP Code:



else if ($cmd=="file" && $file=="visitorWantsToChat" && $Pushed=="true")
{
//do this

}
else if (
$cmd=="file" && $file=="visitorWantsToChat"
Reply With Quote
  #3 (permalink)  
Old 04-29-07, 10:14 AM
method method is offline
Wannabe Coder
 
Join Date: Jul 2006
Posts: 228
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks . do you think i need any break statment for first else if to avoid the second else if statment from running when i call the script :

Code:
http://localhost/script.php?cmd=file&file=visitorWantsToChat&Pushed=true&site=20202020&channel

PHP Code:

else if ($cmd=="file" && $file=="visitorWantsToChat" && $Pushed=="true")

{
//do this

}
else if (
$cmd=="file" && $file=="visitorWantsToChat"
Reply With Quote
  #4 (permalink)  
Old 04-29-07, 10:22 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
No, only one of your if/else if/else statements will be executed if it's in the same group. There's no need to a break. It's just a question of logical order. Run the script in your mind and think how it will be executed. Think about which conditions are true and false at which point.
Reply With Quote
  #5 (permalink)  
Old 04-29-07, 07:19 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
I'd use a switch statement:

PHP Code:

switch ($cmd)

{
  case 
'dive':
    break;
  case 
'file':
    switch (
$visitor)
    {
       case 
'wantstoChat':
         if (
$pushed)
           
// pushed code
         
else
           
// non-pushed code
    
}
    break;

* this is pseudo PHP - it may have syntax errors
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem adding ", inside echo statment between diffrent fields method PHP 7 04-11-07 03:22 AM
login, roles problem dbrook007 ASP.NET 10 11-10-06 03:42 PM
Form Display Problem neevrap02 Visual Basic 1 09-05-06 05:18 AM
Cant figure out this problem. sql statment minority ASP 19 07-05-05 08:59 AM
Asp and Microsoft Access 2002 problem gop373 ASP 2 10-06-04 09:13 AM


All times are GMT -5. The time now is 06:02 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.