Thread: Mys 1.3.4 Shop Listing Display
View Single Post
  #8  
Old 04-24-2015, 04:41 PM
Kyttias's Avatar
Kyttias Kyttias is offline
Premium Member
 
Join Date: Jan 2014
Posts: 857
Gender: Unknown/Other
Credits: 88,498
Kyttias is on a distinguished road
Default

Ok, since this was exactly similar to a problem you had on instructions in another thread, let me explain clearly what you didn't do, and therefore why it's not working.

This is the foreach loop:
PHP Code:
foreach($this->items as $stockitem){
        
$item $this->getitem($stockitem);
    
$cells = new LinkedList;         
    
$cells->add(new TCell(new Image($item->imageurl)));
    
$cells->add(new TCell($item->category));
    
$cells->add(new TCell($item->itemname));
    
$cells->add(new TCell($item->description));
    
$cells->add(new TCell($item->price));
    
$cells->add(new TCell($itemList->getHelper()->getItemPurchaseForm($this$item)));
    
$itemList->buildRow($cells);            

You were supposed to wrap it inside the if statement:
PHP Code:
if ($this->status == "open"){
    foreach(
$this->items as $stockitem){
            
$item $this->getitem($stockitem);
        
$cells = new LinkedList;         
        
$cells->add(new TCell(new Image($item->imageurl)));
        
$cells->add(new TCell($item->category));
        
$cells->add(new TCell($item->itemname));
        
$cells->add(new TCell($item->description));
        
$cells->add(new TCell($item->price));
        
$cells->add(new TCell($itemList->getHelper()->getItemPurchaseForm($this$item)));
        
$itemList->buildRow($cells);            
    }

This is what you have in what you sent me:
PHP Code:
if ($this->status == "open"){
    foreach(
$this->items as $stockitem){
            
$item $this->getitem($stockitem);
        
$cells = new LinkedList;         
        
$cells->add(new TCell(new Image($item->imageurl)));
        
$cells->add(new TCell($item->category));
        
$cells->add(new TCell($item->itemname));
        
$cells->add(new TCell($item->description));
        
$cells->add(new TCell($item->price));
        
$cells->add(new TCell($itemList->getHelper()->getItemPurchaseForm($this$item)));
        
$itemList->buildRow($cells);            
    } 
The foreach closes still, but the if statement does not. There is only one curly brace at the end. You must wrap something completely. It's always best to line up code so that the end curly brace is directly below the thing that opened it - so you can visually make sure that it's indeed closed.

Then, the next thing you did was add the second if statement, checking if the shop is closed, outside of the display function, rather than inside of it, as instructed.

This is what you have:
PHP Code:
    public function display(){
        
$mysidia Registry::get("mysidia");
        
$document $mysidia->frame->getDocument();              
        
$document->addLangvar($mysidia->lang->select_item);
      
        if(
$this->gettotal() == 0){
            
$document->addLangvar($mysidia->lang->empty);
            return 
FALSE;
        }     
      
        
$itemList = new TableBuilder("shop");
        
$itemList->setAlign(new Align("center""middle"));
        
$itemList->buildHeaders("Image""Category""Name""Description""Price""Buy");    
        
$itemList->setHelper(new ShopTableHelper);
      
      if (
$this->status == "open"){
        foreach(
$this->items as $stockitem){
              
$item $this->getitem($stockitem);
            
$cells = new LinkedList;         
            
$cells->add(new TCell(new Image($item->imageurl)));
            
$cells->add(new TCell($item->category));
            
$cells->add(new TCell($item->itemname));
            
$cells->add(new TCell($item->description));
            
$cells->add(new TCell($item->price));
            
$cells->add(new TCell($itemList->getHelper()->getItemPurchaseForm($this$item)));
            
$itemList->buildRow($cells);
            
        }      
        
$document->add($itemList);  
    }
  
  if (
$this->status == "closed"){ $document->add(new Comment("Sorry, this shop is closed.")); } 
This is what you SHOULD have, if you had followed the instructions:
PHP Code:
public function display(){
    
$mysidia Registry::get("mysidia");
    
$document $mysidia->frame->getDocument();              
    
$document->addLangvar($mysidia->lang->select_item);
  
    if(
$this->gettotal() == 0){
        
$document->addLangvar($mysidia->lang->empty);
        return 
FALSE;
    }     
  
    
$itemList = new TableBuilder("shop");
    
$itemList->setAlign(new Align("center""middle"));
    
$itemList->buildHeaders("Image""Category""Name""Description""Price""Buy");    
    
$itemList->setHelper(new ShopTableHelper);
  
      if (
$this->status == "open"){
        foreach(
$this->items as $stockitem){
              
$item $this->getitem($stockitem);
            
$cells = new LinkedList;         
            
$cells->add(new TCell(new Image($item->imageurl)));
            
$cells->add(new TCell($item->category));
            
$cells->add(new TCell($item->itemname));
            
$cells->add(new TCell($item->description));
            
$cells->add(new TCell($item->price));
            
$cells->add(new TCell($itemList->getHelper()->getItemPurchaseForm($this$item)));
            
$itemList->buildRow($cells);
        }
    }
    if (
$this->status == "closed"){ $document->add(new Comment("Sorry, this shop is closed.")); }      
    
$document->add($itemList);  

And, finally, here is the entire document, just in case:
PHP Code:
<?php

use Resource\Collection\LinkedList;

class 
Itemshop extends Model{

    public 
$sid;
    public 
$category;
    public 
$shopname;
    public 
$shoptype;
    public 
$description;
    public 
$imageurl;
    public 
$status;
    public 
$restriction;
    public 
$salestax;
    public 
$items;
    protected 
$total 0;

    public function 
__construct($shopname){
        
// Fetch the database info into object property
        
$mysidia Registry::get("mysidia");
        
$row $mysidia->db->select("shops", array(), "shopname ='{$shopname}'")->fetchObject();        
        if(!
is_object($row)) throw new Exception("Invalid Shopname specified");

        
// loop through the anonymous object created to assign properties
        
foreach($row as $key => $val){
            
$this->$key $val;
        }
        
$this->items $this->getitemnames();
        
$this->total = (is_array($this->items))?count($this->items):0;
    }

    public function 
getcategory(){
        
$mysidia Registry::get("mysidia");
        
$stmt $mysidia->db->select("shops", array(), "category ='{$this->category}'");
        
$cate_exist = ($row $stmt->fetchObject())?TRUE:FALSE;
        return 
$cate_exist;
    }

    public function 
getshop(){
        
$mysidia Registry::get("mysidia");        
        if(empty(
$this->shopname)) $shop_exist FALSE;
        else{
            
$stmt $mysidia->db->select("shops", array(), "shopname ='{$this->shopname}'");
            
$shop_exist = ($row $stmt->fetchObject())?TRUE:FALSE;
        }
        return 
$shop_exist;
    }

    public function 
getitemnames(){
        if(!
$this->items){
            
$mysidia Registry::get("mysidia");
            
$stmt $mysidia->db->select("items", array("itemname"), "shop ='{$this->shopname}'");
            
$items = array();

            while(
$item $stmt->fetchColumn()){
                
$items[] = $item;
            }
            return 
$items;
        }
        else return 
$this->items;
    }

    public function 
gettotal(){
        return 
$this->total;
    }

    public function 
display(){
        
$mysidia Registry::get("mysidia");
        
$document $mysidia->frame->getDocument();
        
$document->addLangvar($mysidia->lang->select_item);
        
        if(
$this->gettotal() == 0){
            
$document->addLangvar($mysidia->lang->empty);
            return 
FALSE;
        }

        
$itemList = new TableBuilder("shop");
        
$itemList->setAlign(new Align("center""middle"));
        
$itemList->buildHeaders("Image""Category""Name""Description""Price""Buy");
        
$itemList->setHelper(new ShopTableHelper);
        
        if(
$this->status == "open"){
            foreach(
$this->items as $stockitem){
                
$item $this->getitem($stockitem);
                
$cells = new LinkedList;
                
$cells->add(new TCell(new Image($item->imageurl)));
                
$cells->add(new TCell($item->category));
                
$cells->add(new TCell($item->itemname));
                
$cells->add(new TCell($item->description));
                
$cells->add(new TCell($item->price));
                
$cells->add(new TCell($itemList->getHelper()->getItemPurchaseForm($this$item)));
                
$itemList->buildRow($cells);
            }
        }
        if(
$this->status == "closed"){
            
$document->add(new Comment("Sorry, this shop is closed."));
        }

        
$document->add($itemList);
    }

    public function 
getitem($itemname){
        return new 
StockItem($itemname);
    }

    public function 
purchase(Item $item){
        
$mysidia Registry::get("mysidia");        
        if(
$item->owner != $mysidia->user->username) Throw new NoPermissionException('Something is very very wrong, please contact an admin asap.');
        else{
            
$item->quantity $mysidia->input->post("quantity");
            
$cost $item->getcost($this->salestax$item->quantity);
            
$moneyleft $mysidia->user->money $cost;
            if(
$moneyleft >= and $item->quantity 0){
                
$purchase $item->append($item->quantity$item->owner);
                
$mysidia->db->update("users", array("money" => $moneyleft), "username = '{$item->owner}'");
                
$status TRUE;
            }
            else throw new 
InvalidActionException($mysidia->lang->money);
        }
        return 
$status;
    }

    public function 
rent($item$period){

    }

    public function 
execute($action){

    }

    protected function 
save($field$value){
        
$mysidia Registry::get("mysidia");
        
$mysidia->db->update("shops", array($field => $value), "sid='{$this->sid}' and shoptype = 'adoptshop'");
    }
}
?>
It's ready to just copy and paste in, but please try to understand where things went wrong.

You might want to take a basic coding course at code Codecademy if you haven't yet - it's totally free and only takes a couple hours.
__________________
Please do not contact me directly outside of Mysidia.
I also cannot troubleshoot code more than two years old - I legit don't remember it.

Last edited by Kyttias; 04-24-2015 at 04:50 PM.
Reply With Quote