PDA

View Full Version : How to "buy" adoptables?


kisazeky
03-26-2009, 01:44 AM
I just can't get this right: Checking if the user has enough money to adopt a certain creature.

How can I do this, if the amount of currency the user has is in the 'users' table, in the 'money' field, and the cost of the adoptable is located in the 'adoptables' table in the 'description' field?

BMR777
03-26-2009, 11:02 AM
You would have to get the info from both tables and store in variables. Then check if the money variable is greater than or equal to the currency in the users table. If it is, then deduct the currency and do the transaction. :)

kisazeky
03-26-2009, 03:13 PM
You would have to get the info from both tables and store in variables. Then check if the money variable is greater than or equal to the currency in the users table. If it is, then deduct the currency and do the transaction. :)


Yeah I tried to do that, but I can't get the code to replace the "name the adoptable" article content with "you can't afford this adoptable" if they don't have enough money.

It takes me to the name the adoptable page even if I in fact have less money than the adoptable cost.

This is the code I'm using:


}
else if ($isloggedin == "yes" and $aID != ""){
//A Logged In user is adopting a new creature

//Check first that the creature does in fact exist...
//Get adoptable info
$query = "SELECT * FROM adoptables WHERE uid = '$aID'";
$result = @mysql_query($query);
$num = @mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$newID=@mysql_result($result,$i,"uid");
$name=@mysql_result($result,$i,"name");
$imageurl=@mysql_result($result,$i,"imageurl");
$cost=@mysql_result($result,$i,"description");

$i++;
}
//Get how much money you have
$query = "SELECT * FROM `users` WHERE `username` = '$username'";
$result = mysql_query($query);
$num = mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$money =@mysql_result($result,$i,"money");
$i++;
}

if($newID != $aID){
$stop = 1;
$article_title = "Error";
$article_date = date('Y-m-d');
$article_content = "This Digimon is not available for adoption. <a href='adopt.php'>Go back</a>.";
}

//Can the user afford the adoptable?
if($money < $cost){
$article_title = "Oops";
$article_date = date('Y-m-d');
$article_content = "You can't afford this Digimon.";
}

else {
//Creature is valid, let's adopt the sucker...
//Procedure for giving the user's adoptable a name

if($cname == "" and $flag !=12){
$article_title = "Adopt ".$name;
$article_date = date('Y-m-d');
$article_content = "<p align='left'>You are about to adopt ".$name.". </p>
<p align='left'><img src='".$imageurl."' border='0'></a> </p>
<p align='left'>You may give your new Digimon a nickname. His or her name can contain letters, numbers and spaces.</p>
<form name='form1' method='get' action='adopt.php'>
<p>
<input name='aID' type='hidden' id='aID' value = '".$aID."'>
Nickname
<input name='cname' type='text' id='cname' size=20 maxlength='20'>
<input name='flag' type='hidden' id='flag' value = '12'>
</p>
<p>
<input type='submit' name='Submit' value='Adopt!'>
</p>
</form>";
}
}
}

else{

Here are what the tables being referenced look like:

http://i42.tinypic.com/65ub2h.jpg

http://i42.tinypic.com/2qwgaci.jpg

What is the problem?

BMR777
03-26-2009, 03:23 PM
I think this...

if($newID != $aID){
$stop = 1;
$article_title = "Error";
$article_date = date('Y-m-d');
$article_content = "This Digimon is not available for adoption. <a href='adopt.php'>Go back</a>.";
}

//Can the user afford the adoptable?
if($money < $cost){
$article_title = "Oops";
$article_date = date('Y-m-d');
$article_content = "You can't afford this Digimon.";
}

else {
//Creature is valid, let's adopt the sucker...
//Procedure for giving the user's adoptable a name

Needs to be:

if($newID != $aID){
$stop = 1;
$article_title = "Error";
$article_date = date('Y-m-d');
$article_content = "This Digimon is not available for adoption. <a href='adopt.php'>Go back</a>.";
}
else {
//Creature is valid, let's adopt the sucker...
//Procedure for giving the user's adoptable a name

//Can the user afford the adoptable?
if($money < $cost){
$article_title = "Oops";
$article_date = date('Y-m-d');
$article_content = "You can't afford this Digimon.";
}
else{

// The code to actually adopt the pet goes here...

} // End the else that processes the actual adoption

So, first we check that the pet is valid. Then we check that the user has enough money to adopt. If both those are true then we adopt the pet. :)

Try that.

kisazeky
03-26-2009, 09:41 PM
Nope, it's still not working.


//A Logged In user is adopting a new creature

//Check first that the creature does in fact exist...
$query = "SELECT * FROM adoptables WHERE uid = '$aID'";
$result = @mysql_query($query);
$num = @mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$newID=@mysql_result($result,$i,"uid");
$name=@mysql_result($result,$i,"name");
$imageurl=@mysql_result($result,$i,"imageurl");
$cost=@mysql_result($result,$i,"description");

$i++;
}

$query = "SELECT * FROM users WHERE uid = '$username'";
$result = @mysql_query($query);
$num = @mysql_numrows($result);

//Loop out code
$i=0;
while ($i < 1) {

$money=@mysql_result($result,$i,"money");

$i++;
}

if($newID != $aID){
$stop = 1;
$article_title = "Error";
$article_date = date('Y-m-d');
$article_content = "This Digimon is not available for adoption. <a href='adopt.php'>Go back</a>.";
}

else {
//Creature is valid, let's adopt the sucker...
//Procedure for giving the user's adoptable a name

//Can the user afford the adoptable?
if($money < $cost){
$article_title = "Oops";
$article_date = date('Y-m-d');
$article_content = "You can't afford this Digimon.";
}

else {
if($cname == "" and $flag !=12){
$article_title = "Adopt ".$name;
$article_date = date('Y-m-d');
$article_content = "<p align='left'>You are about to adopt ".$name.". </p>
<p align='left'><img src='".$imageurl."' border='0'></a> </p>
<p align='left'>You may give ".$money." your new Digimon a nickname. His or her name can contain letters, numbers and spaces.</p>
<form name='form1' method='get' action='adopt.php'>
<p>
<input name='aID' type='hidden' id='aID' value = '".$aID."'>
Nickname
<input name='cname' type='text' id='cname' size=20 maxlength='20'>
<input name='flag' type='hidden' id='flag' value = '12'>
</p>
<p>
<input type='submit' name='Submit' value='Adopt!'>
</p>
</form>";
}
}
}
}

else{

Edit: Alright, I totally ripped up your original adopt code and made it do something similar to managing your adoptables. That is, using adopt3.php?aID=aID&action=adopt instead of doing it all on the adopt page. Basically the adopt crap happens on adopt3.php instead of adopt.php.

zhiichiro
03-27-2009, 06:39 PM
WELL I HOPE WHEN IT'S DONE, I CAN MANAGE IT IN mysite.com/adm .... instead on phpmyadmin..

on the next few days my nose will bleed with those scripts.