codeigniter - How can I simplify this PHP? -


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

Can anybody simplify this code anyway?

  function getTopMenus () {$ data [0] = 'root'; $ This- & gt; Db- & gt; Where ('parentid', 0); $ Q = $ it- & gt; DB- & gt; Get ('menu'); ($ Q-> num_rows ()> 0) {foreach ($ Q-> result_array ($ line as) {$ data [$ row ['id']] = $ row ['name '];}} $ Q- & gt; Free_Ralt (); $ data return;} function getheadMenus () {$ this-> db-> where (' parentid ', 0); $ Q = $ -> DB-> Receive ('menu'); if ($ Q-> num_rows ()> 0) {foreach ($ Q-> result_array (as the $ line) {$ Data [] = $ line;}} $ Q- & gt; Free_Ralt (); $ data return;} getrootMenus function () {$ this-> db-> where ('parentid', 0) ; $ Cue = $ it- & gt; db- & gt; get ('menu'); ($ Q-> num_rows ()> 0) {foreach ($ Q-> Results_re ( $ Line [$ data [$ row ['id']] = $ row ['name'];}} $ Q- & gt; Free_Ralt (); $ data return;}  

I can see a simplification that you can see using a pass-by-reference factor things a function :

  Create a function menu (and $ data) {$ this- & gt; Db- & gt; Where ('guardian', 0); $ Q = $ it- & gt; DB- & gt; Get ('menu'); ($ Q-> num_rows ()> 0) {foreach ($ Q-> result_array ($ line as) {$ data [$ row ['id']] = $ row ['name '];}} $ Q- & gt; Free_Ralt ();} getTopMenus () {$ data [0] =' root '; readyMenus ($ data); $ data return;} getRootMenus function} {prepareMenus ($ Data return; $ data return;}  

There is also the possibility of using and there is a possibility to make the factor in the middle. Can reduce repetition, but can be considered as 'simplification' Is it or not?

Edit What do I mean here function getMenus (& $ data, $ Appendfunc) where $ ($ '>-> $' '> $ Q = $ THIS- & gt; DB- & gt; receive (' menu '); if ($ Q- & Gt; num_rows ()> 0) {foreach ($ Q-> result_are ($ as line) {$ appendFunc (& amp; $ data, $ line);}} $ Q- & gt; Function_address (menu; $ line) {$ data [$ row ['id']] = $ line ['name'];} function appended menu (& amp; $ data, $ line) {} $ Data [] = $ line; } GetTopMenus function () {$ data [0] = 'root'; Millman's ($ data, "appendop menu"); $ Return data; } Function getheadMenus () {getMenus ($ data, "appendHeadMenu"); $ Return data; } GetrootMenus function (getMenus ($ data, "appendTopMenu"); $ Return data; }


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -