简体   繁体   English

仅显示数组中的最后一项

[英]Only showing last item in array

I'm trying to figure out why its only showing the last child_links in the roster, events, and social objects. 我试图弄清楚为什么它只显示花名册,事件和社交对象中的最后一个child_links。 I've included the site that has the print_r of the array function. 我已经包括了具有数组函数的print_r的站点。 Any help would be appreciated. 任何帮助,将不胜感激。

http://kansasoutlawwrestling.com/ http://kansasoutlawwrestling.com/

function getSubMenuPages()
{
    $this->db->select('id');
    $this->db->where('short_name', 'mainnav'); 
    $query = $this->db->get('site_menu_structures'); 
    $menu_id = $query->row()->id; 

    $this->db->select('id, short_name, is_category');
    $this->db->where('menu_structure_id', $menu_id); 
    $query = $this->db->get('site_menu_structures_links');

    if ($query->num_rows() > 0) 
    {
        $linksArray = $query->result();
        echo "<pre>";
        print_r( $linksArray );
        echo "</pre>";
        foreach ($linksArray as $key => $link)
        {
            if ($link->is_category == 'Yes')
            {
                $this->db->select('link_name, site_content_pages_id, link_url');
                $this->db->where('site_menu_structures_links_id', $link->id); 
                $query = $this->db->get('site_menu_structures_links_children');
                if ($query->num_rows() > 0) 
                {
                    foreach ($query->result() as $row)
                    {
                        $site_content_page_id = $row->site_content_pages_id;
                        $linksArray[$key]->child_links = array();
                        if ($site_content_page_id != 0)
                        {
                            $this->db->select('content_page_name, permalink');
                            $this->db->where('id', $site_content_page_id); 
                            $query = $this->db->get('site_content_pages');
                            if ($query->num_rows() > 0)
                            {
                                $row = $query->row(); 
                                $linksArray[$key]->child_links = array(
                                                                'link_name'  => $row->content_page_name,
                                                                'link_url' => $row->permalink
                                                             );
                            }
                        }
                        else
                        {
                            $linksArray[$key]->child_links = array(
                                                                'link_name'  => $row->link_name,
                                                                'link_url' => $row->link_url
                                                             );
                        }
                    }
                }
            }
        }
    }

    return $linksArray;

}

When you loop here: 当您在此处循环时:

foreach ($query->result() as $row)
                    {
                        $site_content_page_id = $row->site_content_pages_id;
                        $linksArray[$key]->child_links = array();
                        if ($site_content_page_id != 0)
                        {

you're inside another loop, so at each passage of this loop here the $key remains the same. 您在另一个循环中,因此在此循环的每个通道中,$ key都保持不变。

So, 1st external loop , ex. 因此, 第一个外部循环 ,例如。 $key = 'key1'; $ key ='key1'; I'll use some pseudo code to give the idea: 我将使用一些伪代码给出想法:

foreach results as row:
  (loop nr 1):
     linksarray['key1'] = empty array.
     linksarray['key1'] = value;
  (loop nr 2):
     linksarray['key1'] = empty array
     linksarray['key1'] = value2
endforeach;

2nd external loop , $Key = 'key2' 第二次外部循环 ,$ Key ='key2'

   foreach results as row:
      (loop nr 1):
         linksarray['key2'] = empty array.
         linksarray['key2'] = value;
      (loop nr 2):
         linksarray['key2'] = empty array
         linksarray['key2'] = value2
    endforeach;

I might have mis-looked something in all those loops, it's evening here and I'm a bit tired :) 我可能在所有这些循环中都看错了东西,今天晚上到了,我有点累:)


