Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,175 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,208 people online right now. Registration is fast and FREE... Join Now!




If statement not working

 
Reply to this topicStart new topic

If statement not working, Problems with $row ??

Sayid Ahmed
27 Aug, 2008 - 08:33 AM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
Hello,

I'm practicing PHP & mysql by making a small text based game (what else?). In my table 'users' there's a column called 'state' which is basically the player's type of government. I have a page called 'soldiers.php' where the player trains soldiers, if they have a government set to 'Police State' then the cost is 2.5 per soldier, otherwise, it's 3.

I made this code, to me it looks correct, but i've noticed it is always set to 2.5, regardless of what value $row['state'] is:

php

<?php
$user = $_SESSION['username']; // session for their username
$result = mysql_query("SELECT * FROM users WHERE username='$user'") or die(mysql_error());
$row = mysql_fetch_array($result);

$money = $row['money']; // how much money they have
$state = $row['state']; // what government they have
if ($state = 'Police State') {
$cost = 2.5; // reduced cost for specific government
} else {
$cost = 3; // normal cost for everything else
}
echo '<a class="style1">You have '.$money.' dinars. It costs '.$cost.' dinars per soldier.'; // cost is always displayed as 2.5 for some reason
?>


Thank you

EDIT: did a bit of thinking - do if statements not work on setting values for variables?

This post has been edited by Sayid Ahmed: 27 Aug, 2008 - 08:40 AM
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: If Statement Not Working
27 Aug, 2008 - 08:47 AM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
you need to use ==, not =

you're setting $row equal to Police State, not checking it...

This post has been edited by JBrace1990: 27 Aug, 2008 - 08:48 AM
User is offlineProfile CardPM
+Quote Post

Sayid Ahmed
RE: If Statement Not Working
27 Aug, 2008 - 08:52 AM
Post #3

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 29


My Contributions
oh dear is that it? God, i feel schoopid now

thanks
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: If Statement Not Working
27 Aug, 2008 - 08:54 AM
Post #4

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
lol... we all do it sooner or later, don't worry =)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:32AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month