Hello everyone,

I have been trying to get this piece of code to work and havent been able to see why it isnt working!

The code is a training school page for my virtual pet site in which members should be able to go and choose which pet they want to teach a particular attack or defense move too! There are certain different species. The admin control panal which allows you to add, edit and delete attacks works fine but whenever you go to the page and try to buy an attack or defense move the page comes up with the elseif snippet which is:

This Attack is not available for this species of pets.
Go Back

As you can see in the code below that is one of the PHP elseif commands! I cant see why it isnt letting me. It doesnt seem to be able to identify the pet species and so says you are not able to teach that pet a move.


Any ideas? Any help is much appreciated!

<?php
$rank_check = 1;
$page_title = "Training Center";
include "header.inc.php";
echo $openHTML;
print "<p align='center'><b>Training Center</b></p>";

If ($_POST['attack_selected'] == 'yes'){
$res1 = mysql_query("SELECT * FROM user_pets2 WHERE id='".$_POST['pet_id']."' AND owner = '$userid' AND adoption = '0' AND game = '$game'");
$pet = mysql_fetch_assoc($res1);
$res2 = mysql_query("SELECT * FROM attacks2 WHERE id='".$_POST['attack_id']."' AND game= '$game'");
$attack = mysql_fetch_assoc($res2);
$res3 = mysql_query("SELECT * FROM members2 WHERE id='$userid'");
$user = mysql_fetch_assoc($res3);
$res4 = mysql_query("SELECT id, name FROM pets2 WHERE id='".$pet['species']."'");
$pet_sp = mysql_fetch_assoc($res4);
$res5 = mysql_query("SELECT * FROM bought_attacks WHERE attack_id='".$attack['id']."' AND user_pet='".$pet['id']."'");
$num = mysql_num_rows($res5);


$form = explode(", ", $attack['learned_by']);
$pet_species = strtolower($pet_sp['name']);

if ($user['points'] < $attack['attack_price']){
	echo "<p align='center'>You don't have enough munny to buy this attack.<br><a href='training.php?game=$game'>Go Back</a></p>";
	exit;
}elseif(!in_array($pet_species, $form)){
	echo "<p align='center'>This Attack is not available for this species of pets.<br><a href='training.php?game=$game'>Go Back</a></p>";
	exit;
}elseif($attack['learn_at_level'] > $pet['level']){
	echo "<p align='center'>You pet's level is not enough to teach this attack.<br><a href='training.php?game=$game'>Go Back</a></p>";
	exit;
}elseif($num != 0){
	echo "<p align='center'>This pet has already learnt this attack.<br><a href='training.php?game=$game'>Go Back</a></p>";
	exit;
}else{
	$del = mysql_query("UPDATE members2 SET points='points-".$attack['attack_price']."' WHERE id='user_id'");
	$_at = $attack['id'];
	$_pet = $pet['id'];
	$ins = mysql_query("INSERT INTO bought_attacks VALUES ('$_at', '$_pet')") or die (mysql_error());
	echo "<p align='center'>".$attack['attack_name']." has been succesfully bought and teached to Your Pet.<br><a href='training.php?game=$game'>Go Back</a></p>";
	exit;
}


	
}else{
echo "<p><center>Here is list of all Attacks You can teach Your Pets. Each Attack is for specified pets.</p><center><table>";
$ret = "<form action='' method='post'><select name='attack_id'>";
$res = mysql_query("SELECT * FROM attacks2 WHERE attack_price <> 0 AND attack_price <> '' AND game= '$game'");
while ($row = mysql_fetch_assoc($res)){
	$ret .= "<option value=".$row['id'].">".$row['attack_name']." -- ".$row['do_what']." -- ".$row['attack_price']." munny</option>";
}
$ret .= "</select><br>Which Pet? <select name = 'pet_id'>";
$res = mysql_query("SELECT * FROM user_pets2 WHERE owner = '$userid' AND adoption = '0' AND game = '$game'");

while ($row = mysql_fetch_assoc($res)){
$ret .= "<option value=".$row['id'].">".$row['name']."</option>";	
	
}
$ret .= "</select><br><input type='hidden' name='attack_selected' value='yes'><input type='submit' name='submit' value='Learn new Attack'></form>" ;
echo $ret;
}








?>

Try this and if the answer still isn't apparent, post the results.

<?php
$rank_check = 1;
$page_title = "Training Center";
include "header.inc.php";
echo $openHTML;
print "<p align='center'><b>Training Center</b></p>";

If ($_POST['attack_selected'] == 'yes'){
$res1 = mysql_query("SELECT * FROM user_pets2 WHERE id='".$_POST['pet_id']."' AND owner = '$userid' AND adoption = '0' AND game = '$game'");
$pet = mysql_fetch_assoc($res1);
$res2 = mysql_query("SELECT * FROM attacks2 WHERE id='".$_POST['attack_id']."' AND game= '$game'");
$attack = mysql_fetch_assoc($res2);
$res3 = mysql_query("SELECT * FROM members2 WHERE id='$userid'");
$user = mysql_fetch_assoc($res3);
$res4 = mysql_query("SELECT id, name FROM pets2 WHERE id='".$pet['species']."'");
$pet_sp = mysql_fetch_assoc($res4);
$res5 = mysql_query("SELECT * FROM bought_attacks WHERE attack_id='".$attack['id']."' AND user_pet='".$pet['id']."'");
$num = mysql_num_rows($res5);


$form = explode(", ", $attack['learned_by']);
$pet_species = strtolower($pet_sp['name']);

print_r($form);
echo "<br />" . $pet_species;
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.