twilitegxa Posted March 13, 2010 Share Posted March 13, 2010 I keep doing this code wrong. Can someone please help me get it right? Here is what I have: //OPEN TRESURE CHESTS if ($action == 'open'){ echo "<h1>Burg Village</h1> <h3>Open All Treasure Chests</h3>"; //check acquired table for existing field values $check_table_acquired = mysql_query("select * from acquired", $conn); $num_rows_acquired = mysql_num_rows($check_table_acquired); //if acquired table is empty if ($num_rows_acquired == 0){ //add the record to the table $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')"; $add_record_res = mysql_query($add_record, $conn) or die(mysql_error()); }//end empty acquired table if statement //if acquired has existing records, check if player has acquired items from location $check_acquired = mysql_query("select * from acquired where identity = '$identity'"); $num_rows_acquired = mysql_num_rows($check_acquired); while ($acquired_items = mysql_fetch_array($check_acquired)){ $location = $acquired_items['location']; $acquired = $acquired_items['acquired']; } //if player has records in the acquired table if ($num_rows_acquired >= '1'){ //if player has acquired items from location if ($location == 'Burg Inn' && $acquired == '1'){ echo "You have already acquired all items from this location. <a href=location.php?location=inn>Back</a>."; }//end of acquired items from location if statement }//end of $num_rows_acquired =< 1 if statement //if player has not acquired items from this location else { //check inventory for existing field values $check_inventory = mysql_query("select * from inventory", $conn); $num_rows_inventory = mysql_num_rows($check_inventory); //if inventory is empty if ($num_rows_inventory == 0){ //insert the items $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); }//end of $num_rows_inventory == 0 if statement //if inventory has existing records if ($num_rows_inventory >= 1){ //check if the items exist $check_inventory = "select * from inventory where identity = '$identity'"; $check_inventory_res = mysql_query($check_inventory, $conn) or die(mysql_error()); while ($items = mysql_fetch_array($check_inventory_res)){ $item = $items['item']; $quantity = $items['quantity']; } //if they do, update the quantity //if they don't, insert the item if ($item == 'Star Light'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Star Light'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } else { $insert_item = "insert into inventory values ('', '$identity', 'Star Light', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); } if ($item == 'Herb'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Herb'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } else { $insert_item = "insert into inventory values ('', '$identity', 'Herb', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); } if ($item == 'Dragonfly Wing'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Dragonfly Wing'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } else { $insert_item = "insert into inventory values ('', '$identity', 'Dragonfly Wing', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); } if ($item == 'Angel Tear'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Angel Tear'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } else { $insert_item = "insert into inventory values ('', '$identity', 'Angel Tear', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); } }//end of $num_rows_inventory >= 1 if statement }//end of not acquired items if statement }//end action if statement What I need to do is this: If the action is 'open', I need to check if any records exist in the acquired table. If records don't exist, I need to add the record. If records exist, i need to check if the player has already acquired the items from this location. If the player has acquired the items, then tell them so. If the player has not acquire the items, insert the record, then check the inventory table. If the inventory table is empty, insert the items. If the inventory table has existing records, check if the player has existing items. If the player has existing items, check if each item exists. If it exists, update the quantity. If it doesn't, insert the item. Can anyone please help me with what I am doing wrong??? Please, it is driving me CRAZY!!! I think I am missing the part where it inserts the record for the acquired items if the player does not have a record in the acquire table. Link to comment https://forums.phpfreaks.com/topic/195089-please-help-i-have-redone-this-code-six-times/ Share on other sites More sharing options...
twilitegxa Posted March 13, 2010 Author Share Posted March 13, 2010 Right now, if the acquired table is empty, the record inserts and the inventory inserts, but the items in the inventory do not update the quantity as expected, they add a whole new record, except for the Angel Tear; it updates that one item properly. But if the acquired table is not empty, if will not insert the record into the acquired table nor will it insert the items into the inventory table. I can't figure this out!!!! Please help me!!! And if the player already has a record in the acquired table that signifies that they have acquired the items from the location, it doesn't say anything. I know I have coded it wrong, I just can't figure out what I am missing. Link to comment https://forums.phpfreaks.com/topic/195089-please-help-i-have-redone-this-code-six-times/#findComment-1025511 Share on other sites More sharing options...
twilitegxa Posted March 13, 2010 Author Share Posted March 13, 2010 I'm still working on this page. So far I have the section that checks the acquired table for records working properly. It checks if the table is empty. If it is not empty, it checks if the player has a record, and if they have a record, it checks if they have acquired the items from the location. If they have, it tells them. If they haven't, it inserts the record into the acquired table. If the acquired table is empty, it inserts the record into the table. Here is the working code so far: //OPEN TRESURE CHESTS if ($action == 'open'){ echo "<h1>Burg Village</h1> <h3>Open All Treasure Chests</h3>"; //check acquired table for existing field values $check_table_acquired = mysql_query("select * from acquired", $conn); $num_rows_acquired = mysql_num_rows($check_table_acquired); //if acquired table is empty if ($num_rows_acquired == 0){ //add the record to the table $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')"; $add_record_res = mysql_query($add_record, $conn) or die(mysql_error()); echo "You have acquired the following items:<br /><br /> Angel Tear<br /> Star Light<br /> Herb<br /> Dragonfly Wing<br /><br /> The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>."; //ADD ITEMS HERE }//end of empty acquired if statement if ($num_rows_acquired >= 1){ //if acquired has existing records, check if player has acquired items from location $check_acquired = mysql_query("select * from acquired where identity = '$identity'"); $num_rows_acquired = mysql_num_rows($check_acquired); while ($acquired_items = mysql_fetch_array($check_acquired)){ $location = $acquired_items['location']; $acquired = $acquired_items['acquired']; } //if player has records in the acquired table if ($num_rows_acquired >= '1'){ //if player has acquired items from location if ($location == 'Burg Inn' && $acquired == '1'){ echo "You have already acquired all items from this location. <a href=location.php?location=inn>Back</a>."; }//end of acquired items from location if statement }//end check if player has records in acquired table if ($num_rows_acquired == 0){ //add the record to the table $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')"; $add_record_res = mysql_query($add_record, $conn) or die(mysql_error()); echo "You have acquired the following items:<br /><br /> Angel Tear<br /> Star Light<br /> Herb<br /> Dragonfly Wing<br /><br /> The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>."; //INSERT ITEMS HERE }//end check for player not having acquired items from location }//end of if statement for existing records in acquired table }//end of action if statement Now I need to get the inventory section working. I need to check if the inventory table is empty. If it is, I need to simply insert all items. If it is not empty, I need to check if the player has items. If the player has items, I need to check if each item exists in the table. If it does, I need to update the quantity. If it does not, I need to insert the item into the table. This is where I have the most frustration. Link to comment https://forums.phpfreaks.com/topic/195089-please-help-i-have-redone-this-code-six-times/#findComment-1025520 Share on other sites More sharing options...
twilitegxa Posted March 13, 2010 Author Share Posted March 13, 2010 Wow, I finally got it working. Here is what I did: //OPEN TRESURE CHESTS if ($action == 'open'){ echo "<h1>Burg Village</h1> <h3>Open All Treasure Chests</h3>"; //check acquired table for existing field values $check_table_acquired = mysql_query("select * from acquired", $conn); $num_rows_acquired = mysql_num_rows($check_table_acquired); //if acquired table is empty if ($num_rows_acquired == 0){ //add the record to the table $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')"; $add_record_res = mysql_query($add_record, $conn) or die(mysql_error()); echo "You have acquired the following items:<br /><br /> Angel Tear<br /> Star Light<br /> Herb<br /> Dragonfly Wing<br /><br /> The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>."; //ADD ITEMS HERE //check inventory for existing field values $check_inventory = mysql_query("select * from inventory", $conn); $num_rows_inventory = mysql_num_rows($check_inventory); //if inventory is empty if ($num_rows_inventory == 0){ //insert the items $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); }//end inventory is empty if statement if ($num_rows_inventory >= 1){ //check inventory for existing records of player $check_inventory = mysql_query("select * from inventory where identity = '$identity'", $conn); $num_rows_inventory = mysql_num_rows($check_inventory); //if player has records in inventory if ($num_rows_inventory >= 1){ //check if the items exist $check_inventory = "select * from inventory where identity = '$identity'"; $check_inventory_res = mysql_query($check_inventory, $conn) or die(mysql_error()); while ($items = mysql_fetch_array($check_inventory_res)){ $item = $items['item']; $quantity = $items['quantity']; //ADD ITEMS HERE //if they do, update the quantity //if they don't, insert the item //STAR LIGHT if ($item == 'Star Light'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Star Light'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Star Light', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 //HERB if ($item == 'Herb'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Herb'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Herb', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 //DRAGONFLY WING if ($item == 'Dragonfly Wing'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Dragonfly Wing'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Dragonfly Wing', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 //ANGEL TEAR if ($item == 'Angel Tear'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Angel Tear'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Angel Tear', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 }//end while statement for checking for items in inventory }//end of existing player records in inventory if statement //if player has no records in inventory if ($num_rows_inventory == 0){ //insert the items $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); }//end no player exisiting records in inventory if statement }//end inventory records exist if statement }//end of empty acquired if statement if ($num_rows_acquired >= 1){ //if acquired has existing records, check if player has acquired items from location $check_acquired = mysql_query("select * from acquired where identity = '$identity'"); $num_rows_acquired = mysql_num_rows($check_acquired); while ($acquired_items = mysql_fetch_array($check_acquired)){ $location = $acquired_items['location']; $acquired = $acquired_items['acquired']; } //if player has records in the acquired table if ($num_rows_acquired >= '1'){ //if player has acquired items from location if ($location == 'Burg Inn' && $acquired == '1'){ echo "You have already acquired all items from this location. <a href=location.php?location=inn>Back</a>."; }//end of acquired items from location if statement }//end check if player has records in acquired table if ($num_rows_acquired == 0){ //add the record to the table $add_record = "insert into acquired values ('', '$identity', 'Burg Inn', '1')"; $add_record_res = mysql_query($add_record, $conn) or die(mysql_error()); echo "You have acquired the following items:<br /><br /> Angel Tear<br /> Star Light<br /> Herb<br /> Dragonfly Wing<br /><br /> The items have been added to your inventory. <a href=location.php?location=inn>Continue</a>."; //INSERT ITEMS HERE //check inventory for existing field values $check_inventory = mysql_query("select * from inventory", $conn); $num_rows_inventory = mysql_num_rows($check_inventory); //if inventory is empty if ($num_rows_inventory == 0){ //insert the items $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); }//end inventory is empty if statement if ($num_rows_inventory >= 1){ //check inventory for existing records of player $check_inventory = mysql_query("select * from inventory where identity = '$identity'", $conn); $num_rows_inventory = mysql_num_rows($check_inventory); //if player has records in inventory if ($num_rows_inventory >= 1){ //check if the items exist $check_inventory = "select * from inventory where identity = '$identity'"; $check_inventory_res = mysql_query($check_inventory, $conn) or die(mysql_error()); while ($items = mysql_fetch_array($check_inventory_res)){ $item = $items['item']; $quantity = $items['quantity']; //ADD ITEMS HERE //if they do, update the quantity //if they don't, insert the item //STAR LIGHT if ($item == 'Star Light'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Star Light'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Star Light', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 //HERB if ($item == 'Herb'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Herb'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Herb', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 //DRAGONFLY WING if ($item == 'Dragonfly Wing'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Dragonfly Wing'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Dragonfly Wing', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 //ANGEL TEAR if ($item == 'Angel Tear'){ $new_quantity = ($quantity + 1); $update_quantity = "update inventory set quantity = '$new_quantity' where identity = '$identity' and item = 'Angel Tear'"; $update_quantity_res = mysql_query($update_quantity, $conn) or die(mysql_error()); } $check_items = mysql_query("select * from inventory where item = '$item'"); $num_rows_items = mysql_num_rows($check_items); if ($num_rows_items == 0){ $insert_item = "insert into inventory values ('', '$identity', 'Angel Tear', '1')"; $insert_item_res = mysql_query($insert_item, $conn) or die(mysql_error()); }//end if statement for $num_rows_items == 0 }//end while statement for checking for items in inventory }//end of existing player records in inventory if statement //if player has no records in inventory if ($num_rows_inventory == 0){ //insert the items $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Angel Tear', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Star Light', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Herb', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); $insert_items = "INSERT INTO inventory VALUES ('', '$identity', 'Dragonfly Wing', '1')"; $insert_items_res = mysql_query($insert_items, $conn) or die(mysql_error()); }//end no player exisiting records in inventory if statement }//end inventory records exist if statement }//end check for player not having acquired items from location }//end of if statement for existing records in acquired table }//end of action if statement It only took eight tries!!! Link to comment https://forums.phpfreaks.com/topic/195089-please-help-i-have-redone-this-code-six-times/#findComment-1025534 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.