2016-04-04 2 views
1

Я пытаюсь сортировать массив по заказам, но когда я использую сортировку или сортировку массива и т. Д., Он дает мне неопределенную ошибку смещения. Без сортировки он отлично работает!php sorting array дает: Неопределенное смещение: 6

Сценарий:

class Dynamic_menu { 

private $ci;    // for CodeIgniter Super Global Reference. 
private $id_menu  = 'class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"'; 
private $class_menu  = 'class="nav navbar-nav"'; 
private $class_parent = 'class="parent"'; 
private $class_last  = 'class="last"'; 

// -------------------------------------------------------------------- 

/** 
* PHP5  Constructor 
* 
*/ 
function __construct() 
{ 
    $this->ci =& get_instance(); // get a reference to CodeIgniter. 
} 

// -------------------------------------------------------------------- 

/** 
* build_menu($table, $type) 
* 
* Description: 
* 
* builds the Dynaminc dropdown menu 
* $table allows for passing in a MySQL table name for different menu tables. 
* $type is for the type of menu to display ie; topmenu, mainmenu, sidebar menu 
* or a footer menu. 
* 
* @param string the MySQL database table name. 
* @param string the type of menu to display. 
* @return string $html_out using CodeIgniter achor tags. 
*/ 
function build_menu($menu_id) 
{ 
    $menu = array(); 
    // use active record database to get the menu. 

    //$this->ci->db->order_by('orders', 'asc'); 
    //$query = $this->ci->db->get('menus_data'); 
    //$query = $this->ci->db->get($table); 
    $this->ci->db->select("*"); 
    $this->ci->db->from("menus_data"); 
    // $this->ci->db->where("menu_id", $menu_id); 
    $query = $this->ci->db->get(); 

    if ($query->num_rows() > 0) 
    { 
     // `id`, `title`, `link_type`, `page_id`, `module_name`, `url`, `uri`, `dyn_group_id`, `position`, `target`, `parent_id`, `show_menu` 

     foreach ($query->result() as $row) 
     { 
      $menu[$row->id]['id']   = $row->id; 
      $menu[$row->id]['title']  = $row->menu_title; 
      //$menu[$row->id]['link']   = $row->link_type; 
      $menu[$row->id]['page']   = $row->page_id; 
      //$menu[$row->id]['module']  = $row->module_name; 
      $menu[$row->id]['url']   = $row->menu_url; 
      //$menu[$row->id]['uri']   = $row->uri; 
      //$menu[$row->id]['dyn_group'] = $row->dyn_group_id; 
      $menu[$row->id]['orders']  = $row->orders; 
      //$menu[$row->id]['target']  = $row->target; 
      $menu[$row->id]['parent']  = $row->parent_id; 
      $menu[$row->id]['is_parent'] = $row->is_parent; 
      $menu[$row->id]['show']   = $row->visable; 
     } 
    } 
    $query->free_result(); // The $query result object will no longer be available 



    // ---------------------------------------------------------------------- 
    // now we will build the dynamic menus. 
    $html_out = "\t".'<div '.$this->id_menu.'>'."\n"; 
    $html_out .= "\t\t".'<ul '.$this->class_menu.'>'."\n"; 




    // asort($menu); 
    usort($menu, function($a, $b) { 
     return $a['orders'] - $b['orders']; 
    }); 


    // loop through the $menu array() and build the parent menus. 
    for ($i = 0; $i < count($menu); $i++) 
    { 
     if (is_array($menu[$i])) // must be by construction but let's keep the errors home 
     { 
      if ($menu[$i]['show'] && $menu[$i]['parent'] == 0) // are we allowed to see this menu? 
      { 
       if ($menu[$i]['is_parent'] == TRUE) 
       { 
        // CodeIgniter's anchor(uri segments, text, attributes) tag. 
        $html_out .= "\t\t\t".'<li class="dropdown">'.anchor($menu[$i]['url'], ''.$menu[$i]['title'].'<span class="caret"></span>', 'class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"'); 
       } 
       else 
       { 
        $html_out .= "\t\t\t\t".'<li>'.anchor($menu[$i]['url'], '<span>'.$menu[$i]['title'].'</span>'); 
       } 

       // loop through and build all the child submenus. 
       $html_out .= $this->get_childs($menu, $i); 

       $html_out .= '</li>'."\n"; 
      } 
     } 
     else 
     { 
      exit (sprintf('menu nr %s must be an array', $i)); 
     } 
    } 

    $html_out .= "\t\t".'</ul>' . "\n"; 
    $html_out .= "\t".'</div>' . "\n"; 

    return $html_out; 
} 
/** 
* get_childs($menu, $parent_id) - SEE Above Method. 
* 
* Description: 
* 
* Builds all child submenus using a recurse method call. 
* 
* @param mixed $menu array() 
* @param string $parent_id id of parent calling this method. 
* @return mixed $html_out if has subcats else FALSE 
*/ 
function get_childs($menu, $parent_id) 
{ 
    $has_subcats = FALSE; 

    $html_out = ''; 
    $html_out .= "\t\t\t\t\t".'<ul class="dropdown-menu">'."\n"; 

    for ($i = 0; $i < count($menu); $i++) 
    { 
     if ($menu[$i]['show'] && $menu[$i]['parent'] == $parent_id) // are we allowed to see this menu? 
     { 
      $has_subcats = TRUE; 

      if ($menu[$i]['is_parent'] == TRUE) 
      { 
       $html_out .= "\t\t\t\t\t\t".'<li>'.anchor($menu[$i]['url'], '<span>'.$menu[$i]['title'].'</span>'); 
      } 
      else 
      { 
       $html_out .= "\t\t\t\t\t\t".'<li>'.anchor($menu[$i]['url'], '<span>'.$menu[$i]['title'].'</span>'); 
      } 

      // Recurse call to get more child submenus. 
      $html_out .= $this->get_childs($menu, $i); 

      $html_out .= '</li>' . "\n"; 
     } 
    } 
    $html_out .= "\t\t\t\t\t".'</ul>' . "\n"; 

    return ($has_subcats) ? $html_out : FALSE; 
} 

}

