Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. function buildings() {
  2. global $database;
  3. global $player;
  4. global $self_link;
  5.  
  6.  
  7. // Fetch buildings
  8. $result = $database->query("SELECT * FROM buildings");
  9. $buildings = array();
  10. while($building = $database->fetch($result)) {
  11. $buildings[$building['id']] = $building;
  12. }
  13.  
  14. if(!empty($_POST['build'])) {
  15. $building_id = (int)$_POST['building_id'];
  16. $amount = $_POST['amount'];
  17.  
  18. try {
  19. if($player->money < $buildings[$building_id]['cost']) {
  20. throw new Exception("You do not have enough money to build this!");
  21. }
  22.  
  23. // Purchase technique
  24. $player->money -= $buildings[$building_id]['cost'] * $amount;
  25. $database->query("INSERT INTO player_buildings (owner_id, name, power_use, life, att_inf, att_veh, att_air, att_sea)
  26. VALUES ('$_SESSION[user_id]', '$buildings[name]', '$power_use', '$life', '$att_inf', '$att_veh', '$att_air', '$att_sea')");
  27. $player->update();
  28.  
  29. echo "Debugg: Buildings have been added.... I hope..";
  30. } catch (Exception $e) {
  31. echo $e->getMessage();
  32. }
  33.  
  34. }
  35. // Display form
  36. echo "<table style='width:900px;'>
  37. <tr>
  38. <th style='width:40% text-align:left;'>Name</th>
  39. <th style='width:50%;'>Description</th>
  40. <th style='width:5%;'>Price</th>
  41. <th style='width:5%;'>Power Usage</th>
  42. <th style='width:5%;'>&nbsp;</th>
  43. </tr>";
  44. foreach($buildings as $id => $building) {
  45. echo "<tr>
  46. <td>{$building['name']}</td>
  47. <td>{$building['description']}</td>
  48. <td>{$building['cost']}</td>
  49. <td>{$building['power_use']}</td>
  50. <td>
  51. <form action='$self_link' method='POST'>
  52. <input type='hidden' name='building_id' value='$id' />
  53. <input style='width:40px' type='number' name='amount' value='amount' />
  54. <input type='submit' name='build' value='Build' />
  55. </form>
  56. </td>
  57. </tr>";
  58. }
  59.  
  60. echo "</table>";
  61. }
  62.  
  63. $buildings[$building['id']] = $building;
  64. ^^^^^^^^^^^^^^^
  65.  
  66. VALUES ('$_SESSION[user_id]', '$buildings[name]', '$power_[..snip..]
  67. ^^^^
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement