1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Easing up code

Discussion in 'General PHP and MySQL Discussions' started by katsulynx, Jul 24, 2015.

  1. katsulynx

    katsulynx Well-Known Member

    I've created a function that makes me ill by just looking at it, but I honestly don't know how to make it better at the moment. A stack of three foreach-loops can't be the solution however, that's for sure...


    The Code:
    PHP:
            foreach($params['templates'] as &$templateCategory) {                                   // for every category
                
    if(isset($templateCategory['templates'])) {                                         // templates exist?
                    
    foreach($templateCategory['templates'] as &$template) {                         // for every template
                        
    $arrayForums    unserialize($template['forums']);                         // all forums
                        
    $arrayRMC       unserialize($template['resource_manager_categories']);    // all categories
                    
                        
    $forums         = array();
                        
    $rmcs           = array();
                     
                        foreach(
    $arrayForums as $forum)
                            if(!(
    $forum === 'all'))
                                
    $forums[] = $nodes[$forum]['title'];                                    // get forum names
                        
    foreach($arrayRMC as $rmc) {
                            if(!(
    $rmc === 'all'))
                                
    $rmcs[] = $categories[$rmc]['category_title'];                          // get category names
                        
    }
                     
                        
    $template['forums'] = implode(', 'array_merge($forums$rmcs));           // merge and implode arrays to generate string
                    
    }
                }
            }
    And examples for the variables (I've removed all unnecessary columns to make it easier to read and handle):

    $nodes
    PHP:
    Array
    (
        [
    5] => Array
            (
                [
    node_id] => 5
                
    [title] => Groups
            
    )
    )
    $categories
    PHP:
    Array (
    [
    3] => Array
            (
                [
    resource_category_id] => 3
                
    [category_title] => Child Category
            
    )
    )
    $params['templates']
    PHP:
    Array
    (
        [
    1] => Array
            (
                [
    category_id] => 1
                
    [templates] => Array
                    (
                        [
    0] => Array
                            (
                                [
    template_id] => 1
                                
    [forums] => a:4:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:2:"12";i:3;s:1:"5";}
                                [
    resource_manager_categories] => a:3:{i:0;s:1:"1";i:1;s:1:"3";i:2;s:1:"2";}
                            )

                        [
    1] => Array
                            (
                                [
    template_id] => 2
                                
    [forums] => a:1:{i:0;s:2:"12";}
                                [
    resource_manager_categories] => a:1:{i:0;s:3:"all";}
                            )
                    )
            )

    )

    The only thing that matters is the output here: a string containing the name of all selected forums and resource manager categories in the form "Main Category, Main Forum, Forum 2, Groups, Example Category, Child Category, Different Category". If someone has an idea, I'd be glad to hear it.
     

Share This Page