illegalife Posted January 20, 2011 Share Posted January 20, 2011 Hello, sorry to bother you guys, but i've been trying to figure this out and i'm just too much of a noob. I need a code that would total up all the prices of items (inventory). So in this case the code would show "$35" as the sum of both items. Thanks! Quote Link to comment Share on other sites More sharing options...
Karlos94 Posted January 20, 2011 Share Posted January 20, 2011 http://www.w3schools.com/php/php_operators.asp Maybe that link could help! Quote Link to comment Share on other sites More sharing options...
Djkanna Posted January 20, 2011 Share Posted January 20, 2011 Real quick and should work but it's 2am my logic is a little flawed. <?php //Asuming that the inventory list is in a form of loop, I presume a while loop. $totalPrice = 0; //The while loop for the inventory listings while ( row = $db->fetch_row ($getItems) ) { $totalPrice += $row['itemValue']; } echo 'Total value of items in inventory is $ '. number_format ($totalPrice); ?> Quote Link to comment Share on other sites More sharing options...
Sim Posted January 20, 2011 Share Posted January 20, 2011 I would go with Djkanna's method since your allready pulling all the data from the DB. If you was not pulling all the data, i would look into the SUM() function. ;] Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted January 20, 2011 Share Posted January 20, 2011 Just to clarify SUM for mysql not PHP select SUM(fieldname) from items you may wish to also GROUP by item type should be a simple adjustment to your existing select query. Quote Link to comment Share on other sites More sharing options...
illegalife Posted January 20, 2011 Author Share Posted January 20, 2011 Thanks guys but can you be a little more detailed? i really dont know what im doing... :/ Also, I would like to put the sum of items in inventory so if you have two weapons the code would show "2". Here's the code: <?php include "globals.php"; $q=$db->query("SELECT * FROM items WHERE itmid IN({$ir['equip_primary']}, {$ir['equip_secondary']}, {$ir['equip_armor']})"); print "<h3>Equipped Items</h3><hr />"; while($r=$db->fetch_row($q)) { $equip[$r['itmid']]=$r; } print "<table width='75%' cellspacing='1' class='table'> <tr> <th>Primary Weapon</th> <td>"; if($equip[$ir['equip_primary']]['itmid']) { print $equip[$ir['equip_primary']]['itmname']."</td><td>[url='unequip.php?type=equip_primary']Unequip Item[/url]</td>"; } else { print "None equipped.</td><td></td>"; } print "</tr> <tr> <th>Secondary Weapon</th> <td>"; if($equip[$ir['equip_secondary']]['itmid']) { print $equip[$ir['equip_secondary']]['itmname']."</td><td>[url='unequip.php?type=equip_secondary']Unequip Item[/url]</td>"; } else { print "None equipped.</td><td></td>"; } print "</tr> <tr> <th>Armor</th> <td>"; if($equip[$ir['equip_armor']]['itmid']) { print $equip[$ir['equip_armor']]['itmname']."</td><td>[url='unequip.php?type=equip_armor']Unequip Item[/url]</td>"; } else { print "None equipped.</td><td></td>"; } print "</tr> </table><hr /> <h3>Inventory</h3><hr />"; $inv=$db->query("SELECT iv.*,i.*,it.* FROM inventory iv LEFT JOIN items i ON iv.inv_itemid=i.itmid LEFT JOIN itemtypes it ON i.itmtype=it.itmtypeid WHERE iv.inv_userid={$userid} ORDER BY i.itmtype ASC, i.itmname ASC"); if ($db->num_rows($inv) == 0) { print "[b]Twoj koszyk jest pusty![/b]"; } else { print "[b]Your items are listed below.[/b] <table width=70% class=\"table\" border=\"0\" cellspacing=\"1\"> <tr> <td class=\"h\">Item</td> <td class=\"h\">Sell Value</td> <td class=\"h\">Links</td> </tr>"; $lt=""; while($i=$db->fetch_row($inv)) { if($lt!=$i['itmtypename']) { $lt=$i['itmtypename']; print "\n<tr><td colspan=4>[b]{$lt}[/b]</td></tr>"; } if($i['weapon']) { $i['itmname']="<font color='red'>*</font>".$i['itmname']; } if($i['armor']) { $i['itmname']="<font color='green'>*</font>".$i['itmname']; } print "<tr><td>{$i['itmname']}"; if ($i['inv_qty'] > 1) { print "x{$i['inv_qty']}"; } $calosc="$".($i['itmbuyprice']*$i['inv_qty']); print "</td><td>$calosc</td>"; print "<td>[[url='iteminfo.php?ID={$i[']Info[/url]] [[url='itemsell.php?ID={$i[']Usun[/url]]"; if($i['effect1_on'] || $i['effect2_on'] || $i['effect3_on']) { print " [[url='itemuse.php?ID={$i[']Use[/url]]"; } if($i['weapon']) { print " [[url='equip_weapon.php?ID={$i[']Equip as Weapon[/url]]"; } if($i['armor']) { print " [[url='equip_armor.php?ID={$i[']Equip as Armor[/url]]"; } print "</td></tr>"; } print "</table>"; print "$IWANT-THE-SUM-TO-PRINT-HERE"; } $h->endpage(); ?> Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted January 20, 2011 Share Posted January 20, 2011 $multiplier=1; $calosc = $i['itmbuyprice']*$i['inv_qty']; $total += $multiplier*$calosc; Then where you have your print print "{$total}"; Quote Link to comment Share on other sites More sharing options...
illegalife Posted January 20, 2011 Author Share Posted January 20, 2011 Thanks, but still no good ;/ im getting "0" as the result. I need this to work guys. I will make a banner for your game for anyone that gets this to work. ;] Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted January 20, 2011 Share Posted January 20, 2011 worked for me lol Quote Link to comment Share on other sites More sharing options...
illegalife Posted January 20, 2011 Author Share Posted January 20, 2011 O_o I just uploaded the original inventory.php, tried your thing and still getting "0" as the result. ;/ Quote Link to comment Share on other sites More sharing options...
rulerofzu Posted January 20, 2011 Share Posted January 20, 2011 <?php include "globals.php"; $q=$db->query("SELECT * FROM items WHERE itmid IN({$ir['equip_primary']}, {$ir['equip_secondary']}, {$ir['equip_armor']})"); print "<h3>Equipped Items</h3><hr />"; while($r=$db->fetch_row($q)) { $equip[$r['itmid']]=$r; } print "<table width='75%' cellspacing='1' class='table'> <tr> <th>Primary Weapon</th> <td>"; if($equip[$ir['equip_primary']]['itmid']) { print $equip[$ir['equip_primary']]['itmname']."</td><td>[url='unequip.php?type=equip_primary']Unequip Item[/url]</td>"; } else { print "None equipped.</td><td></td>"; } print "</tr> <tr> <th>Secondary Weapon</th> <td>"; if($equip[$ir['equip_secondary']]['itmid']) { print $equip[$ir['equip_secondary']]['itmname']."</td><td>[url='unequip.php?type=equip_secondary']Unequip Item[/url]</td>"; } else { print "None equipped.</td><td></td>"; } print "</tr> <tr> <th>Armor</th> <td>"; if($equip[$ir['equip_armor']]['itmid']) { print $equip[$ir['equip_armor']]['itmname']."</td><td>[url='unequip.php?type=equip_armor']Unequip Item[/url]</td>"; } else { print "None equipped.</td><td></td>"; } print "</tr> </table><hr /> <h3>Inventory</h3><hr />"; $inv=$db->query("SELECT iv.*,i.*,it.* FROM inventory iv LEFT JOIN items i ON iv.inv_itemid=i.itmid LEFT JOIN itemtypes it ON i.itmtype=it.itmtypeid WHERE iv.inv_userid={$userid} ORDER BY i.itmtype ASC, i.itmname ASC"); if ($db->num_rows($inv) == 0) { print "[b]Twoj koszyk jest pusty![/b]"; } else { print "[b]Your items are listed below.[/b] <table width=70% class=\"table\" border=\"0\" cellspacing=\"1\"> <tr> <td class=\"h\">Item</td> <td class=\"h\">Sell Value</td> <td class=\"h\">Links</td> </tr>"; $lt=""; while($i=$db->fetch_row($inv)) { if($lt!=$i['itmtypename']) { $lt=$i['itmtypename']; print "\n<tr><td colspan=4>[b]{$lt}[/b]</td></tr>"; } if($i['weapon']) { $i['itmname']="<font color='red'>*</font>".$i['itmname']; } if($i['armor']) { $i['itmname']="<font color='green'>*</font>".$i['itmname']; } print "<tr><td>{$i['itmname']}"; if ($i['inv_qty'] > 1) { print "x{$i['inv_qty']}"; } $multiplier=1; $calosc = $i['itmbuyprice']*$i['inv_qty']; $total += $multiplier*$calosc; print "</td><td>$calosc</td>"; print "<td>[[url='iteminfo.php?ID={$i[']Info[/url]] [[url='itemsell.php?ID={$i[']Usun[/url]]"; if($i['effect1_on'] || $i['effect2_on'] || $i['effect3_on']) { print " [[url='itemuse.php?ID={$i[']Use[/url]]"; } if($i['weapon']) { print " [[url='equip_weapon.php?ID={$i[']Equip as Weapon[/url]]"; } if($i['armor']) { print " [[url='equip_armor.php?ID={$i[']Equip as Armor[/url]]"; } print "</td></tr>"; } print "</table>"; print "--->> THE RESULT YOU WANT {$total} <<---"; } $h->endpage(); ?> Thats what you was wishing? works for me. Quote Link to comment Share on other sites More sharing options...
illegalife Posted January 20, 2011 Author Share Posted January 20, 2011 Works now! ;] as promised, hit me up whenever you need a banner. 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.