views:

122

answers:

3

I have the following model functions with PHP. I know I am repeating myself.

Is there anyway I can simplify this code?

function getTopMenus(){
     $data[0] = 'root';
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();  
    return $data; 
 }  

function getheadMenus(){
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data; 
 }  
function getrootMenus(){
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
          $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();  
    return $data; 
 }
A: 

Why not pass parameters into your function and place them in the 'where' and 'get' methods?

Ben Fransen
You'll notice where/get don't change. He's only changing how the data gets saved into the array/hash. That makes it a lot harder to factor out.
Adam Luchjenbroers
+3  A: 

I can see one simplification you might try, using pass-by-reference to factor things out into a function:

function prepareMenu(&$data) {
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
          $data[$row['id']] = $row['name'];
       }
    }
    $Q->free_result();
}

function getTopMenus() {
    $data[0] = 'root';
    prepareMenus($data);
    return $data;
}

function getRootMenus() {
    prepareMenus($data);
    return $data;
}

There's also the possibility of using pass-by-reference and variable functions to factor out the part in the middle. May reduce duplication, but may or may not be considered 'simplifying'.

EDIT Here's what I mean. This code is untested.

function getMenus(&$data, $appendFunc) {
     $this->db->where('parentid',0);
     $Q = $this->db->get('menus');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $appendFunc(&$data, $row);
       }
    }
    $Q->free_result(); 
}

function appendTopMenu(&$data, $row) {
    $data[$row['id']] = $row['name'];
}

function appendHeadMenu(&$data, $row) {
    $data[] = $row;
}

function getTopMenus() {
    $data[0] = 'root';
    getMenus($data, "appendTopMenu");
    return $data; 
}  

function getheadMenus() {
    getMenus($data, "appendHeadMenu");
    return $data; 
 } 

function getrootMenus() {
    getMenus($data, "appendTopMenu");
    return $data;  
}
Adam Luchjenbroers
A: 

I don't know what your db and query classes look like, but i'd start improving these in the first place. Add "array fetch" and "hash fetch" functions to the query class:

 class Query ...

        function as_array() {
           $data = array();
           if($this->num_rows() > 0)
             foreach ($this->result_array() as $row)
                 $data[] = $row;
           $this->free_result();  
           return $data;
         }
        function as_hash($key = 'id') {
           $data = array();
           if($this->num_rows() > 0)
             foreach ($this->result_array() as $row)
                 $data[$row[$key]] = $row;
           $this->free_result();  
           return $data;
         }

Make 'db->where()' return itself

     class DB
          function where(...) {
              stuff
              return $this;

Once you have this, your client functions become trivial:

function getTopMenus() {
     $data = $this->db->where('parentid',0)->get('menus')->as_hash();
     $data[0] = 'root';
     return $data;
 } 

function getheadMenus() {
     return $this->db->where('parentid',0)->get('menus')->as_array();
 }  

function getrootMenus() {
     return $this->db->where('parentid',0)->get('menus')->as_hash();
}
stereofrog