Automatically sort all Arrays in controller when getting from database PHP -
so have controller passes associative array called $pagedata view. inside array 3 more associative arrays, , view renders 3 select elements array data options. want sort 3 arrays don't want write sort 3 times here or add order_by query methods, because there dozens of similar pages , don't want write hundreds of sort method calls. told solve in constructor. wondering if there's oop solution lets me automatically sort child arrays inside $pagedata.
class sku extends ci_controller { protected $pagedata = array(); public function __construct() { parent::__construct(); $this->load->model('mc'); } public function inventory() { $this->pagedata['class_ids'] = $this->mc->get_class_ids(); $this->pagedata['retail_readys'] = $this->mc->get_all_retail_ready(); $this->pagedata['statuses'] = $this->mc->get_all_status(); } } edit: i'm exploring using arrayobject or wrapping $pagedata in object , watch changes.
ok painfull codeigniter yes kind of solution
public function __construct() { parent::__construct(); $this->load->model('mc'); $class_ids = $this->mc->get_class_ids(); $class_ids = $this->sortascending($class_ids, $key); $this->pagedata['class_ids'] = $class_ids; $retail_readys = $this->mc->get_all_retail_ready(); $class_ids = $this->sortascending($class_ids, $key); $this->pagedata['class_ids'] = $class_ids; $statuses = $this->mc->get_all_status(); $statuses = $this->sortascending($class_ids, $key); $this->pagedata['statuses'] = $statuses; } function sortascending($accounts, $key) { $ascending = function($accounta, $accountb) use ($key) { if ($accounta[$key] == $accountb[$key]) { return 0; } return ($accounta[$key] < $accountb[$key]) ? -1 : 1; }; usort($accounts, $ascending); return $accounts; } public function inventory() { // values //$this->pagedata['class_ids'] = $this->mc->get_class_ids(); //$this->pagedata['retail_readys'] = $this->mc->get_all_retail_ready(); //$this->pagedata['statuses'] = $this->mc->get_all_status(); $this->load->view('index',$this->pagedata); } public function another_function() { // values //$this->pagedata['class_ids'] = $this->mc->get_class_ids(); //$this->pagedata['retail_readys'] = $this->mc->get_all_retail_ready(); //$this->pagedata['statuses'] = $this->mc->get_all_status(); $this->load->view('another page',$this->pagedata); }
Comments
Post a Comment