2
\$\begingroup\$

I'm trying to build a superfish dropdown menu from a sql query. Working example is can be seen from here check the "emlak bul" category: http://www.bedavaemlaksitesi.com/mersinemlakrehberi210/sayfalar/HABERLER

 function menu_emlak_kategori() {
        $this->db->distinct();
        $this->db->select('
        kgsim_ilceler.isim as ilce,
        kgsim_mahalleler.isim as mahalle,
        durum,
        kategori,
        tip,
        ozellik');
        $this -> db -> join('kgsim_ilceler', 'kgsim_ilceler.id = ilan_genel.ilce', 'left');
        $this -> db -> join('kgsim_mahalleler', 'kgsim_mahalleler.id = ilan_genel.mahalle', 'left');    
        $this->db->from('ilan_genel');
        $this->db->order_by('ilce,mahalle,durum,kategori,tip,ozellik');
        $q = $this -> db -> get();
        if ($q -> num_rows() > 0) {
            foreach ($q->result_array() as $row) {
                $data[] = $row;
            }
            return $data;
        }       
        return $q;  
    }

    function menu_emlak_kategori_duzenle($dizi) {
        $array = $this->menu_emlak_kategori();
        //$array = menu_emlak_kategori();
        foreach ($array as $key => $option) {
            // ilce start
            $ilce = $option['ilce'];
            $array_new['ilce'][$key] = $ilce;
            $array_new['ilce_unique'] = array_unique($array_new['ilce']);
            // ilce finish

            // mahalle start
            $mahalle = $option['mahalle'];
            $ilce_mahalle = $array_new['ilce'][$key];
            $ilce_mahalle .= "_mahalle";
            $ilce_mahalle_unique = $ilce_mahalle."_unique";
            $array_new[$ilce_mahalle][$key] = $mahalle;
            $array_new[$ilce_mahalle_unique] = array_unique($array_new[$ilce_mahalle]);
            // mahalle finish

            // durum start
            $durum = $option['durum'];
            $ilce_mahalle_durum = $ilce."_".$mahalle."_durum";
            $ilce_mahalle_durum_unique = $ilce_mahalle_durum."_unique";
            $array_new[$ilce_mahalle_durum][$key] = $durum;
            $array_new[$ilce_mahalle_durum_unique] = array_unique($array_new[$ilce_mahalle_durum]);
            // durum finish

            // kategori start
            $kategori = $option['kategori'];
            $ilce_mahalle_durum_kategori = $ilce."_".$mahalle."_".$durum."_kategori";
            $ilce_mahalle_durum_kategori_unique = $ilce_mahalle_durum_kategori."_unique";
            $array_new[$ilce_mahalle_durum_kategori][$key] = $kategori;
            $array_new[$ilce_mahalle_durum_kategori_unique] = array_unique($array_new[$ilce_mahalle_durum_kategori]);
            // kategori finish

            // tip start
            $tip = $option['tip'];
            $ilce_mahalle_durum_kategori_tip = $ilce."_".$mahalle."_".$durum."_".$kategori."_tip";
            $ilce_mahalle_durum_kategori_tip_unique = $ilce_mahalle_durum_kategori_tip."_unique";
            $array_new[$ilce_mahalle_durum_kategori_tip][$key] = $tip;
            $array_new[$ilce_mahalle_durum_kategori_tip_unique] = array_unique($array_new[$ilce_mahalle_durum_kategori_tip]);
            // tip finish

            // özellik start
            $ozellik = $option['ozellik'];
            $ilce_mahalle_durum_kategori_tip_ozellik = $ilce."_".$mahalle."_".$durum."_".$kategori."_".$tip."_ozellik";
            $ilce_mahalle_durum_kategori_tip_ozellik_unique = $ilce_mahalle_durum_kategori_tip_ozellik."_unique";
            $array_new[$ilce_mahalle_durum_kategori_tip_ozellik][$key] = $ozellik;
            $array_new[$ilce_mahalle_durum_kategori_tip_ozellik_unique] = array_unique($array_new[$ilce_mahalle_durum_kategori_tip_ozellik]);       
            // özellik finish


            // tum ilceler start

                $tumilceler = "tumilceler";

                // durum start
                $tumilceler_durum = $tumilceler."_durum";
                $tumilceler_durum_unique = $tumilceler."_durum_unique";
                $array_new['tumilceler_durum'][$key] = $durum;
                $array_new[$tumilceler_durum_unique] = array_unique($array_new['tumilceler_durum']);
                // durum finish

                // kategori start
                $tumilceler_durum_kategori = $tumilceler."_".$durum."_kategori";
                $tumilceler_durum_kategori_unique = $tumilceler_durum_kategori."_unique";
                $array_new[$tumilceler_durum_kategori][$key] = $kategori;
                $array_new[$tumilceler_durum_kategori_unique] = array_unique($array_new[$tumilceler_durum_kategori]);
                // kategori finish

                // tip start
                $tumilceler_durum_kategori_tip = $tumilceler."_".$durum."_".$kategori."_tip";
                $tumilceler_durum_kategori_tip_unique = $tumilceler_durum_kategori_tip."_unique";
                $array_new[$tumilceler_durum_kategori_tip][$key] = $tip;
                $array_new[$tumilceler_durum_kategori_tip_unique] = array_unique($array_new[$tumilceler_durum_kategori_tip]);
                // tip finish

                // özellik start
                $tumilceler_durum_kategori_tip_ozellik = $tumilceler."_".$durum."_".$kategori."_".$tip."_ozellik";
                $tumilceler_durum_kategori_tip_ozellik_unique = $tumilceler_durum_kategori_tip_ozellik."_unique";
                $array_new[$tumilceler_durum_kategori_tip_ozellik][$key] = $ozellik;
                $array_new[$tumilceler_durum_kategori_tip_ozellik_unique] = array_unique($array_new[$tumilceler_durum_kategori_tip_ozellik]);       
                // özellik finish
            // tum ilceler finish

            // tüm mahalleler start     
                $tummahalleler = "tummahalleler";
                // durum start
                $tummahalleler_ilce_durum = $tummahalleler."_".$ilce."_durum";
                $tummahalleler_ilce_durum_unique = $tummahalleler_ilce_durum."_unique";
                $array_new[$tummahalleler_ilce_durum][$key] = $durum;
                $array_new[$tummahalleler_ilce_durum_unique] = array_unique($array_new[$tummahalleler_ilce_durum]);
                // durum finish

                // kategori start
                $tummahalleler_ilce_durum_kategori = $tummahalleler."_".$ilce."_".$durum."_kategori";
                $tummahalleler_ilce_durum_kategori_unique = $tummahalleler_ilce_durum_kategori."_unique";
                $array_new[$tummahalleler_ilce_durum_kategori][$key] = $kategori;
                $array_new[$tummahalleler_ilce_durum_kategori_unique] = array_unique($array_new[$tummahalleler_ilce_durum_kategori]);
                // kategori finish

                // tip start
                $tummahalleler_ilce_durum_kategori_tip = $tummahalleler."_".$ilce."_".$durum."_".$kategori."_tip";
                $tummahalleler_ilce_durum_kategori_tip_unique = $tummahalleler_ilce_durum_kategori_tip."_unique";
                $array_new[$tummahalleler_ilce_durum_kategori_tip][$key] = $tip;
                $array_new[$tummahalleler_ilce_durum_kategori_tip_unique] = array_unique($array_new[$tummahalleler_ilce_durum_kategori_tip]);
                // tip finish

                // özellik start
                $tummahalleler_ilce_durum_kategori_tip_ozellik = $tummahalleler."_".$ilce."_".$durum."_".$kategori."_".$tip."_ozellik";
                $tummahalleler_ilce_durum_kategori_tip_ozellik_unique = $tummahalleler_ilce_durum_kategori_tip_ozellik."_unique";
                $array_new[$tummahalleler_ilce_durum_kategori_tip_ozellik][$key] = $ozellik;
                $array_new[$tummahalleler_ilce_durum_kategori_tip_ozellik_unique] = array_unique($array_new[$tummahalleler_ilce_durum_kategori_tip_ozellik]);       
                // özellik finish
            // tüm mahalleler finish
        }

        if(is_array($array_new['ilce_unique'])) {
            // $array_superfish = "<div class=\"navbar\">";
            $array_superfish = "<ul>";
            // tüm ilçeler start
            $array_superfish .= "<li><a><span class=\"menu-title\">";
            $array_superfish .= "Tüm İlçeler";  
            $array_superfish .= "</span></a>";  
            if(is_array($array_new[$tumilceler_durum_unique])) {
            $array_superfish .= "<ul>";
                foreach ($array_new[$tumilceler_durum_unique] as $durum) {
                $array_superfish .= "<li><a><span class=\"menu-title\">";
                $array_superfish .= $dizi[11][$durum];
                $array_superfish .= "</span></a>";
                $tumilceler_durum_kategori_unique = $tumilceler."_".$durum."_kategori_unique";
                if(is_array($array_new[$tumilceler_durum_unique])) {
                $array_superfish .= "<ul>";     
                    foreach ($array_new[$tumilceler_durum_kategori_unique] as $kategori) {
                    $array_superfish .= "<li><a><span class=\"menu-title\">";
                    $array_superfish .= $dizi[21][$kategori];
                    $array_superfish .= "</span></a>";
                    $tumilceler_durum_kategori_tip_unique = $tumilceler."_".$durum."_".$kategori."_tip_unique";
                    if(is_array($array_new[$tumilceler_durum_kategori_tip_unique])) {
                    $array_superfish .= "<ul>"; 
                        foreach ($array_new[$tumilceler_durum_kategori_tip_unique] as $tip) {
                        $kategori_kod = $kategori;  
                        if($kategori==3 or $kategori==NULL)
                        $kategori_kod = 2;  
                        $tip_kod = ''.$kategori_kod.'31';
                        $array_superfish .= "<li><a><span class=\"menu-title\">";
                        $array_superfish .= $dizi[$tip_kod][$tip];
                        $array_superfish .= "</span></a>";
                        $tumilceler_durum_kategori_tip_unique = $tumilceler."_".$durum."_".$kategori."_tip_unique";
                        if(is_array($array_new[$tumilceler_durum_kategori_tip_unique])) {
                        $array_superfish .= "<ul>"; 
                        $tumilceler_durum_kategori_tip_ozellik_unique = $tumilceler."_".$durum."_".$kategori."_".$tip."_ozellik_unique";
                            foreach ($array_new[$tumilceler_durum_kategori_tip_ozellik_unique] as $ozellik) {
                            $array_superfish .= "<li><a><span class=\"menu-title\">";
                            $genel_ozellik_kod = $dizi[101][$kategori]*10+2;
                            $genel_ozellik_metin = $dizi[$genel_ozellik_kod][$ozellik]; 
                            $array_superfish .= $dizi[$genel_ozellik_kod][$ozellik];
                            $array_superfish .= "</span></a>";
                            $array_superfish .= "</li>";
                            }
                        $array_superfish .= "</ul>";
                        }
                        $array_superfish .= "</li>";
                        }
                    $array_superfish .= "</ul>";
                    }
                    $array_superfish .= "</li>";
                    }
                $array_superfish .= "</ul>";
                }       
                $array_superfish .= "</li>";
                }
            $array_superfish .= "</ul>";
            }
            $array_superfish .= "</li>";
            // tüm ilçeler finish
        foreach ($array_new['ilce_unique'] as $ilce) {
            $array_superfish .= "<li><a><span class=\"menu-title\">";
            $array_superfish .= $ilce;  
            $array_superfish .= "</span></a>";
            $ilce_mahalle_unique = $ilce."_mahalle_unique";
                if(is_array($array_new[$ilce_mahalle_unique])) {
                $array_superfish .= "<ul>";
                    // tüm mahalleler start
            $array_superfish .= "<li><a><span class=\"menu-title\">";
            $array_superfish .= "Tüm Mahalleler";   
            $array_superfish .= "</span></a>";  
            $tummahalleler_ilce_durum_unique = $tummahalleler."_".$ilce."_durum_unique";
            if(is_array($array_new[$tummahalleler_ilce_durum_unique])) {
            $array_superfish .= "<ul>";
                foreach ($array_new[$tummahalleler_ilce_durum_unique] as $durum) {
                $array_superfish .= "<li><a><span class=\"menu-title\">";
                $array_superfish .= $dizi[11][$durum];
                $array_superfish .= "</span></a>";
                $tummahalleler_ilce_durum_kategori_unique = $tummahalleler."_".$ilce."_".$durum."_kategori_unique";
                if(is_array($array_new[$tummahalleler_ilce_durum_kategori_unique])) {
                $array_superfish .= "<ul>";     
                    foreach ($array_new[$tummahalleler_ilce_durum_kategori_unique] as $kategori) {
                    $array_superfish .= "<li><a><span class=\"menu-title\">";
                    $array_superfish .= $dizi[21][$kategori];
                    $array_superfish .= "</span></a>";
                    $tummahalleler_ilce_durum_kategori_tip_unique = $tummahalleler."_".$ilce."_".$durum."_".$kategori."_tip_unique";
                    if(is_array($array_new[$tummahalleler_ilce_durum_kategori_tip_unique])) {
                    $array_superfish .= "<ul>"; 
                        foreach ($array_new[$tummahalleler_ilce_durum_kategori_tip_unique] as $tip) {
                        $kategori_kod = $kategori;  
                        if($kategori==3 or $kategori==NULL)
                        $kategori_kod = 2;  
                        $tip_kod = ''.$kategori_kod.'31';
                        $array_superfish .= "<li><a><span class=\"menu-title\">";
                        $array_superfish .= $dizi[$tip_kod][$tip];
                        $array_superfish .= "</span></a>";
                        $tummahalleler_ilce_durum_kategori_tip_unique = $tummahalleler."_".$ilce."_".$durum."_".$kategori."_tip_unique";
                        if(is_array($array_new[$tummahalleler_ilce_durum_kategori_tip_unique])) {
                        $array_superfish .= "<ul>"; 
                        $tummahalleler_ilce_durum_kategori_tip_ozellik_unique = $tummahalleler."_".$ilce."_".$durum."_".$kategori."_".$tip."_ozellik_unique";
                            foreach ($array_new[$tummahalleler_ilce_durum_kategori_tip_ozellik_unique] as $ozellik) {
                            $array_superfish .= "<li><a><span class=\"menu-title\">";
                            $genel_ozellik_kod = $dizi[101][$kategori]*10+2;
                            $genel_ozellik_metin = $dizi[$genel_ozellik_kod][$ozellik]; 
                            $array_superfish .= $dizi[$genel_ozellik_kod][$ozellik];
                            $array_superfish .= "</span></a>";
                            $array_superfish .= "</li>";
                            }
                        $array_superfish .= "</ul>";
                        }
                        $array_superfish .= "</li>";
                        }
                    $array_superfish .= "</ul>";
                    }
                    $array_superfish .= "</li>";
                    }
                $array_superfish .= "</ul>";
                }       
                $array_superfish .= "</li>";
                }
            $array_superfish .= "</ul>";
            }
            $array_superfish .= "</li>";
                // tüm mahalleler finish
                    foreach ($array_new[$ilce_mahalle_unique] as $mahalle) {
                    if($mahalle!=null)  {
                    $array_superfish .= "<li><a><span class=\"menu-title\">";
                    $array_superfish .= $mahalle;
                    $array_superfish .= "</span></a>";
                    $ilce_mahalle_durum_unique = $ilce."_".$mahalle."_durum_unique";
                        if(is_array($array_new[$ilce_mahalle_durum_unique])) {
                        $array_superfish .= "<ul>";
                            foreach ($array_new[$ilce_mahalle_durum_unique] as $durum) {    
                            $array_superfish .= "<li><a><span class=\"menu-title\">";
                            $array_superfish .= $dizi[11][$durum];
                            $array_superfish .= "</span></a>";
                            $ilce_mahalle_durum_kategori_unique = $ilce."_".$mahalle."_".$durum."_kategori_unique";
                                if(is_array($array_new[$ilce_mahalle_durum_kategori_unique])) {
                                $array_superfish .= "<ul>";
                                    foreach ($array_new[$ilce_mahalle_durum_kategori_unique] as $kategori) {
                                    $array_superfish .= "<li><a><span class=\"menu-title\">";
                                    $array_superfish .= $dizi[21][$kategori];
                                    $array_superfish .= "</span></a>";
                                    $ilce_mahalle_durum_kategori_tip_unique = $ilce."_".$mahalle."_".$durum."_".$kategori."_tip_unique";
                                        if(is_array($array_new[$ilce_mahalle_durum_kategori_tip_unique])) {
                                        $array_superfish .= "<ul>";
                                            foreach ($array_new[$ilce_mahalle_durum_kategori_tip_unique] as $tip) {
                                            $kategori_kod = $kategori;  
                                            if($kategori==3 or $kategori==NULL)
                                            $kategori_kod = 2;  
                                            $tip_kod = ''.$kategori_kod.'31';
                                            $array_superfish .= "<li><a><span class=\"menu-title\">";
                                            $array_superfish .= $dizi[$tip_kod][$tip];
                                            $array_superfish .= "</span></a>";
                                            $ilce_mahalle_durum_kategori_tip_ozellik_unique = $ilce."_".$mahalle."_".$durum."_".$kategori."_".$tip."_ozellik_unique";
                                                if(is_array($array_new[$ilce_mahalle_durum_kategori_tip_ozellik_unique])) {
                                                $array_superfish .= "<ul>";
                                                    foreach ($array_new[$ilce_mahalle_durum_kategori_tip_ozellik_unique] as $ozellik) {
                                                        $array_superfish .= "<li><a><span class=\"menu-title\">";
                                                        $genel_ozellik_kod = $dizi[101][$kategori]*10+2;
                                                        $genel_ozellik_metin = $dizi[$genel_ozellik_kod][$ozellik]; 
                                                        $array_superfish .= $dizi[$genel_ozellik_kod][$ozellik];
                                                        $array_superfish .= "</span></a>";
                                                        $array_superfish .= "</li>";
                                                    }
                                                $array_superfish .= "</ul>";
                                                }

                                            $array_superfish .= "</li>";    
                                            }
                                        $array_superfish .= "</ul>";
                                        }
                                    $array_superfish .= "</li>";
                                    }
                                $array_superfish .= "</ul>";
                                }
                            $array_superfish .= "</li>";
                            }
                        $array_superfish .= "</ul>";
                        }
                    $array_superfish .= "</li>";
                    }
                    }
                $array_superfish .= "</ul>";
                }
            $array_superfish .= "</li>";
        }
            $array_superfish .= "</ul>";
        }   
         // $array_superfish .= "</div>";
            return $array_superfish;
    }

note: using codeigniter 2.10 functions are from model file.

\$\endgroup\$
3
  • \$\begingroup\$ bunun kesinlikle daha basarili yapilabilecegi kesin :) presentasyon kismini oncelikle view e almani tavsiye ederim \$\endgroup\$
    – Herr
    Commented Dec 5, 2011 at 14:15
  • \$\begingroup\$ isn't that cause much more complicated view file ? \$\endgroup\$
    – mirza
    Commented Dec 6, 2011 at 4:26
  • \$\begingroup\$ Actually no, this should definitely be in the presentation file. You should generate the array and deliver it as a menu in the view file. Why not do a separate view file called "menu.php" and then include it where you need it? I would definitely do it like this \$\endgroup\$
    – Herr
    Commented Dec 6, 2011 at 9:30

1 Answer 1

2
\$\begingroup\$

Yes, it can definitely be improved. Here are some things to think about:

Data

The data should match your problem. Make sure it is in a format that represents your menu. A menu can be handled well with recursive functions. These are an easy way to visit each element and build the appropriate content you want to display.

Nesting

Excessive nesting is bad - it makes it very hard to understand where a problem is. Consider writing functions to perform common actions. Each loop or if statement is another level of complexity. If you get past 3 levels deep you should seriously consider rewriting your code. You have many more than this. This would be fixed by focussing on your data and writing this in a recursive way.

Misc.

If you are going to append to a string you can do it in one step rather than over 3 lines.

$array_superfish .= "<li><a><span class=\"menu-title\">" . $dizi[11][$durum] .
   "</span></a>";

Note: $array_superfish is a bad name as I would be expecting it to be an array rather than a string.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.