Oh, and a piece of advice: your code gets easily a bit difficult to read; 哦,还有一条建议:您的代码容易变得有点难以阅读; a small improvement would be to omit the table name (as it's not necessary), especially when it's so long; 一个小改进是省略表名(因为它没有必要),尤其是当它太长时; moreover, in AR you can omit the "from" clause and also chain method. 此外,在AR中,您可以省略“ from”子句以及链方法。

So, for ex., this: 因此,例如:

$this->db->select('site_menu_structures_links_children.link_name, site_menu_structures_links_children.site_content_pages_id, site_menu_structures_links_children.link_url');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();

Could be easily rewritten as: 可以很容易地重写为:

$query = $this->db->select('link_name,site_content_pages_id,link_url')
                  ->where('site_menu_structures_links_id',$link->id)
                  ->get('site_menu_structures_links_children');

Doing this might help you in identifying out the general code flow, spotting out the various runs 这样做可以帮助您识别常规代码流,找出各种运行

UPDATE 更新

Try this 尝试这个

function getSubMenuPages()
{
    $this->db->select('id');
    $this->db->where('short_name', 'mainnav'); 
    $query = $this->db->get('site_menu_structures'); 
    $menu_id = $query->row()->id; 

    $this->db->select('id, short_name, is_category');
    $this->db->where('menu_structure_id', $menu_id); 
    $query2 = $this->db->get('site_menu_structures_links');

    if ($query2->num_rows() > 0) 
    {
        $linksArray = $query2->result();
        echo "<pre>";
        print_r( $linksArray );
        echo "</pre>";
        foreach ($linksArray as $key => $link)
        {
            if ($link->is_category == 'Yes')
            {
                $this->db->select('link_name, site_content_pages_id, link_url');
                $this->db->where('site_menu_structures_links_id', $link->id); 
                $query3 = $this->db->get('site_menu_structures_links_children');
                if ($query3->num_rows() > 0) 
                {
                    $linksArray[$key]->child_links = array();
                    foreach ($query3->result() as $row)
                    {
                        $site_content_page_id = $row->site_content_pages_id;
                        //$linksArray[$key]->child_links = array();
                        if ($site_content_page_id != 0)
                        {
                            $this->db->select('content_page_name, permalink');
                            $this->db->where('id', $site_content_page_id); 
                            $query4 = $this->db->get('site_content_pages');
                            if ($query4->num_rows() > 0)
                            {
                                $row = $query4->row(); 
                                $linksArray[$key]->child_links[]['link_name'] = $row->content_page_name;
                                $linksArray[$key]->child_links[]['link_url'] = $row->permalink;
                            }
                        }
                        else
                        {
                            $linksArray[$key]->child_links[]['link_name'] = $row->link_name;
                            $linksArray[$key]->child_links[]['link_url'] = $row->link_url;
                        }
                    }
                }
            }
        }
    }

    return $linksArray;

}

basically, I moved the property assigment $linksArray[$key]->child_links = array(); 基本上,我移动了属性$linksArray[$key]->child_links = array(); outside the loop. 在循环之外。 Being followed by another loop, which can possibly have more values, I've created an index for each loop: 紧接着另一个循环(可能具有更多值),我为每个循环创建了一个索引:

$linksArray[$key]->child_links[]['link_name'] = $row->content_page_name;
$linksArray[$key]->child_links[]['link_url'] = $row->permalink;

Try replacing this block of code: 尝试替换以下代码块:

$this->db->select('link_name, site_content_pages_id, link_url');
$this->db->where('site_menu_structures_links_id', $link->id); 
$query = $this->db->get('site_menu_structures_links_children');
if ($query->num_rows() > 0) 
{
    $linksArray[$key]->child_links = array();

    foreach ($query->result() as $row)
    {
        $site_content_page_id = $row->site_content_pages_id;

        if ($site_content_page_id != 0)
        {
            $this->db->select('content_page_name, permalink');
            $this->db->where('id', $site_content_page_id); 
            $query2 = $this->db->get('site_content_pages');
            if ($query2->num_rows() > 0)
            {
                $row2 = $query2->row(); 
                array_push($linksArray[$key]->child_links, array(
                                                'link_name'  => $row2->content_page_name,
                                                'link_url' => $row2->permalink
                                             ));
            }
        }
        else
        {
            array_push($linksArray[$key]->child_links, array(
                                                'link_name'  => $row->link_name,
                                                'link_url' => $row->link_url
                                             ));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM