Jump to content

input and select from a form dropdown menu


ferrin

Recommended Posts

i'm making a game with little pets and you can breed the pets using a form. i'd post code, but at this point my code is so packed with errors that i probably need to just completely rewrite it. i have no clue how to go about this and i've tried everything for two days now, including googling to death.

 

this is what i'm trying to do:

 

create a form that shows a list of the females the user owns, and the males the user owns. you must select a female from the females, and a male from the males, using a simple form like so:

 

Select a female:

(dropdown menu listing all females user has that are able to breed)

 

Select a male:

(dropdown menu listing all males user has that are able to breed)

 

Submit button

 

then the script checks the database to see if the pet is able to breed, meaning that the column 'breed' is not set to 'breeding'. a pet that is currently breeding is unable to breed for a random number of days.

 

if the pet is able to breed, then the value of 'breeding' is added to the 'breed' part of the database.

 

the database looks like this:

http://i44.tinypic.com/5ul3tl.jpg

 

below the form is a table showing pets currently breeding.

 

i have tried -everything-. i have so many errors that it's just ridiculous to try and go about posting my code as is. if you -really- want to see it, i'll post it.

 

i'm not really asking someone to just write this up for me, because i know that's lame to do, but i need some serious help here, if anyone would be so kind.

Link to comment
Share on other sites

i decided to post my code, since i really don't want people to think i'm trying to get freebies. but i realize it sucks... i've only been doing php for about two weeks now.

 

it isn't showing ANYTHING in the female or male form when i try to load it. you can see the test page here:  http://fiendfashion.biz/ferrin/test.php

 

on top of it not showing anything, i can't figure out how to make it only save the breeding 'yes' to the selected pet. i was planning to use the pet 'id' but i don't want it to show to the user.

 

<?php
session_start();

$username= $_SESSION['username'];

// --------------check if form has been filled
//
echo "$username<br>";
if ( $_POST['male'] && $_POST['female'])
{
//
$male = $_POST['male'];
$female = $_POST['female'];
$id = $_POST['id'];
//

echo "Female selected: $female<br />";
echo "Male selected: $male<br />";
echo "id of female: $id<br><br>";
//
//
$con = mysql_connect("gamelogin.db.6086537.hostedresource.com","gamelogin","-----");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }



// ----- ADD BREEDING FEMALES TO TABLE ----------
mysql_select_db("gamelogin", $con);
$query="SELECT petname FROM user_pets where username = '$username' AND gender = 'female' breeding = 'yes' ORDER BY petname";
   $result = mysql_query ($query);
echo "<select petname=females=''>females</option>";
//
if (!$query or mysql_num_rows($query))
{
echo "Empty";
}
else
{

// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[petname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
//
//
}
// ----- ADD BREEDING MALES TO TABLE ----------

$sql="SELECT * FROM user_pets WHERE username like '$username' AND breed like '$breeding' ORDER BY gender";

$result=mysql_query($sql);

?>
<table width="300" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><table width="400%" border="0" cellpadding="3" cellspacing="1" bgcolor="#000000">
<tr>
<td bgcolor="#000000"> </td>
<td colspan="4" bgcolor="#000000"><strong>Breeding Pets</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#000000"><strong>id</strong></td>
<td align="center" bgcolor="#000000"><strong>element</strong></td>
<td align="center" bgcolor="#000000"><strong>pet species</strong></td>
<td align="center" bgcolor="#000000"><strong>age</strong></td>
<td align="center" bgcolor="#000000"><strong>age</strong></td>
<td align="center" bgcolor="#000000"><strong>gender</strong></td>
<td align="center" bgcolor="#000000"><strong>breeding</strong></td>
<td align="center" bgcolor="#000000"> </td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<table width="340" border="1" cellspacing="0" cellpadding="3">
<tr>
<td width="5%"><?php echo $rows['id']; ?></td>
<td width="20%"><?php echo $rows['type']; ?></td>
<td width="25%"><?php echo $rows['petname']; ?></td>
<td width="10%"><?php echo $rows['age']; ?></td>
<td width="10%"><?php echo $rows['gender']; ?></td>
<td width="10%"><?php echo $rows['breed']; ?></td>
</tr>
</table>

<?php
}
// close while loop

echo "</div>";

// close connection;
mysql_close();
              }
