A
A
alestro2016-02-27 13:45:28
PHP
alestro, 2016-02-27 13:45:28

How can attribute output be refactored?

The database has 4 tables. The first one is attributes_group, it contains fields: id,name,sort_order. The second is attributes(id, name, attributes_groud_id(foregin key for attributes_group.id, sort_order) The third is attributes_value(id,int,str,attributes_id(foregin key for attributes.id)) The fourth is products_attributes(id,products_id(fk for products.id), attributes_id(fk for attributes.id))
, there is also this class:

class Attributes{
  private $db;
  public $group;
  public $name;
  public $value;
  
  public function __construct(\core\Database $db){
    $this->db=$db;
  }
  
  public function getProductsAttributes($pid){
    $sth=$this->db->Q
    (
      [
        'sql'=>'SELECT id,name FROM attributes_group as ag WHERE ag.id IN (SELECT a.attributes_group_id FROM attributes as a join products_attributes as pa on a.id=pa.attributes_id where pa.products_id =?) order by sort_order',
        'bind'=>
          [
            $pid
          ]
      ]
    );
    while($row=$sth->fetch(\PDO::FETCH_ASSOC)){
      $this->group[$row['id']]=$row['name'];
    }
    $sth=$this->db->Q
    (
      [
        'sql'=>'select name, attributes_group_id as id, `int`,str  from attributes as a join products_attributes as pa on pa.attributes_id = a.id join attributes_value as av on av.attributes_id = a.id where pa.products_id = ? order by sort_order',
        'bind'=>
          [
            $pid
          ]
      ]
    );
    while($row=$sth->fetch(\PDO::FETCH_ASSOC)){
      $this->name[$row['id']][]=$row['name'];
      empty($row['int'])?$this->value[$row['id']][]=$row['str']:$this->value[$row['id']][]=$row['int'].' '.$row['str'];
    }
    
    return $this;
  }
}

I make two queries to the database, in order to be able to sort groups and attributes, respectively.
As a result, I get an object with product attributes and output something like this:
<table class="table table-bordered">
                                   
                                    <?php foreach($a->group as $key=>$value){?>
                                        <tr>
                                         <th><?=$value?></th>
                                        </tr>
                                        <tr>
                                        <?php foreach($a->name[$key] as $key2=>$value2){?>
                                            <td width="200"><?=$value2?></td>
                                            
                                            <td><?=$a->value[$key][$key2]?></td>
                                        </tr>
                                        <? } }?>
                                    </table>

How can you implement all this so that it would be more kosher?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question