печати Результат:

Array ([0] => Array ([id] => 5 [title] => testurl [page] => 0 [url] => http://www.google.nl [orders] => 0 [parent] => 0 [is_parent] => 1 [show] => 1) [1] => Array ([id] => 3 [title] => Home2 [page] => 1 [url] => [orders] => 0 [parent] => 0 [is_parent] => 0 [show] => 0) [2] => Array ([id] => 2 [title] => Jantje [page] => 3 [url] => /home/jantje [orders] => 1 [parent] => 5 [is_parent] => 0 [show] => 1) [3] => Array ([id] => 4 [title] => Jantje2 [page] => 3 [url] => [orders] => 1 [parent] => 3 [is_parent] => 0 [show] => 0) [4] => Array ([id] => 1 [title] => Home [page] => 1 [url] =>/[orders] => 2 [parent] => 0 [is_parent] => 1 [show] => 1) [5] => Array ([id] => 6 [title] => Demo [page] => 2 [url] => /demo [orders] => 3 [parent] => 1 [is_parent] => 0 [show] => 1)) 

Может кто-нибудь, пожалуйста, помогите мне исправить это?

Благодаря

+0

Это следующая ошибка после некоторые изменения. Увеличили все в apache и php allready до максимума и все еще эту ошибку. Неустранимая ошибка: разрешенный размер памяти 536870912 байт исчерпан (пытался выделить 523800 байт) в /var/www/html/system/core/Config.php в строке 244 –

+0

Решение найдено! Где-то была пустая переменная. –

ответ

0

Вы переборе от 1 до 6, когда вы должны быть итерация от 0 до 5. Изменения:

for ($i = 1; $i <= count($menu); $i++) 

к:

for ($i = 0; $i < count($menu); $i++) 
+0

По-прежнему такая же ошибка. –

+0

@MitchDaniels Вы заменили '<=' на '<'? – Paulpro

+0

да, я тоже заменил это. –

Смежные вопросы