(($thing))

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

(($thing))

Post by Mightywayne »

Hello there. Bit of a problem...

Code: Select all

if (($hardinjury > 50 && $montype == 'work' && $training == 'marbleworking') || ($hardinjury > 50 && $montype == 'work' && $training == 'factoryworking') || ($hardinjury > 50 && $montype == 'work' && $training == 'sailor') || ($hardinjury > 50 && $montype == 'work' && $training == 'civil') || ($hardinjury > 50 && $montype == 'work' && $training == 'doortodoor'))

What's happening is, even if those conditions are met, it's not doing what's after them. Well uh. That's all. I'm using PHP4.

I'm sure it's a simple thing but I just can't put my finger on it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I don't see anything wrong with your if statement, however it could be shortened to:

Code: Select all

$montype_possible = array('marbleworking', 'factoryworking', 'sailor', 'civil', 'doortodoor');

if (in_array($montype, $montype_possible) && $hardinjury > 50 && $montype == 'work')
{

}
try var_dump() on all your variables to make sure you are getting the expected values.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Ah dude perfect. Got it. :) Thanks.
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Okay I lied. :( I guess it wasn't working after-all. I suppose coding at night is just not something I should be doing after-all.

Well, here's what I have now, even though I hate posting my game's code...

Code: Select all

$montype_possible = array('marbleworking', 'factoryworking', 'sailor', 'civil', 'doortodoor');
	  
	  if (in_array($montype, $montype_possible) && $hardinjury > 30 && $montype != 'worker')
{
{
	  $injury = rand(1,2);
	  $injuryhours = $injury+=$injuryadd * 12;
	  $injuryadd = $injury += $injuryadd;
	  echo "<font color='red'><b>Injury Report:</b></font> Your monster has been injured for roughly $injuryhours hours, but has continued to work until the day was done.<br><br>";
	  }
}
What it's SUPPOSED to do is check if they are NOT a worker, and the variable $hardinjury is over 30, then it will injure the monster. I thought the problem was that I was just calling "work" as a type instead of "worker" (late night of coding y'see) but even if a working monster is chosen for the job, it won't do the if statement. I can't see what's wrong with it!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

var_dump() the $montype, $hardinjury, and $montype variables, and post what they return
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

Edit: Well that was insanely stupid. One moment, please, JCart.

Edit2: Okay... $training is a variable selected from the past page. It works correctly.

How would I turn the code you gave me:

Code: Select all

if (in_array($montype, $montype_possible) && $hardinjury > 50 && $montype == 'worker')
{
{
	  $injury = rand(1,2);
	  $injuryhours = $injury+=$injuryadd * 12;
	  $injuryadd = $injury += $injuryadd;
	  echo "<font color='red'><b>Injury Report:</b></font> Your monster has been injured for roughly $injuryhours hours, but has continued to work until the day was done.<br><br>";
	  }
}
To ALSO say that the variable $training had to be a certain value I put in? (IE: training would be "civil" for "civil duties")
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I'm not sure I understood exactly what you've just said, as what I got from it all you would need to do is add

Code: Select all

&& $training == 'civil'
But I take it I've misunderstood, care to elaborate?
Mightywayne
Forum Contributor
Posts: 237
Joined: Sat Dec 09, 2006 6:46 am

Post by Mightywayne »

No, no, I was just being a noob as usual.

But anyway. So I added the $training == 'civil', but still nothing! I dumped the variable and I got "evil" for mon type, which is correct... everything else is fine, too! I can't think of ANYTHING that could be wrong with this damn thing.

Is there a better way to structure it, you think?

Edit:

(my idea for structurizing - any good?)

if ($hardinjury > 30)
{
if ($montype != 'worker')
{}
}

I'll test tommorow. I need to get more sleep. Ach.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Not to be blunt, but I'm trying to help you.. so if I ask for something I think it is in your best interest to provide it. I don't like asking again and again.. you know? Proving a var_dump() of the variables would immediatly tell me whats not evaluating true in your if statement. Now back to being the nice guy..
Mightywayne wrote:I dumped the variable and I got "evil" for mon type, which is correct... everything else is fine, too! I can't think of ANYTHING that could be wrong with this damn thing.
If you look at your code, were checking if $montype is in the array $montype_possible.. I don't see 'evil' as one of the values.
Mightywayne wrote:Is there a better way to structure it, you think?
I'm not speaking on behalf of anyone, but I'm sure they'd agree nested if statements are usually a big no no.
Post Reply