Emiiya Posted June 18, 2010 Share Posted June 18, 2010 I'm not going to explain the whole back story, so you might be totally clueless when it comes to what the heck I'm trying to accomplish here. It's a long story. BUT, I have this simple(ish) HTML form with a "discard" button (that deletes the entry from the table, yada yada, that stuff works fine) and a "wear" button, which should do some stuff with the database when you click it, ie. update some rows and delete stuff, etc, depending on which number you choose from the drop-down list I have coded. What exactly it does doesn't matter, I've got that all coded. So, I have an if statement for each of the possible 9 outcomes depending on which number you select; however, only #1 is working and I have no idea why. There are no MySQL or PHP errors, just some stubborn code. Whatever number you choose from the drop-down list, it will ALWAYS echo "You chose 1." However, I'd like it to say "You chose __(number)." Of course, the code in the end isn't going to echo that at all. The MySQL stuff that happens when you click "Wear" is gonna go in each of the places where it says "echo "You chose 2";". I noticed that my code there wasn't working, so I took it out and substituted it with this simple output to see why the form wasn't working. I can't seem to figure it out. Any help? (some header stuff is here, before the code) <? $sql=mysql_query("SELECT * FROM inventory WHERE username='$username'"); if(mysql_num_rows($sql)==0){ echo "<center><font class=header>Your inventory is empty.</font></center>"; } if(mysql_num_rows($sql) > 0){ echo "<table border=\"0\" cellspacing=\"10\">"; echo "<tr>"; for($i=0;$i<=mysql_num_rows($sql);$i=$i+1){ while($row = mysql_fetch_array($sql)){ $itemcode = $row['itemcode']; $category = $row['category']; $itemname = $row['itemname']; $idofitem = $row['id']; $i++; if($i%6==0){ // use the operator to find the remainder of $i / 6 (%) // if the remainder is 0; it is a multiple of 6, so make a new row. echo "<td align=\"center\"><img src=\"http://*******.com/images/items/$category/$itemcode.png\" /><br />$itemname<br />"; ?> <form action="<? echo $PHP_SELF; ?>" method="post" name="discarditem"> <input type="hidden" name="idofitem" value="<? echo $idofitem; ?>" /> <input type="submit" name="discarditem" value="Discard" /></form> <? if ($category == "clothes") { ?> <form action="<? echo $PHP_SELF; ?>" method="post" name="wearitem"> <input type="hidden" name="idofitem" value="<? echo $idofitem; ?>" /> <input type="hidden" name="codeofitem" value="<? echo $itemcode; ?>" /> <input type="hidden" name="nameofitem" value="<? echo $itemname; ?>" /> <select name="wdno"><option value="99">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option> <option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option> </select> <input type="submit" name="wearitem" value="Wear" /></form> <? } ?> <? echo "</td></tr><tr>"; } else { echo "<td align=\"center\"><img src=\"http://*******.com/images/items/$category/$itemcode.png\" /><br />$itemname<br />"; ?> <form action="<? echo $PHP_SELF; ?>" method="post" name="discarditem"> <input type="hidden" name="idofitem" value="<? echo $idofitem; ?>" /> <input type="submit" name="discarditem" value="Discard" /></form> <? if ($category == "clothes") { ?> <form action="<? echo $PHP_SELF; ?>" method="post" name="wearitem"> <input type="hidden" name="idofitem" value="<? echo $idofitem; ?>" /> <input type="hidden" name="codeofitem" value="<? echo $itemcode; ?>" /> <input type="hidden" name="nameofitem" value="<? echo $itemname; ?>" /> <select name="wdno"><option value="99">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option> <option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option> </select> <input type="submit" name="wearitem" value="Wear" /></form> <? } ?> <? echo "<td>"; } } } echo "</tr></table>"; } $yep = mysql_query("SELECT * FROM characters WHERE owner='$username'") or die(mysql_error()); while($row = mysql_fetch_array( $yep )) { $name1 = $row['name1']; $name2 = $row['name2']; $name3 = $row['name3']; $name4 = $row['name4']; $name5 = $row['name5']; $name6 = $row['name6']; $name7 = $row['name7']; $name8 = $row['name8']; $name9 = $row['name9']; } if (isset($_POST['discarditem'])) { $idofitem2 = $_POST['idofitem']; mysql_query("DELETE FROM inventory WHERE id='$idofitem2'") or die(mysql_error()); echo "<font color=\"red\">This item has been permanently discarded.</font>"; } if (isset($_POST['wearitem'])) { $idofitem2 = $_POST['idofitem']; $nameofitem2 = $_POST['nameofitem']; $codeofitem2 = $_POST['codeofitem']; $wdnumber = $_POST['wdno']; if ($wdnumber = "1") { echo "You chose 1"; } elseif ($wdnumber = "2") { echo "You chose 2"; } elseif ($wdnumber = "3") { echo "You chose 3"; } elseif ($wdnumber = "4") { echo "You chose 4"; } elseif ($wdnumber = "5") { echo "You chose 5"; } elseif ($wdnumber = "6") { echo "You chose 6"; } elseif ($wdnumber = "7") { echo "You chose 7"; } elseif ($wdnumber = "8") { echo "You chose 8"; } elseif ($wdnumber = "9") { echo "You chose 9"; } } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 Ok, you really need to start using the "logic" that is available in PHP instead of brute force coding. I see your error, but let's go over a couple of things that would make your life so much easier first. Instead of this while($row = mysql_fetch_array( $yep )) { $name1 = $row['name1']; $name2 = $row['name2']; $name3 = $row['name3']; $name4 = $row['name4']; $name5 = $row['name5']; $name6 = $row['name6']; $name7 = $row['name7']; $name8 = $row['name8']; $name9 = $row['name9']; } You could just to this: extract($row); And, you should modify your query to only pull the fields you want. Then there is the mess of if/else statements if ($wdnumber = "1") { echo "You chose 1"; } elseif ($wdnumber = "2") { echo "You chose 2"; } elseif ($wdnumber = "3") { echo "You chose 3"; } elseif ($wdnumber = "4") { echo "You chose 4"; } elseif ($wdnumber = "5") { echo "You chose 5"; } elseif ($wdnumber = "6") { echo "You chose 6"; } elseif ($wdnumber = "7") { echo "You chose 7"; } elseif ($wdnumber = "8") { echo "You chose 8"; } elseif ($wdnumber = "9") { echo "You chose 9"; } Why not just do this: echo "You chose {$wdnumber}"; Anyway, this is your problem if ($wdnumber = "1") { You are doing an assignment NOT a comparison. Since the PHP parser has no problem assigning the value of 1 to the variable the condition always passes. You use double equal signs for comparisons. if ($wdnumber == "1") { Quote Link to comment Share on other sites More sharing options...
Emiiya Posted June 18, 2010 Author Share Posted June 18, 2010 Then there is the mess of if/else statements if ($wdnumber = "1") { echo "You chose 1"; } elseif ($wdnumber = "2") { echo "You chose 2"; } elseif ($wdnumber = "3") { echo "You chose 3"; } elseif ($wdnumber = "4") { echo "You chose 4"; } elseif ($wdnumber = "5") { echo "You chose 5"; } elseif ($wdnumber = "6") { echo "You chose 6"; } elseif ($wdnumber = "7") { echo "You chose 7"; } elseif ($wdnumber = "8") { echo "You chose 8"; } elseif ($wdnumber = "9") { echo "You chose 9"; } Why not just do this: echo "You chose {$wdnumber}"; Anyway, this is your problem if ($wdnumber = "1") { You are doing an assignment NOT a comparison. Since the PHP parser has no problem assigning the value of 1 to the variable the condition always passes. You use double equal signs for comparisons. if ($wdnumber == "1") { Thank you so much for that! I'm not really an "advanced" programmer at all. I do it in my spare time and I've been teaching myself because I like getting things to work. The problem is, I'm doing something a little different for each of the numbers, that's why I have all the if statements. Maybe you can answer another question I had before this, which would be really helpful - the reason I have all these if statements is because depending on the number chosen from the drop-down, the "item" would be inserted into a certain corresponding row in the table ("row1"; "row2", etc. as examples). But - I had no idea how to do that...this is generally what each if statement looks like: elseif ($wdnumber = "2") { if ($name2 != "none") { echo "<font color=\"red\">There is already something in that row.</font>"; } else { mysql_query("DELETE FROM inventory WHERE id='$idofitem2'") or die(mysql_error()); mysql_query("UPDATE characters SET name2='$nameofitem2', item2='$codeofitem2' WHERE owner='$username'") or die(mysql_error()); } } Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 The problem is, I'm doing something a little different for each of the numbers, that's why I have all the if statements. Then you shoudl be using a switch() stement instead of all the If/Esle statements. Maybe you can answer another question I had before this, which would be really helpful - the reason I have all these if statements is because depending on the number chosen from the drop-down, the "item" would be inserted into a certain corresponding row in the table ("row1"; "row2", etc. as examples). But - I had no idea how to do that... Not 100% sure what your questin is. What is the problem you are having with that code? Quote Link to comment Share on other sites More sharing options...
Emiiya Posted June 18, 2010 Author Share Posted June 18, 2010 In the database table, basically, there's rows such as username (your/the user's username), and then rows called Name1, Item1, Name2, Item2, Name3, Item3, etc. So a user's username is in the username column, and then they have something in each row. This is in inventory script, so in Name1 there might be a name like "Black Shoes" and in Item1 is the code for the item, such as blackshoes.png, which is for the image of the item. All of this works. With this, I'm also seeing if I can build a human avatar system, with layering images of clothes/etc on the base image of a person. I've gotten this script to work completely. All I need is for the user to be able to insert things from the inventory to the table (above) with the items they are "wearing." I don't want to have columns in the table like "Shirt", "Pants", etc because that would mean only one shirt can be worn at a time, and only pair of pants, etc. So I've made 9 slots for items so that 9 items can be worn at one time. In the human avatar script, I've already got layering and stuff to work, and the z-index of each image is in another row called index1, index2, etc. But that's besides the point. Basically, it would look like this: Username---Name1----Item1----Index1---Name2---Item2---Index2---etc Em-------------Blah-----blah.png----1-------etc The drop down list has 9 different slots to choose from, and then clicking "Wear" would delete that item from the inventory table and update the Character table where username=$whatever the username is, which I have done now in that jumble of code in my first post (thanks to you fixing my error before). You asked why I had all the if/else statements. This is because I have no idea how to compare what you selected in the drop-down list to the row names. What I would prefer having, instead of the handful of if/elses, is inserting the item into the row corresponding to $wdnumber (the number you chose). So, for instance, if you chose 5, the row where username=$username would be updated and insert the code of the image in the row Item5 and the name of it in Name5. I just have NO idea how to do this, and to get around it, I used a million if/else statements. In short, is there a much easier way to do this? Quote Link to comment Share on other sites More sharing options...
jcbones Posted June 18, 2010 Share Posted June 18, 2010 Something like this: $wdnumber = $_POST['where']; $column = 'name' . $wdnumber; $item = $_POST['item']; $sql = "UPDATE `table` SET `$column`='$item' WHERE `username`='$user'"; Quote Link to comment Share on other sites More sharing options...
Emiiya Posted June 19, 2010 Author Share Posted June 19, 2010 Something like this: $wdnumber = $_POST['where']; $column = 'name' . $wdnumber; $item = $_POST['item']; $sql = "UPDATE `table` SET `$column`='$item' WHERE `username`='$user'"; THIS. This is what I was looking for. I didn't know you could have a variable as the row, where it's '$column'='$item'. Thank you, I appreciate all the help! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 19, 2010 Share Posted June 19, 2010 Well, you are doing it all wrong. You need to normalize your database (too much to try and explain here). The "items" should be stored in a separate table with a foreign key back to the user table. Creating nine (times two) serparate columns is a poor design. Tables would look something like this: Users user_id username items user_id item index name Quote Link to comment 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.