内联三元运算符不起作用
由于某种原因,我的三元运算符分配不适用于数组的第二部分。有人看到我做错了什么吗?它应该只是查看永久链接字段是否有值,如果没有,则将 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
)
)
For some reason my ternary operator assignment isn't work for the second part of my array. Anyone see what I'm doing wrong? Its supposed to just see if there is a value for the permalink field and if there isn't then insert the link_url
into the array.
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;
}
EDIT:
Also not that there should be 3 items inside the Social Array and its not saying there is. I'm wondering if it has something to do with that join. Here's my output:
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
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有:
我假设您的意思是
'link_url'
而不是'linke_url'
,所以看起来您正在尝试执行以下操作:If
$child- >permalink
为空,将'link_url'
设置为$child->link_url
,但是,如果$child->permalink
代码> 不为空,将'link_url'
设置为$child->permalink
。我建议使用 的简写版本三元运算符 ?: 其形式为:
$a = $b ?: $c
与$b
计算结果为true 相同
,即如果$b
可以为任何值,$a = $b
,否则$a = $c
。对于您来说:如果
$child->permalink
有一个值,则将使用该值,否则,将使用$child->link_url
的值。祝你好运!You have:
I'll assume you meant
'link_url'
and not'linke_url'
, so it looks like you're trying to do:If
$child->permalink
is empty, set'link_url'
to$child->link_url
, but, if$child->permalink
isn't empty, set'link_url'
to$child->permalink
.I would recommend using the shorthand version of the ternary operator ?: which is the form:
$a = $b ?: $c
which is the same as if$b
evaluates totrue
, i.e. if$b
has any value,$a = $b
, otherwise$a = $c
. For you:If
$child->permalink
has a value, that will be used, otherwise, the value of$child->link_url
will be used. Good luck!