2011-11-23 6 views
4

제 삼항 연산자 할당이 제 배열의 두 번째 부분에서 작동하지 않습니다. 누구든지 내가 뭘 잘못하고 있는지 알아? 그 permalink 필드에 대한 값이 있는지 그리고 단지 배열에 link_url을 삽입하지 않으면 볼 수 있습니다.인라인 삼항 연산자가 작동하지 않습니다.

function getSiteMap() 
{ 
    $this->db->select('site_menu_structures_links.id, site_menu_structures_links.link_name'); 
    $this->db->from('site_menu_structures_links'); 
    $this->db->where('site_menu_structures_links.status_id', 1); 
    $this->db->where('site_menu_structures_links.is_category', 'Yes'); 
    $this->db->order_by('site_menu_structures_links.sort_order'); 
    $catQuery = $this->db->get(); 

    if ($catQuery->num_rows()) 
    { 
     foreach ($catQuery->result() as $cats) 
     { 
      // Set the Main Category into the array 
      $testArray[$cats->id] = array(
       'id' => $cats->id, 
       'name' =>$cats->link_name 
      ); 

      $this->db->select('site_content_pages.permalink, site_menu_structures_links_children.id, site_menu_structures_links_children.link_url, site_menu_structures_links_children.link_name'); 
      $this->db->from('site_menu_structures_links_children'); 
      $this->db->join('site_content_pages', 'site_content_pages.id = site_menu_structures_links_children.site_content_pages_id'); 
      $this->db->where('site_menu_structures_links_id', $cats->id); 
      $this->db->where('site_menu_structures_links_children.status_id', 1); 
      $this->db->order_by('site_menu_structures_links_children.sort_order'); 
      $childrenQuery = $this->db->get(); 



      if ($childrenQuery->num_rows()) 
      { 
       foreach ($childrenQuery->result() as $child) 
       { 
        $testArray[$cats->id]['children'][$child->id] = array(
         'linke_url' => (empty($child->permalink)) ? $child->link_url : $child->permalink, 
         'link_name' => $child->link_name, 
        ); 
       } 
      } 
     } 
    } 

    return $testArray; 
} 

편집 : 또한

없는 사회 배열과 그가 말하는 게 내부의 3 개 항목이되어야한다고. 내가 그 조인과 관련이 있는지 궁금합니다. 여기 내 출력입니다 :

Array 
(
[2] => Array 
    (
     [id] => 2 
     [name] => Roster 
     [children] => Array 
      (
       [1] => Array 
        (
         [linke_url] => 
         [link_name] => Superstars 
        ) 

       [2] => Array 
        (
         [linke_url] => 
         [link_name] => Champions and Contenders 
        ) 

       [3] => Array 
        (
         [linke_url] => 
         [link_name] => Title History 
        ) 

      ) 

    ) 

[3] => Array 
    (
     [id] => 3 
     [name] => Events 
     [children] => Array 
      (
       [4] => Array 
        (
         [linke_url] => 
         [link_name] => Preview Next Event 
        ) 

       [5] => Array 
        (
         [linke_url] => 
         [link_name] => Latest Event Results 
        ) 

       [6] => Array 
        (
         [linke_url] => 
         [link_name] => Event Archives 
        ) 

       [7] => Array 
        (
         [linke_url] => 
         [link_name] => Schedule An Event 
        ) 

      ) 

    ) 

[4] => Array 
    (
     [id] => 4 
     [name] => Media 
     [children] => Array 
      (
       [8] => Array 
        (
         [linke_url] => 
         [link_name] => Photo Gallery 
        ) 

      ) 

    ) 

[5] => Array 
    (
     [id] => 5 
     [name] => Social 
    ) 

) 
+0

작동하지 않는 기능은 무엇입니까? –

+0

'linke_url'이되어야하나요? 아마도'link_url' 또는 뭔가에 액세스하려하지 않고 오타입니다. 나는 $ child-> link_name 다음에 쉼표 때문에 오류가 있다고 생각했을 것입니다. 그러나 그렇지 않을 수도 있습니다. 이 경우 PHP 오류가 발생합니다. 특히 'NOTICE'유형의 오류가있을 수 있습니다. –

+0

또한 카테고리의 계층 구조를 만들려고한다는 것을 이해해야하며'site_menu_structures_links_id'는 내부적으로'site_menu_structures_links.id'를 참조하는 상위 ID입니다 .- 그렇다면 매우 최적화 될 수 있습니다 _ –

답변

10

들 :

'linke_url' => (empty($child->permalink)) ? $child->link_url : $child->permalink, 

난 당신이 'link_url'하지 'linke_url'을 의미 가정합니다, 그것은 당신이하려는 것 같습니다 있도록 :

경우 $child->permalink입니다 비어있는 경우 'link_url'에서 $child->link_url으로 설정하고 $child->permalink이 비어 있지 않은 경우 'link_url'에서 $child->permalink으로 설정하십시오. $b 어떤 값 $a = $b 달리 $a = $c있는 경우는, 즉 $btrue로 평가 될 경우와 동일 $a = $b ?: $c :

난 형태이다 ternary operator ?:의 축약 버전을 사용하는 것을 권장한다. 당신이 :

'link_url' => $child->permalink ?: $child->link_url 

$child->permalink 만약 그렇지 않으면, $child->link_url의 값이 사용되며, 사용되는 값을 가지고 있습니다. 행운을 빕니다!

+2

약식 3 진법은 5.3 –

+1

으로 만 제공됩니다. @BenSwinburne은 맞습니다. 이것은 5.3+에서만 사용 가능하며, 이전 버전에 상응하는 것은 약간 더 길다 :''link_url '=> $ child_permalink? $ child-> permalink : $ child-> link_url' –

관련 문제