2012-07-03 6 views
0

먼저 뷰에서 달성하고자하는 것을 보여주기 위해 유형별로 그룹화 된 엑스트라 목록을 보여줍니다. 나는 그것이 $extras 배열 매번 덮어 쓰기하는 것이 좋습니다 있다고 생각하지 않지만PHP 다차원 배열 만들기

<ul> 
{foreach $extras as $extra_type} 
<li> 
    <h2>{$extra_type.title}</h2> 
    <ul> 
     {foreach $extra_type.extras as $extra} 
     <li> 
      {$extra.name} 
     </li> 
     {/foreach} 
    </ul> 
</li> 
{/foreach} 
</ul> 

나는, 중복 엑스트라를 만드는 다음이있다. 이 작업을 수행하는 더 좋은 방법이 있습니까? 데이터베이스에만 1 개의 쿼리를 만드는 것을 선호합니다.

$stmnt = "SELECT e.*, ed.extra_description_name, ed.extra_description_description, 
      et.extra_type_title, ep.extra_price_amount, c.currency_htmlcode 
      FROM product_extras pe 
      LEFT JOIN extras e ON e.extra_id = pe.extra_id 
      LEFT JOIN extra_descriptions ed ON ed.extra_id = pe.extra_id 
      LEFT JOIN extra_prices ep ON ep.extra_id = pe.extra_id 
      LEFT JOIN extra_types et ON et.extra_type_id = e.extra_type_id 
      LEFT JOIN currencies c ON c.currency_id = ep.currency_id 
      WHERE pe.product_id = '{$this->id}' AND e.extra_status = 'ENABLED' 
      AND ed.language_id = 1 AND ep.currency_id = 1"; 

if ($isPublished) { 
    $stmnt .= " 
     AND IF(e.extra_publish_from IS NOT NULL, e.extra_publish_from, NOW()) <= NOW() 
     AND IF(e.extra_publish_until IS NOT NULL, e.extra_publish_until, NOW()) >= NOW()"; 
} 

$stmnt .= " ORDER BY e.extra_type_id, pe.product_extra_ordering, ed.extra_description_name"; 

$cache = Cache::getInstance()->newObject(300); 
$sig = Cache::getCacheName(sha1($stmnt)); 

if (!$res = $cache->load($sig)) { 
    $res = $db->fetchAll($stmnt); 
    $cache->save($res, $sig); 
} 

if(!empty($res)) { 
    foreach($res as $row) { 
     $extra[] = array(
      'id'  => $row['extra_id'], 
      'type'  => $row['extra_type_title'], 
      'weight' => $row['extra_weight'], 
      'image'  => $row['extra_image'], 
      'name'  => $row['extra_description_name'], 
      'description' => $row['extra_description_description'], 
      'price'  => Formatter::getInstance()->outFormat("currency", $row['extra_price_amount']), 
      'currency' => $row['currency_htmlcode'] 
     ); 

     $extras[$row['extra_type_id']] = array(
      'title' => $row['extra_type_title'], 
      'extras' => $extra 
     ); 
    } 
} 

이는 DB가

-- 
-- Table structure for table `extras` 
-- 

CREATE TABLE IF NOT EXISTS `extras` (
    `extra_id` int(11) NOT NULL AUTO_INCREMENT, 
    `extra_weight` float(4,1) NOT NULL COMMENT 'Weight in KG', 
    `extra_image` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `extra_available_all_countries` tinyint(1) NOT NULL, 
    `extra_type_id` int(11) DEFAULT NULL, 
    `extra_publish_from` int(11) DEFAULT NULL, 
    `extra_publish_until` int(11) DEFAULT NULL, 
    `extra_created` datetime NOT NULL, 
    `extra_created_by` int(11) NOT NULL, 
    `extra_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `extra_modified_by` int(11) NOT NULL, 
    `extra_status` enum('ENABLED','DISABLED') COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`extra_id`), 
    KEY `extra_typeFK` (`extra_type_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `extra_types` 
-- 

CREATE TABLE IF NOT EXISTS `extra_types` (
    `extra_type_id` int(11) NOT NULL AUTO_INCREMENT, 
    `extra_type_title` varchar(120) COLLATE utf8_unicode_ci NOT NULL, 
    PRIMARY KEY (`extra_type_id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

답변

0

문법을 지원하지 않는 템플릿 엔진이 될 수 있습니다.

<ul> 
{foreach $extras as $extra} 
    {if $_extra_type != $extra.type || [email protected]} 
    <li> 
     <h2>{$extra.type}</h2> 
     <ul> 
     {/if} 

      <li> 
       {$extra.type} - {$extra.name} 
      </li> 

     {if $_extra_type != $extra.type || [email protected]} 
     </ul> 
    </li> 
    {/if} 
    {assign var="_extra_type" value=$extra.type} 
{/foreach} 
</ul> 


if(!empty($res)) { 
    foreach($res as $row) { 
     $extras[] = array(
      'id'   => $row['extra_id'], 
      'type'   => $row['extra_type_title'], 
      'weight'  => $row['extra_weight'], 
      'image'   => $row['extra_image'], 
      'name'   => $row['extra_description_name'], 
      'description' => $row['extra_description_description'], 
      'price'   => Formatter::getInstance()->outFormat("currency", $row['extra_price_amount']), 
      'currency'  => $row['currency_htmlcode'] 
     ); 
    } 
} 
0

루프가 두 번째 foreach 문에

<ul> {foreach $extras as $extra_type} <li> 
    <h2>{$extra_type.title}</h2> 
    <ul> 
     {foreach $extra_type.extras as $extra} 
     <li> 
      {$extra.name} 
     </li> 
     {/foreach} 
    </ul> </li> {/foreach} 

잘못된 노트 $의 extra_type.extras입니다 도움이된다면.

내가 훨씬 쉬웠다보기에 논리를 포기하고 이동