Jump to content
MakeWebGames

Each inside each issue


Sim

Recommended Posts

public function constructModule() {

     //get user inventory first
     $rec = $this->db->prepare("
     SELECT
       IV_id AS 'id',
       IV_quantity AS 'amount',
       IT_name as 'type',  
       I_name AS name,
       I_rank as 'rank',
       ROUND(I_damage / 100, 2) as 'damage'
     FROM
       inventory
     JOIN
       items
     JOIN
       itemTypes
     WHERE
       IV_userID=:user
     AND
       I_id = IV_itemID
     AND
       IT_id = I_type
     ");

   $rec->bindParam(":user", $this->user->id);
   $rec->execute();
   //$allItems = array();
   $inv = $rec->fetchAll(PDO::FETCH_ASSOC);
   
   //get item types
   $types = $this->db->prepare("
   SELECT
       IT_id AS 'id',
       IT_name as 'type'
     FROM
       itemTypes
     ORDER BY IT_id ASC
     ");
   
   $types->execute();
   $allItems = array();
   
   while($type = $types->fetchObject())
   {
     $allItems[]["type"] = $type->type;
     
     $items = $this->db->prepare("
     SELECT
       I_id AS id,
       I_name AS name,
       I_rank as 'rank',
       ROUND(I_damage / 100, 2) as 'damage'
     FROM 
       items
     WHERE
       I_type=:typeID
     ");
     
     $items->bindParam(":typeID", $type->id);
     $items->execute();
     
     while($item = $items->fetchObject()) {
       $allItems[]["item"]["name"] = $item->name;
       $allItems[]["item"]["id"] = $item->id;
     }
     
     
     //$allItems[]->item = $item;
     
   }
   var_dump($allItems);
   
   $this->html .= $this->page->buildElement("blackMarket", array(
                "location" => $this->user->getLocation(), 
                "inventory" => $inv,
                "itemType" => $allItems
            ));
}

 

My vardump of $allItems:

array(12) { 
  [0]=> array(1) { 
    ["type"]=> string(6) "Weapon" 
  } 
  [1]=> array(1) { 
    ["item"]=> array(1) { 
      ["name"]=> string(11) "Test weapon" 
    } 
  } 
  [2]=> array(1) { 
    ["item"]=> array(1) { 
      ["id"]=> string(1) "1" 
    } 
  } 
  [3]=> array(1) { 
    ["item"]=> array(1) { 
      ["name"]=> string(8) "Ball bat" 
    } 
  } 
  [4]=> array(1) { 
    ["item"]=> array(1) { 
      ["id"]=> string(1) "4" 
    } 
  } 
  [5]=> array(1) { 
    ["type"]=> string(5) "Armor" 
  } 
  [6]=> array(1) { 
    ["item"]=> array(1) { 
      ["name"]=> string(11) "Test Armour" 
    } 
  } 
  [7]=> array(1) { 
    ["item"]=> array(1) { 
      ["id"]=> string(1) "3" 
    } 
  } 
  [8]=> array(1) { 
    ["type"]=> string(3) "Hat" 
  } 
  [9]=> array(1) { 
    ["type"]=> string(4) "Shoe" 
  } 
  [10]=> array(1) { 
    ["item"]=> array(1) { 
      ["name"]=> string(19) "First edition Nikes" 
    } 
  } 
  [11]=> array(1) { 
    ["item"]=> array(1) { 
      ["id"]=> string(1) "2" 
    } 
  } 
}

The problem is I can't get the items to show. The inventory shows.

Link to comment
Share on other sites

I don't know the engine that well, but those arrays have caught my attention.
Humour me, change to
 

public function constructModule() {

     //get user inventory first
     $rec = $this->db->prepare("
     SELECT
       IV_id AS 'id',
       IV_quantity AS 'amount',
       IT_name as 'type',  
       I_name AS name,
       I_rank as 'rank',
       ROUND(I_damage / 100, 2) as 'damage'
     FROM
       inventory
     JOIN
       items
     JOIN
       itemTypes
     WHERE
       IV_userID=:user
     AND
       I_id = IV_itemID
     AND
       IT_id = I_type
     ");

   $rec->bindParam(":user", $this->user->id);
   $rec->execute();
   //$allItems = array();
   $inv = $rec->fetchAll(PDO::FETCH_ASSOC);
   
   //get item types
   $types = $this->db->prepare("
   SELECT
       IT_id AS 'id',
       IT_name as 'type'
     FROM
       itemTypes
     ORDER BY IT_id ASC
     ");
   
   $types->execute();
   $allItems = array();
   
   while($type = $types->fetchObject())
   {
     $allItems["type"][] = $type->type;
     
     $items = $this->db->prepare("
     SELECT
       I_id AS id,
       I_name AS name,
       I_rank as 'rank',
       ROUND(I_damage / 100, 2) as 'damage'
     FROM 
       items
     WHERE
       I_type=:typeID
     ");
     
     $items->bindParam(":typeID", $type->id);
     $items->execute();
     
     while($item = $items->fetchObject()) {
       $allItems["item"]["name"][] = $item->name;
       $allItems["item"]["id"][] = $item->id;
     }
     
   }
   
   $this->html .= $this->page->buildElement("blackMarket", array(
                "location" => $this->user->getLocation(), 
                "inventory" => $inv,
                "itemType" => $allItems
            ));
}

 

Link to comment
Share on other sites

Still don't work. My TPL       

public $blackMarket = '


            <div class="panel panel-default">
                <div class="panel-heading">{location} Black Market</div>
                <div class="panel-body">
            

                    <div class="row">
                        <div class="col-md-6">
                            <h4>INVENTORY</h4>
             {#each inventory}
                                <div class="crime-holder">
                                    <p>
                                        <span class="action">
                                            {name}
                                        </span> 
                                        <span class="cooldown">
                                            {type}
             {/if}
                                        </span> 
                                           <span class="commit">  
                                            <a href="?page=blackmarket&action=sell&item={id}">Sell</a>
                                           </span>

                                    </p>
                                </div>
           {/each}
                      {#unless inventory}
                                <div class="text-center">
                                    <em>There are no weapons for you to buy</em>
                                </div>
                     {/unless}
                        </div>
                        
                  {each itemType}
                        <div class="col-md-6">
                            <h4>{type}</h4>
                            {#each item}
                                <div class="crime-holder">
                                    <p>
                                        <span class="action">
                                            {name} 
                                        </span> 
                                        <span class="cooldown">
                                            {#if cost} ${number_format cost} {/if}
                                            {#if points}
                                                {#if cost} + {/if}
                                                {number_format points} {_setting "pointsName"}
                                            {/if}
                                        </span> 
                                        {#unless cantBuy}
                                            <span class="commit">
                                                {#if owned}
                                                    Owned
                                                {/if}
                                                {#unless owned}
                                                    <a href="?page=blackmarket&action=buy&item={id}">
                                                        Buy
                                                    </a>
                                                {/unless}
                                            </span>
                                        {/unless}
                                    </p>
                                </div>
                            {/each}
                            {#unless item}
                                <div class="text-center">
                                    <em>There is no armor to buy</em>
                                </div>
                            {/unless}

                        </div>
                    </div>
                    
                 {/each}
                </div>
            </div>

        ';

 

Link to comment
Share on other sites

I may be mistaken here, but I'm sure you only need to write one query for this. Also feels like you may be doing more work than needed.

Surely this returns both $inv and $allItems in the correct format? Without seeing the code / database, I'm winging it a little though. I also make the assumption that the resource, $rec, can be used twice.

<?php
public function constructModule()
{
  $sql = "SELECT IV_id AS 'id', IV_quantity AS 'amount', IT_id AS 'type_id',
                 IT_name as 'type', I_name AS name, I_rank as 'rank',
                 ROUND(I_damage / 100, 2) as 'damage', I_id AS 'item_id'
            FROM inventory
            JOIN items
            JOIN itemTypes
           WHERE IV_userID = :user
             AND I_id = IV_itemID
             AND IT_id = I_type";
  $rec = $this->db->prepare($sql);
  $rec->bindParam(":user", $this->user->id);
  $rec->execute();
  $inv = $rec->fetchAll(PDO::FETCH_ASSOC);

  $allItems = [];

  foreach ($inv as $inventory) {
    $allItems['type'][$inventory->type]['item'][] = [
      'id'   => $inventory->item_id,
      'name' => $inventory->name,
    ]
  }

  var_dump($allItems);

  $this->html .= $this->page->buildElement('blackMarket', [
    'location'  => $this->user->getLocation(), 
    'inventory' => $inv,
    'itemType'  => $allItems
  ));
}

 

  • Like 2
Link to comment
Share on other sites

I'll let you know very shortly. Nice work!

That knocked about 60 lines of code off. 🙂

 

But here's the errors I kept running into before. The reason I did it the way I did it I think

Edit: now that I'm fully awake,  I need two queries. One gets all items in game(black market) whole other gets users inventory.

Maybe this will be able to help me though

foreach ($inv as $inventory) {
    $allItems['type'][$inventory->type]['item'][] = [
      'id'   => $inventory->item_id,
      'name' => $inventory->name,
    ]
  }

Edit: latest vardump, code and errors



   //get items
   $rec = $this->db->prepare("
    SELECT
       I_id AS itemID,
       I_name AS name,
       I_rank as 'rank',
       ROUND(I_damage / 100, 2) as 'damage',
       IT_id AS 'typeID',
       IT_name as 'type'
    FROM
       itemTypes
    JOIN
       items
    WHERE
      I_type=IT_id
    ORDER BY 
      IT_id ASC
  ");
  
  

  $rec->execute();
  
  $items = $rec->fetchAll(PDO::FETCH_ASSOC);
  
  $allItems = [];
  
  foreach ($items as $item) {
      $allItems['type'][$item['type']]['item'][] = [
        'id'   => $item['itemID'],
        'name' => $item['name']
      ];
    }     
   
   var_dump($allItems);
   
   $this->html .= $this->page->buildElement("blackMarket", array(
                "location" => $this->user->getLocation(), 
                "inventory" => $inv,
                "itemType" => $allItems
            ));

[/Code]

array(1) { ["type"]=> array(4) { ["Weapon"]=> array(1) { ["item"]=> array(3) { [0]=> array(2) { ["id"]=> string(1) "7" ["name"]=> string(9) "Smokiness" } [1]=> array(2) { ["id"]=> string(1) "4" ["name"]=> string(8) "Ball bat" } [2]=> array(2) { ["id"]=> string(1) "1" ["name"]=> string(11) "Test weapon" } } } ["Armor"]=> array(1) { ["item"]=> array(3) { [0]=> array(2) { ["id"]=> string(1) "5" ["name"]=> string(7) "Testers" } [1]=> array(2) { ["id"]=> string(1) "3" ["name"]=> string(11) "Test Armour" } [2]=> array(2) { ["id"]=> string(1) "8" ["name"]=> string(10) "Will other" } } } ["Hat"]=> array(1) { ["item"]=> array(1) { [0]=> array(2) { ["id"]=> string(1) "6" ["name"]=> string(9) "Hhhhsjshd" } } } ["Shoe"]=> array(1) { ["item"]=> array(1) { [0]=> array(2) { ["id"]=> string(1) "2" ["name"]=> string(19) "First edition Nikes" } } } } }

The log file minor.txt is not writable!

There was an error!

File: /home/simmakew/public_html/GL/class/templateRender.php
Line: 86
Error: htmlspecialchars() expects parameter 1 to be string, array given
Type: E_RECOVERABLE_ERROR

There was an error!

File: /home/simmakew/public_html/GL/class/templateRender.php
Line: 90
Error: Variable passed to each() is not an array or object
Type: E_RECOVERABLE_ERROR

I can't access the code thing to add code in posts as code tag

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...