else // ------------form hasn't been filled, so show form
// ------------FEMALE FORM ---------
?><br><br>Females<br><?php
$con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","-------") or die(mysql_error());
$db = mysql_select_db("gamelogin",$con) or die(mysql_error());
$query="SELECT petname FROM user_pets where username = '$username' AND gender='female' AND breeding='' ORDER BY petname";
   $result = mysql_query ($query);
echo "<select name=femalepets=''>females</option>";
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[females]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box

//   --------- MALE FORM ----------
?><br><br>Males<br><?php
$con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","-------") or die(mysql_error());
$db = mysql_select_db("gamelogin",$con) or die(mysql_error());
$query="SELECT petname FROM user_pets where username = '$username' AND gender='male' AND breeding='' ORDER BY petname";
   $result = mysql_query ($query);
echo "<select name=malepets=''>males</option>";
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[males]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>

Link to comment
Share on other sites

i'm posting again, sorry.

 

i finally rewrote the entire thing completely, and NOW it works. it updates the database as well as lets you select per gender.

only issue left is that instead of saying the names of pets breeding, it shows "resource id 2", no idea why.

 

<?php
session_start();
$username= $_SESSION['username'];
echo "$username<br>";
// --------------check if form has been filled

$con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","-----") or die(mysql_error());
$db = mysql_select_db("gamelogin",$con) or die(mysql_error());
$result4 = mysql_query("SELECT petname FROM user_pets WHERE username = '$username' AND breeding = 'yes'");

echo "<b><u>Pets breeding:</u></b><br>";
echo "$result4<br>";

if ( $_POST['female'] && $_POST['male'])
{
//
$male = $_POST['male'];
$female = $_POST['female'];
//
echo "Breeding Female: $female<br />";
echo "with male : $male<br />";
// ---------------- CONNECT TO DATABASE ----------------------
$con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","----") or die(mysql_error());
$db = mysql_select_db("gamelogin",$con) or die(mysql_error());
//
// ------------ UPDATE DATABASE WITH FEMALE BREEDING = YES (USE ID) --------
$result2 = mysql_query("UPDATE user_pets SET breeding='yes' WHERE id='$female'") 
or die(mysql_error()); 
//
// ------------ UPDATE DATABASE WITH MALE BREEDING = YES (USE ID) -----------
$result3 = mysql_query("UPDATE user_pets SET breeding='yes' WHERE id='$male'") 
or die(mysql_error()); 
//
}
else // ------------form hasn't been filled, so show form
// ------------MALE FORM ---------
?><br><br><u>Males:</u><br>
<form id="breed" action="test.php" method="post">
<?php
$con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","----") or die(mysql_error());
$db = mysql_select_db("gamelogin",$con) or die(mysql_error());
$query="SELECT petname,id FROM user_pets WHERE username = '$username' AND gender = 'male'";

$result = mysql_query ($query);
echo "<select name=\"male\" value=\"male\">male</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[petname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
// ------------FEMALE FORM ---------
?>
<br><br><br>
<u>Females:</u><br>
<?php
$con = mysql_connect ("gamelogin.db.6086537.hostedresource.com","gamelogin","-------") or die(mysql_error());
$db = mysql_select_db("gamelogin",$con) or die(mysql_error());
$query="SELECT petname,id FROM user_pets WHERE username = '$username' AND gender = 'female'";

$result = mysql_query ($query);
echo "<select name='female' value='female'>Female</option>";
// printing the list box select command

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[petname]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
<br><br><br><input type="submit" value="submit"><br><br><br></form>
<u>NOTE:</u><i> Once a pair has begun bonding, you may not use them for fighting or any other task during that time. Bonding processes vary widely in length, so be careful with your decision!</I><br>

Link to comment
Share on other sites

$result4 = mysql_query("SELECT petname FROM user_pets WHERE username = '$username' AND breeding = 'yes'");

echo "<b><u>Pets breeding:</u></b><br>";
echo "$result4<br>";  <----------------- WRONG

 

You can't do this.  When the data is SELECTED from a table, you need to extract what you want.

 

$result4 = mysql_query("SELECT petname FROM user_pets WHERE username = '$username' AND breeding = 'yes'");
echo "<b><u>Pets breeding:</u></b><br>";

while ($myrow = mysql_fetch_array($result4))     {
           echo "$myrow['petname']<br/>;
}

 

Link to comment
Share on other sites

hello, thank you for responding. i actually finally figured it out. now i'm trying to figure out a way to have each breeding timed so that the pets being used can't breed for a certain amount of time.

 

my host doesn't have cron jobs so i'm sort of feeling screwed right now. any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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