Announcement

Collapse
No announcement yet.

Recursive Php function for items menu list

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Recursive Php function for items menu list

    This is a powerful recursive function to list items that are belong to a menu that is stored in a mysql database.
    The menu is this that is possible to create with unlimited sections and subsections of the w3images plugin images gallery for wordpress.
    Sections (items) and subsections are stored in the wp_w3images_sections table.
    The function isn't finished yet, but correctly return any list of existent sections and subsections for any passed (parent) section id.
    WordPress seem to have problems on outputting template with code tag that do not display correctly the php code, so this thread/post is linked at axew3.com where the explaining post about this function was published today, but where the code function has been removed caused by the issues reported.
    This is a first RAW version that need to be improved but it return correctly any subsections list.

    PHP Code:

    // axew3 - 19 Jul 2010
    // last update: 20 Jul 2010

    function w3_SectionsSubNames($sectionid)
    {
       global 
    $wpdb$w3images_sections_table;

         
    $getsubsections $wpdb->get_results("SELECT sectionid, parentid, name FROM $w3images_sections_table WHERE parentid = '$sectionid' ORDER BY sectionid ASC");
       
        foreach(
    $getsubsections as $subsection)
        {
               echo 
    $subsection->name "<br /> ";
                
    $subcont w3_SectionsSubNames($subsection->sectionid);
             
            }

       return;

    This function is called inside another foreach construct that pass to the function a parent section to grab subsections. As explained in the post at axew3.com will be very interesting if someone can suggest another maybe best way to build the menu items list. Another mysql query? A different approach? Some redundant instruction that can be reduced?

    LOOK below for the final improved version of this function

  • #2
    now, the function should be improved in the way to return an hyphen that need to be added once more any time a subsection is funded in the iteration.
    the menu should so return an output like (example):
    Code:
    home
    - first subsection of home
    -- subsection of first subsection of home - another subsection of first subsection of home
    --- .... ... ....
    ---- and so on

    Comment


    • #3
      the power of recursive functions: menu items functions, the step 2

      The function w3_SectionsSubNames($arg1 ) has been coded, it is a little bit more complex of course than the simple started above code.
      With addition of this new function that will be added here in reply with the next w3images 1.0.6 version release in few days more, w3images gallery will display all subsections links that belongs to a parent section as above described, so the w3images menu will be result much better than now.
      Can't wait to improve the new w3images 1.0.6 images gallery plugin for WordPress and release it publishing together the function explanation and code, sharing and searching maybe for some help and improvement.
      As soon i can i will go to finish with this step, so stay tuned please if interested on it!

      Comment


      • #4
        The menu items php function code: w3_SectionsSubNames($arg)

        This is the advanced version, so improved, of the above function. Now the function list for each line all sections that are belongs to the same parent.

        w3images - Recursive php menu items function:


        PHP Code:
        // axew3 - 19 Jul 2010
        // last update: 07 Aug 2010
        // info: this code is part of the w3images the gallery images plugin for WordPress
        // the function is called inside a foreach construct
        // that pass to the function related sections as argument

        function w3_SectionsSubNames($sectionid)
        {
           global 
        $wpdb$w3images_sections_table$PHP_SELF;

           
        $purl0 $PHP_SELF "/wp-w3images.php?w3_sectionid=";

             
        $getsubsections $wpdb->get_results("SELECT sectionid, parentid, name FROM $w3images_sections_table WHERE parentid = '$sectionid' AND activated = 1 ORDER BY sectionid ASC");
                 
                 
        $b sizeof($getsubsections);
                 
        $c 0;
                 
                 foreach( 
        $getsubsections as $getsubin ){

                  
        $c++;
             
                   if( 
        $getsubin->sectionid == ) {  continue; }

                      echo 
        "<small><a href=\"".$purl0.$getsubin->sectionid."\" title=\"".$getsubin->name."\">".$getsubin->name."</a></small> ";
                     
                        if( 
        $c == $b ){ echo "<br />"; }
                        
                         
        $getsubde $wpdb->get_results("SELECT sectionid, parentid, name FROM $w3images_sections_table WHERE parentid = '$getsubin->sectionid' AND activated = 1 ORDER BY sectionid ASC");
                     
                         
        $d sizeof($getsubde);
                           
        $e 0;  
                        
                           foreach( 
        $getsubde as $getsubdew ){
                              
                                
        $e++;
                 
                              
                            echo  
        "<small><a href=\"".$purl0.$getsubdew->sectionid."\" title=\"".$getsubdew->name."\">".$getsubdew->name."</a></small> ";
                             
                              if( 
        $d == $e ){ echo "<br />"; }
                         
                            }    
                    
                                
        w3_SectionsSubNames($getsubdew->sectionid);
                     }
             
              return;


        ismissed:
        as above: the function should be improved in the way to return an hyphen that need to be added once more
        for any subsections lines if there are. Any help editing the above code to return this improved result for the function will be great!
        ismissed:


        This is the definitive function released as code into the w3images-functions.php file

        Comment


        • #5
          The function should be improved in the way to return an hyphen that need to be added once more any time a subsection is funded in the iteration.??









          _________________________ _____________
          GuL

          Comment


          • #6
            i mean this, for the above function:
            category 1 - category 2
            sub1 - sub 2 - sub 3
            it can be achieved with:
            PHP Code:
            if( $c == $b ){ echo "<br />"; }
                             else { echo 
            " - "; } 
            but the function need to be rewrite to be more efficient: i look to this code now, after long time, and it seem to me to be crazy: but ok, it work!

            Comment

            Previously entered content was automatically saved. Restore or Discard.
            Auto-Saved
            x
            Insert: Thumbnail Small Medium Large Fullsize Remove  
            x
            x
            Working...
            X