doutu9810 2011-12-06 20:19 采纳率: 0%
浏览 8
已采纳

仅显示数组中的最后一项

I'm trying to figure out why its only showing the last child_links in the roster, events, and social objects. I've included the site that has the print_r of the array function. Any help would be appreciated.

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;

}
  • 写回答

2条回答 默认 最新

  • duanhongxian6982 2011-12-06 20:33
    关注

    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.

    So, 1st external loop, ex. $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'

       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.

    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(); 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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?