Jump to content

[SOLVED] Problem with IF ELSE Statments.. Please help!Hel


robertmo116

Recommended Posts

Hello!

My name is Robert and I was suggested to come to PHPFreaks by one of my friends, so far I like! Well anyways, I am having a little problem.. I am runnning a case on a game i have made and I am wanting it to detect IF someone is in a gang then result in displaying the most recent 5 gang attacks - and IF not in a gang then display a group of 5 div tags with text on one of the div's saying Sorry your not in a gang. I almost found a solution for it - I got it to display the right results if not in a gang but an additional 5 div tags are for some reason running of what looks like 5 recent attacks in the game from random peeps.. this shouldnt do that as i am not in a gang.

 

Well if this is confusing I should be online for a while longer checking out the community but if you would like to get ahold of me hit me up on aim: robertmo116 or msn: robert@mobgamingnetwork.com

Thank you sooo much in advance.. I've been stuck on this problem for a few hours now and cant find a solution!  :'(

 

Well here is the Code!

<?php
case "1":
if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";
}
global $db,$ir,$c,$userid,$gangdata;
$atks=$db->query("SELECT a.*,u1.username as attackern,u1.gang as attacker_gang, u2.username as attackedn,u2.gang as attacked_gang FROM attacklogs a LEFT JOIN users u1 ON a.attacker=u1.userid LEFT JOIN users u2 ON a.attacked=u2.userid WHERE (u1.gang={$ir['gang']} OR u2.gang={$ir['gang']}) AND result='won' ORDER BY time DESC LIMIT 5");
while($r=$db->fetch_row($atks))
{
if($r['attacker_gang'] == $ir['gang'])
{ $color="green"; 
  } else { 
    $color="red"; 
}
$d=date('F j,',$r['time']);
echo "<div class='fitembgs'><a href='viewuser.php?u={$r['attacker']}'>{$r['attackern']}</a> <font color='$color'>attacked</font> <a href='viewuser.php?u={$r['attacked']}'>{$r['attackedn']}</a> on $d</div>";
}
$count=mysql_num_rows($atks);
if($count == 0) 
{ 
echo"<div class='fitembgs'>There hasnt been any attacks</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>"; 
}
$update= mysql_query("UPDATE `users` SET `toolbar`='1' WHERE `userid`='$ir[userid]'");
break;?>

 

(edited to change

tags to


tags)

Link to comment
Share on other sites

I'm sorry if I'm misunderstanding your problem as it's pretty cryptic but if you're not in a gang and your code is still running as if you are it seems to me that this is your problem:

 

if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";
}

 

What's the problem?  ...your if statement may correctly be recognizing $ir['gang'] == 0 designating you're not in a gang (i'm assuming) but after those five divs are written via the echo, your code continues to execute all lines of code below it because it looks like the "attack" code is not in the else conditional of being in a gang or not OR you're not using exit() if you're in a gang.  the exit(); method is a little terminal, final, or ungraceful (however u want to think of it) but it's one way to stop code from executing.

Link to comment
Share on other sites

I'm sorry if I'm misunderstanding your problem as it's pretty cryptic but if you're not in a gang and your code is still running as if you are it seems to me that this is your problem:

 

if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";
}

 

What's the problem?  ...your if statement may correctly be recognizing $ir['gang'] == 0 designating you're not in a gang (i'm assuming) but after those five divs are written via the echo, your code continues to execute all lines of code below it because it looks like the "attack" code is not in the else conditional of being in a gang or not OR you're not using exit() if you're in a gang.  the exit(); method is a little terminal, final, or ungraceful (however u want to think of it) but it's one way to stop code from executing.

Hmm I have tried adding the exit like the following;

 

<?php 
if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";
}
Exit();
?>

but this causes the rest of the script not to run.. its weird..

Link to comment
Share on other sites

i don't see an exit in that code...

 

i'm talking about something like:

 

Well here is the Code!
Code: [select]
<?php
case "1":
if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";
EXIT();
}
global $db,$ir,$c,$userid,$gangdata;
$atks=$db->query("SELECT a.*,u1.username as attackern,u1.gang as attacker_gang, u2.username as attackedn,u2.gang as attacked_gang FROM attacklogs a LEFT JOIN users u1 ON a.attacker=u1.userid LEFT JOIN users u2 ON a.attacked=u2.userid WHERE (u1.gang={$ir['gang']} OR u2.gang={$ir['gang']}) AND result='won' ORDER BY time DESC LIMIT 5");
while($r=$db->fetch_row($atks))
{
if($r['attacker_gang'] == $ir['gang'])
{ $color="green"; 
  } else { 
    $color="red"; 
}
$d=date('F j,',$r['time']);
echo "<div class='fitembgs'><a href='viewuser.php?u={$r['attacker']}'>{$r['attackern']}</a> <font color='$color'>attacked</font> <a href='viewuser.php?u={$r['attacked']}'>{$r['attackedn']}</a> on $d</div>";
...MORE CODE
}

 

OR

 

Well here is the Code!
Code: [select]
<?php
case "1":
if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";

} ELSE {
global $db,$ir,$c,$userid,$gangdata;
$atks=$db->query("SELECT a.*,u1.username as attackern,u1.gang as attacker_gang, u2.username as attackedn,u2.gang as attacked_gang FROM attacklogs a LEFT JOIN users u1 ON a.attacker=u1.userid LEFT JOIN users u2 ON a.attacked=u2.userid WHERE (u1.gang={$ir['gang']} OR u2.gang={$ir['gang']}) AND result='won' ORDER BY time DESC LIMIT 5");
while($r=$db->fetch_row($atks))
{
if($r['attacker_gang'] == $ir['gang'])
{ $color="green"; 
  } else { 
    $color="red"; 
}
$d=date('F j,',$r['time']);
echo "<div class='fitembgs'><a href='viewuser.php?u={$r['attacker']}'>{$r['attackern']}</a> <font color='$color'>attacked</font> <a href='viewuser.php?u={$r['attacked']}'>{$r['attackedn']}</a> on $d</div>";
...MORE CODE
} //END ELSE
}

 

Link to comment
Share on other sites

but this causes the rest of the script not to run.. its weird..

that's the entire point of the exit(); function -- to completely halt code execution.  that's why i said you should use ELSE conditional logic or some other method to only execute your "gang code" upon the appropriate conditions.

Link to comment
Share on other sites

i don't see an exit in that code...

 

i'm talking about something like:

 

Well here is the Code!
Code: [select]
<?php
case "1":
if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";
EXIT();
}
global $db,$ir,$c,$userid,$gangdata;
$atks=$db->query("SELECT a.*,u1.username as attackern,u1.gang as attacker_gang, u2.username as attackedn,u2.gang as attacked_gang FROM attacklogs a LEFT JOIN users u1 ON a.attacker=u1.userid LEFT JOIN users u2 ON a.attacked=u2.userid WHERE (u1.gang={$ir['gang']} OR u2.gang={$ir['gang']}) AND result='won' ORDER BY time DESC LIMIT 5");
while($r=$db->fetch_row($atks))
{
if($r['attacker_gang'] == $ir['gang'])
{ $color="green"; 
  } else { 
    $color="red"; 
}
$d=date('F j,',$r['time']);
echo "<div class='fitembgs'><a href='viewuser.php?u={$r['attacker']}'>{$r['attackern']}</a> <font color='$color'>attacked</font> <a href='viewuser.php?u={$r['attacked']}'>{$r['attackedn']}</a> on $d</div>";
...MORE CODE
}

 

OR

 

Well here is the Code!
Code: [select]
<?php
case "1":
if ($ir['gang'] == 0)
{
echo "<div class='fitembgs'>Sorry your not in a gang!</div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>
<div class='fitembgs'></div>";

} ELSE {
global $db,$ir,$c,$userid,$gangdata;
$atks=$db->query("SELECT a.*,u1.username as attackern,u1.gang as attacker_gang, u2.username as attackedn,u2.gang as attacked_gang FROM attacklogs a LEFT JOIN users u1 ON a.attacker=u1.userid LEFT JOIN users u2 ON a.attacked=u2.userid WHERE (u1.gang={$ir['gang']} OR u2.gang={$ir['gang']}) AND result='won' ORDER BY time DESC LIMIT 5");
while($r=$db->fetch_row($atks))
{
if($r['attacker_gang'] == $ir['gang'])
{ $color="green"; 
  } else { 
    $color="red"; 
}
$d=date('F j,',$r['time']);
echo "<div class='fitembgs'><a href='viewuser.php?u={$r['attacker']}'>{$r['attackern']}</a> <font color='$color'>attacked</font> <a href='viewuser.php?u={$r['attacked']}'>{$r['attackedn']}</a> on $d</div>";
...MORE CODE
} //END ELSE
}

 

THANK YOU!! Finally It Works! I appreciate it mate +1 to you!! the second option worked with a little editing

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.