Good day! It is necessary to sort the list of displayed positions by name, or alternatively screw the button for sorting. Well there is such a code.

<? if( !isset( $_COOKIE['compare'] ) || $_COOKIE['compare'] == '{}'){ $compare_html = '<p class="no-compare">Не выбраны товары для сравнения</p>'; }else{ $data = json_decode($_COOKIE['compare'],true); $in_arr = implode(',',$data); $sql = "SELECT `i`.`id` as `type_id`, `i`.`name` as `type_name`, `c`.`id` as `cat_id`, `c`.`name` as `cat_name`, `ci`.`id` as `item_id`, `jva`.`attr` as `attr_id`, `jva`.`name` as `attr_name`, `jva`.`value` as `attr_value` FROM `plug_cat_join_cat_item` as `ci` JOIN ( SELECT `v`.`write_id`, `v`.`attr`, `v`.`value`, `a`.`name` FROM `plug_cat_vals` as `v` INNER JOIN `plug_cat_attr` as `a` ON `a`.`id` = `v`.`attr` ) `jva` ON `jva`.`write_id` = `ci`.`id` INNER JOIN `plug_cat_item` as `i` ON `i`.`id` = `ci`.`item` INNER JOIN `plug_cat_cat` as `c` ON `c`.`id` = `ci`.`cat` WHERE `ci`.`id` IN (".$in_arr.") ORDER BY `item_id`" $itms = $db->GetTable($sql); $items = array(); foreach ($itms as $item) { //$i = isset($items[$item['cat_id']]) ? sizeof($items[$item['cat_id']])+1 : 0; $items[$item['cat_id']][$item['item_id']]['id'] = $item['item_id']; $items[$item['cat_id']][$item['item_id']]['cat_id'] = $item['cat_id']; $items[$item['cat_id']][$item['item_id']]['cat_name'] = $item['cat_name']; if( $item['attr_id'] == 1 ){ $items[$item['cat_id']][$item['item_id']]['item_name'] = ( $item['attr_value']!='' ) ? $item['attr_value'] : $item['type_name']; } if( $item['attr_id'] == 3 ){ $items[$item['cat_id']][$item['item_id']]['item_price'] = ( $item['attr_value']!='' ) ? $item['attr_value'].' руб.' : 'Не указано'; } if( $item['attr_id'] == 6 ){ $items[$item['cat_id']][$item['item_id']]['item_image'] = ( $item['attr_value']!='' ) ? $item['attr_value'] : '/plugins/catalog/front/img/default.gif'; } if( $item['attr_id'] > 11 ) { $items[$item['cat_id']][$item['item_id']]['attrs'][$item['attr_id']]['name'] = $item['attr_name']; $items[$item['cat_id']][$item['item_id']]['attrs'][$item['attr_id']]['attr'] = $item['attr_id']; $items[$item['cat_id']][$item['item_id']]['attrs'][$item['attr_id']]['value'] = $item['attr_value'];; } } //$items = array_values($items); $compare_html = '<a class="compare_print" href="javascript:window.print()">Распечатать страницу</a>'; foreach ($items as $cid => $itm) { $table = '<p class="compare_table_name">'.$itm[key($itm)]['cat_name'].'</p> <table class="compare_table"> <thead> <tr> <th>Товар</th> <th>Цена</th>'; foreach ($itm[key($itm)]['attrs'] as $key => $value) { $table .=' <th>'.$value['name'].'</th>'; } $table .=' </tr> </thead> <tbody>'; $row = 1; foreach ($itm as $item) { $class = $row%2 == 0 ? 'odd' :''; $table .= '<tr class="'.$class.'"> <td class="item-column"> <a class="item-link" href="'.$catalog->getItemURL($item['id']).'"> <img src="'.$item['item_image'].'" width="100px" /> <span>'.$item['item_name'].'</span> <a href="#" class="del-compare" onClick="delCompare('.$item['id'].');location.reload();">Убрать из сравнения</a> </a> </td> <td>'.$item['item_price'].'</td>'; foreach ($item['attrs'] as $key => $value) { $table .= '<td>'.$value['value'].'</td>'; } $table .= '</tr>'; $row++; } $table .= '</tbody> </table>'; $compare_html .= $table; } } ?> <div class="catalog"> <div class="compare"> <?=$compare_html?> </div> </div> 

In general, okay ... knowledge network ... nothing is a knowledge network, where people don't understand php, giving superficial answers ... So what does sql inq mean? that is, someone got access by injection ?!

  • > <? = $ vvv ['name'], sort ($ vvv);?> Is that what? 0_o What do you want to say in the middle of the link? - Opalosolo
  • sort ($ vvv); we exclude. Sorry .. this is a mistake - JhonnyMnemonik
  • 2
    It is better to sort the database tools, if the data from it and have such an opportunity. - Alexey Motylev
  • website on php + mysql - JhonnyMnemonik
  • Tried the type_name did not lead to the result. - JhonnyMnemonik

1 answer 1

If you sort using php, it turns out something like this:

 //объявить ф-цию 1 раз где-нибудь вне циклов function my_cmp($a, $b) { return strcmp($a["item_name"], $b["item_name"]); } //перед foreach ($itm as $item) { вызывать usort($itm, "my_cmp"); 

Read more: http://www.php.net/manual/ru/function.usort.php