danp Posted September 4, 2009 Share Posted September 4, 2009 I really appreciate all the help you guys have given me in the past. Today's conundrum is as follows... My scripts grabs the "Gender" entry from a table called "Users", and depending if it's "Male" or "Female" it can make a variable say either "his" or "her" for use in a sentence. The same below it works similar, but it's "he" and "she". It's been stumping me, and right now for test purposes it displays "Female" then "his" and "he"... totally incorrect below is the code <?php require 'connect.php'; include 'header.php'; include 'headin.php'; if(!isset($_COOKIE['member_id'])) die('You're Not logged in, please <a href=index.php>go back</a>'); $Player=$_COOKIE['member_id']; $Query="SELECT * from Users where ID='$Player'"; $Query2=mysql_query($Query) or die("Could not get user stats"); $User=mysql_fetch_array($Query2); if($_COOKIE['password'] !=$User['Password']) die('It seems you have an incorrect password! please <a href=index.php>go back</a>'); $gender = "$User[Gender]"; if ($gender = "Male"){$gender = "his";} elseif ($gender = "Female"){$gender = "her";} else ($gender = "their"); $gender2 = "$User[Gender]"; if ($gender2 = "Male"){$gender2 = "he";} elseif ($gender2 = "Female"){$gender2 = "she";} else ($gender2 = "they"); echo "$User[Gender]<br>"; echo "$gender"; echo "<br>"; echo "$gender2"; ?> Any help is appreciated! Thank you Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/ Share on other sites More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 Is this your actual code? Because it has syntax errors and shouldn't be executing at all. Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912134 Share on other sites More sharing options...
danp Posted September 4, 2009 Author Share Posted September 4, 2009 Seems it was missing the \' for an escape character (dunno what happened there, had it earlier) here's what it is with the syntax error fixed <?php require 'connect.php'; include 'header.php'; include 'headin.php'; if(!isset($_COOKIE['member_id'])) die('You\'re Not logged in, please <a href=index.php>go back</a>'); $Player=$_COOKIE['member_id']; $Query="SELECT * from Users where ID='$Player'"; $Query2=mysql_query($Query) or die("Could not get user stats"); $User=mysql_fetch_array($Query2); if($_COOKIE['password'] !=$User['Password']) die('It seems you have an incorrect password! please <a href=index.php>go back</a>'); $gender = "$User[Gender]"; if ($gender = "Male"){$gender = "his";} elseif ($gender = "Female"){$gender = "her";} else ($gender = "their"); $gender2 = "$User[Gender]"; if ($gender2 = "Male"){$gender2 = "he";} elseif ($gender2 = "Female"){$gender2 = "she";} else ($gender2 = "they"); echo "$User[Gender]<br>"; echo "$gender"; echo "<br>"; echo "$gender2"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912140 Share on other sites More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 The comparison operator is == not = Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912141 Share on other sites More sharing options...
danp Posted September 4, 2009 Author Share Posted September 4, 2009 Hmm... any tips on what I would do to get this functioning? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912145 Share on other sites More sharing options...
Philip Posted September 4, 2009 Share Posted September 4, 2009 The comparison operator is == not = Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912146 Share on other sites More sharing options...
danp Posted September 4, 2009 Author Share Posted September 4, 2009 I tried replacing the "=" with "==" in the if statements.... just got back "Male" 3 times... thanks guys.... Now where would they need to go to get this to actually work? Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912157 Share on other sites More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912158 Share on other sites More sharing options...
danp Posted September 4, 2009 Author Share Posted September 4, 2009 Sure it's <?php require 'connect.php'; include 'header.php'; include 'headin.php'; if(!isset($_COOKIE['member_id'])) die('You\'re Not logged in, please <a href=index.php>go back</a>'); $Player=$_COOKIE['member_id']; $Query="SELECT * from Users where ID='$Player'"; $Query2=mysql_query($Query) or die("Could not get user stats"); $User=mysql_fetch_array($Query2); if($_COOKIE['password'] !=$User['Password']) die('It seems you have an incorrect password! please <a href=index.php>go back</a>'); $gender = "$User[Gender]"; if ($gender = "Male"){$gender == "his";} elseif ($gender == "Female"){$gender == "her";} else ($gender == "their"); $gender2 = "$User[Gender]"; if ($gender2 == "Male"){$gender2 == "he";} elseif ($gender2 == "Female"){$gender2 == "she";} else ($gender2 == "they"); echo "$User[Gender]<br>"; echo "$gender"; echo "<br>"; echo "$gender2"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912159 Share on other sites More sharing options...
Philip Posted September 4, 2009 Share Posted September 4, 2009 Is your data in the database 'Female' and 'Male' and not 'female' and 'male' (or something similar)? Just a quick reformat of how I'd format your current code. <?php require 'connect.php'; include 'header.php'; include 'headin.php'; if(!isset($_COOKIE['member_id'])) die('You\'re Not logged in, please <a href="index.php">go back</a>'); // Escape the ID - remember I could change a cookie and could potential exploit the query $Player = mysql_real_escape_string($_COOKIE['member_id']); $Query = "SELECT * from Users where ID='$Player'"; $Query2 = mysql_query($Query) or die("Could not get user stats"); $User = mysql_fetch_assoc($Query2); // I never recommend placing the password (even hashed/encrypted) into a cookie. if($_COOKIE['password'] !=$User['Password']) die('It seems you have an incorrect password! please <a href="index.php">go back</a>'); // I'd change it to the following switch statement, cleaner code // and using an array - its just a lot cleaner IMO switch($User['Gender']) { case 'Male': $gender['p'] = 'his'; $gender['s'] = 'he'; break; case 'Female': $gender['p'] = 'hers'; $gender['s'] = 'she'; break; default: $gender['p'] = 'theirs'; $gender['s'] = 'they'; break; } echo $User['Gender'].'<br>'; echo $gender['s']; // singluar echo '<br>'; echo $gender['p']; // plural ?> Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912161 Share on other sites More sharing options...
Philip Posted September 4, 2009 Share Posted September 4, 2009 Sure it's <?php require 'connect.php'; include 'header.php'; include 'headin.php'; if(!isset($_COOKIE['member_id'])) die('You\'re Not logged in, please <a href=index.php>go back</a>'); $Player=$_COOKIE['member_id']; $Query="SELECT * from Users where ID='$Player'"; $Query2=mysql_query($Query) or die("Could not get user stats"); $User=mysql_fetch_array($Query2); if($_COOKIE['password'] !=$User['Password']) die('It seems you have an incorrect password! please <a href=index.php>go back</a>'); $gender = "$User[Gender]"; if ($gender = "Male"){$gender == "his";} elseif ($gender == "Female"){$gender == "her";} else ($gender == "their"); $gender2 = "$User[Gender]"; if ($gender2 == "Male"){$gender2 == "he";} elseif ($gender2 == "Female"){$gender2 == "she";} else ($gender2 == "they"); echo "$User[Gender]<br>"; echo "$gender"; echo "<br>"; echo "$gender2"; ?> if ($gender = "Male"){$gender == "his";} Your first if statement still has just one '=', looks like you got the 2 mixed up. Try: if ($gender == "Male"){$gender = "his";} Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912163 Share on other sites More sharing options...
Garethp Posted September 4, 2009 Share Posted September 4, 2009 You only use == in the if operators. It's like this When you want to compare something to something else, you use == if you want to set something to something else, you use = Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912164 Share on other sites More sharing options...
danp Posted September 4, 2009 Author Share Posted September 4, 2009 Total facepalm moment.... thanks guys -Dan Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912175 Share on other sites More sharing options...
anatak Posted September 4, 2009 Share Posted September 4, 2009 in your if statement if($gender=male) what you just did is that you gave the variable $gender the value male so you $gender variable will have the value male no matter what. if($gender==male) what this does is check if the value of the variable $gender equals male the $gender variable will have the value that it had before you ran the if statement Quote Link to comment https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.