Jump to content

[SOLVED] Gender Bender


danp

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/173054-solved-gender-bender/
Share on other sites

Seems it was missing the \' for an escape character  (dunno what happened there, had it earlier) :shrug:

 

here's what it is with the syntax error fixed :P

 

<?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";

?>

Link to comment
https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912140
Share on other sites

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";

?>

Link to comment
https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912159
Share on other sites

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

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912161
Share on other sites

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";}

Link to comment
https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912163
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/173054-solved-gender-bender/#findComment-912178
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.