Question

Je les fonctions de modèle suivants avec PHP. Je sais que je me répète.

Y at-il de toute façon je peux simplifier ce 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; 
 }
Était-ce utile?

La solution

Je peux voir une simplification, vous pouvez essayer, en utilisant un passage par référence au facteur des choses en fonction:

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;
}

Il y a aussi la possibilité d'utiliser passage par référence et fonctions variables pour factoriser la partie dans le milieu. Peut réduire le double emploi, mais peut ou non être considéré comme « la simplification ».

EDIT Voici ce que je veux dire. Ce code est non testé.

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;  
}

Autres conseils

Pourquoi pas passer des paramètres dans votre fonction et placez-les dans le « où » et « get » méthodes?

Je ne sais pas ce que vos classes db et requêtes ressemblent, mais je commence à les améliorer en premier lieu. Ajouter « tableau fetch », et les fonctions « hachage chercher » à la classe de requête:

 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;
         }

Faire 'db-> où () lui-même retour

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

Une fois que vous avez cela, les fonctions de votre client devient 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();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top