H
H
Hazrat Hajikerimov2014-02-14 12:18:23
PHP
Hazrat Hajikerimov, 2014-02-14 12:18:23

Sorted catalog: how to solve the problem with sorted output?

I create a catalog of computer equipment with filters. All filters are pre-parsed and everything you need is in the SQL database.
I decided to make the selection of goods using Ajax for faster loading of the site. for example, a person went to the site, the page loaded quickly, then a beautiful animation that the goods are loading, at this time a request to the Class, and it returns json of the product category, using the jQuery .append () method I add to the site.
Everything looks like this.
ea41fb35a267.png

Next comes the checkbox.
When choosing values, let's say you clicked ASUS, the page sends an Ajax request to the server, in the same place these parameters are saved in

$_SESSION['sorted_string'] = array(
    'Производитель' => array('ASUS','ZOTAC')
);

and with these parameters, this class is for selecting goods

protected function sql (){
        self::$sql->where('*','catalog'); // составляем запрос типа SELECT * FROM catalog
        self::$sql->join('options','catalog.art','options.art'); // объеденяем таблицы в одно
        self::$sql->add('catalog_3',self::$catalog); // условие WHERE catalog_3 = Видеокарты
        self::$sql->add('price','0','!='); // фильтруем без ценника
        self::$sql->add('avail','0','!='); // фильтруем отсутствующие
        self::sql_sort();  // ОБРАТИТЕ ВНИМАНИЕ на этот метод
        self::$sql->query();  
        self::sorted_data(); // ASC или DESC
        self::$sql->limit(self::$start,self::$num);  // организовать пагинатор
        self::$sql->query();  
        self::$res = self::$sql->arr(); // вот тут готовый Массив с товарами 

    }


the actual question, having received values ​​of type name=>value, how to sort the query if the attribute data is serialized in the base in the option table, when fetching and unserializing, the array looks like this
array (
  'Цена' => '26.7',
  'Производитель' => 'PALIT',
  'Графический процессор' => 'nVidia GeForce 210',
  'Частота графического процессора' => 589,
  'Объем видеопамяти' => 512,
  'Тип видеопамяти' => 'DDR3',
  'Частота видеопамяти' => 625,
  'Разрядность шины видеопамяти' => 32,
  'Интерфейс' => 'PCI-E 2.0',
  'Система охлаждения' => 'активное',
  'Особенности' => 
  array (
    0 => 'Разъем DVI',
    1 => 'Разъем VGA',
    2 => 'Разъем HDMI',
  ),
)


and the method for sorting
protected function sql_sort (){
        if (self::$ajax_start){
            if(empty(self::$ajax_name) and empty(self::$ajax_value)){return;}
            if (isset($_SESSION['sorted_string'])){
                if (!in_array(self::$ajax_value,$_SESSION['sorted_string'])){
                    $_SESSION['sorted_string'][] = self::$ajax_value;
                }
                foreach ($_SESSION['sorted_string'] as $el){
                     // вот тут нужно сортировать 
                }
            }else{
                $_SESSION['sorted_string'][] = self::$ajax_value;
            }
            // и тут если это новое значение
        }
    }


That is, the problem is that there is an array of parameters in $_SESSION , by which you need to sort all this, but before sorting, you need to make a request to GET AN ARRAY OF PARAMETERS with an option, this is a request, then process each product from the category and see if it has array value corresponding.
And let's say we have 100 suitable products. But how to enter them into SQL, do you need to create 100 conditions?

That's the problem I faced and did not sleep at night.

Who will help?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FanatPHP, 2014-02-14
@FanatPHP

if the attribute data is serialized in the base in the option table,

Take the rail, and for a long time with pleasure beat on the head of the one who began to store attribute data serialized in the database in the option table. Then add in the kidneys.
Then redesign the base.
Make categories in a normal table, attach to the query and sort by them.

F
fabrykant, 2014-02-14
@fabrykant

create a table with attributes:
attribute_id | product_id | attribute_name | attribute_value

Q
Quber, 2014-02-14
@Quber

I didn't quite understand what was needed. And sorting cannot be organized on the client side by means, knockout, for example? Or do you need to make a correct selection from the database?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question