2011-11-23 180 views
4

由于某种原因,我的三元运算符分配不适用于我的数组的第二部分。任何人看到我做错了什么?它应该只是看看是否有永久链接字段的值,如果没有,然后将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,如果是,可以_massively optimised_ –

回答

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

我建议使用ternary operator ?:其为形式的简写:$a = $b ?: $c其是相同的,如果$b的计算结果为true,即如果$b具有任何值,$a = $b,否则$a = $c。对于你:

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

如果$child->permalink有一个值,将被使用,否则,将被使用的$child->link_url值。祝你好运!

+2

被警告,速记三元只能从5.3 –

+1

@BenSwinburne是正确的;这仅在5.3+版本中可用,而早期版本的等效版本稍长一些:''link_url'=> $ child_permalink? $ child-> permalink:$ child-> link_url' –