MasterACE14 Posted December 4, 2011 Share Posted December 4, 2011 Good Day Everyone! I'm having trouble figuring out how to assign weapons from a MySQL table to soldiers. Say I have the following... $soldiers = 100; $damage = 0; $q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id`='".$userid."' ORDER BY `item_power` DESC"); while($row = mysql_fetch_assoc($q)) { // assign weapons to soldiers and add `item_power` * `item_quantity`to $damage } I want to assign all the weapons until there's not enough soldiers to assign them to, and I want to assign them in order of `item_power` descending. I'm sure this is really simple, I just can't for the life of me get my head around it. Thanks in advance! Kind Regards, Ace Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 <?php $i = 1; $damage = 0; // don't surrond integers with single quotes. single quotes are for strings. $q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid). " ORDER BY `item_power` DESC"); while($row = mysql_fetch_assoc($q) AND $i <= 100 # 100 = soldier count) { // $solider[] now contains array('item_quantity', 'item_power'); $solider[] = $row; ++$i; } Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 I'm trying to avoid a method in which a loop is used to pass through every single soldier, in the likely case that a user has say 30 million soldiers, that could get quite ugly. I Appreciate the suggestion. Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 Well how else are you going to assign the soldiers a weapon? That loop will either pass through every soldier or through every weapon, whichever case is smaller. So if you want to assign weapons to soldiers but don't want to loop through every weapon you're out of options. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 assigning weapons to soldiers is an explanation of what I'm theoretically trying to achieve, I'm not literally trying to assign an array of values to each individual soldier. As I commented within my example // assign weapons to soldiers and add `item_power` * `item_quantity`to $damage thinking something like... if($soldiers > $row['item_quantity']) { $damage += $row['item_power']; } else // ... Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 Your comment says "assign weapons to soliders" which is what my code does. I'm not sure what you're asking now, because your comments are conflicting. Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 Example of what I mean by contradicting comments: "I want to assign all the weapons until there's not enough soldiers to assign them to" "I'm not literally trying to assign an array of values to each individual soldier" Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 assigning weapons to soldiers is different to assigning arrays to variables. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted December 4, 2011 Share Posted December 4, 2011 Maybe if you explained how soldiers are represented in your code you'd get a better answer. $soldier = 100 doesn't convey any meaning. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 Maybe if you explained how soldiers are represented in your code you'd get a better answer. $soldier = 100 doesn't convey any meaning. My apologies, $soldier = 100; is the total amount of soldiers the user has(usually comes from the database, 100 for the purpose of this thread). I'll try to explain my problem differently. If I have 100 soldiers, and say 3 records for weapons(items) this particular user has in the database containing... weapon -------- item_quantity ---------- item_power 1 30 25 2 50 10 3 50 5 I want to determine the total power(damage) that this player has by essentially adding `item_power` * `soldiers`(from strongest item_power to lowest) until there is no more 'weaponless' soldiers or, there is not enough soldiers to virtually 'equip' the items. So in this scenario the total power of this user would be: first weapon: 30 * 25 (30 soldiers have the first weapon equipped) second weapon: 50 * 25 (50 soldiers have the second weapon equipped, 80 soldiers have weapons equipped in total now) third weapon: 20 * 5 (20 soldiers have the third weapon equipped, 100 soldiers have weapons equipped in total, 30 are weaponless) total power = (30 * 25) + (50 * 25) + (20 * 5) If in this scenario there was less weapons than soldiers, it would not equip anymore weapons once all the soldiers have weapons. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 Your comment says "assign weapons to soliders" which is what my code does. I'm not sure what you're asking now, because your comments are conflicting. Sorry I just noticed the mistake I made in the comment in my first example, should of been $soldiers * item_power rather than item_power * item_quantity. Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 Okay, now you're making sense: <?php $weapons = array(); $total_weapons = 0; $soldiers = 100; $q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid) . " ORDER BY `item_power` DESC"); while($row = mysql_fetch_assoc($q)) { $row['item_quantity'] = intval($row['item_quantity']); $weapon[] = array('quantity' => $row['item_quantity'], 'damage' => $row['item_quantity'] * intval($row['item_power'])); $total_weapons += $row['item_quantity']; if ($total_weapons >= $soldiers) { break; } } Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 Or if you just want total damage and don't care about individual weapon stats: <?php $total_power = 0; $total_weapons = 0; $soldiers = 100; $q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid) . " ORDER BY `item_power` DESC"); while($row = mysql_fetch_assoc($q)) { $row['item_quantity'] = intval($row['item_quantity']); $total_weapons += $row['item_quantity']; if ($total_weapons > $soldiers) { $extra_weapons = $total_weapons - $soldiers; // trim the item_quantity so that it doesn't surpass how many soliders you have if ($row['item_quantity'] = ($row['item_quantity'] - $extra_weapons) != 0) { $total_power += $row['item_quantity'] * intval($row['item_power'])); } break; } $total_power += $row['item_quantity'] * intval($row['item_power'])); } Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 Or if you just want total damage and don't care about individual weapon stats: <?php $total_power = 0; $total_weapons = 0; $soldiers = 100; $q = mysql_query("SELECT `item_quantity`, `item_power` FROM `items` WHERE `owner_id` = " . intval($userid) . " ORDER BY `item_power` DESC"); while($row = mysql_fetch_assoc($q)) { $row['item_quantity'] = intval($row['item_quantity']); $total_weapons += $row['item_quantity']; if ($total_weapons > $soldiers) { $extra_weapons = $total_weapons - $soldiers; // trim the item_quantity so that it doesn't surpass how many soliders you have if ($row['item_quantity'] = ($row['item_quantity'] - $extra_weapons) != 0) { $total_power += $row['item_quantity'] * intval($row['item_power'])); } break; } $total_power += $row['item_quantity'] * intval($row['item_power'])); } that's done the trick! many thanks! Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 just one problem with this line though: // trim the item_quantity so that it doesn't surpass how many soliders you have if ($row['item_quantity'] = ($row['item_quantity'] - $extra_weapons) != 0) is it suppose to be == or... ? Thanks Quote Link to comment Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 No. I'm assigning $row['item_quantity'] that value then making sure it isn't 0 so I don't divide by 0. I didn't test it but it should work fine. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted December 4, 2011 Author Share Posted December 4, 2011 ah ok, thanks again! 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.