extend functionality of php array parameter function

Let's say I have a function that given an array it returns a piece of code like

//the array
$arr = array("EAL"   => "Eal",
         "CAZO"  => "Cazo",
         "COS"   => "Cos"           
        );

$result = '';
foreach ($arr as $key => $value){
    $result .= ' $("'.$key.'").click(function() { '
        . '        $("'.$value.'").fadeToggle();'
        . '  });'  
}
$result .= ' ; ';

which would look like

$result=' $("#EAL").click(function() { '
      . '   $("#Eal").fadeToggle();'
      . '    });'
      . ' $("#CAZO").click(function() { '
      . '   $("#Cazo").fadeToggle();'
      . '    });'
      . ' $("#COS").click(function() { '
      . '   $("#Cos").fadeToggle();'
      . '    });'  ;

Now how can I accomplish the case when I have multiple values for one key like:

$("#CAZO").click(function() { 
   $("#Cazo_x").fadeToggle();
   $("#Cazo_y").fadeToggle();
   $("#Cazo_z").fadeToggle();
});

so final result would be

$result=' $("#EAL").click(function() { '
      . '   $("#Eal").fadeToggle();'
      . '    });'
      . ' $("#CAZO").click(function() { '
      . '   $("#Cazo_x").fadeToggle();'
      . '   $("#Cazo_y").fadeToggle();'
      . '   $("#Cazo_z").fadeToggle();'
      . '    });'
      . ' $("#COS").click(function() { '
      . '   $("#Cos").fadeToggle();'
      . '    });'  ;

Answers


Use a class to avoid all that duplicate code. Then you can do:

$(".fadeThis").on('click', function(){

  // the ID of the element is available as this.id
  $(this).fadeToggle();      

  // $(this).children() to get the children...

});

$arr = [
    'EAL'  => 'Eal',
    'CAZO' => [
        'Cazo_x',
        'Cazo_y',
        'Cazo_z'
    ],
    'COS'  => 'Cos'           
];

$result = '';
foreach($arr as $key => $value) {
    $result .= '$("'.$key.'").click(function() {';
    if(!is_array($value)) { $value = [$value]; }
    foreach($value as $v) {
        $result .= '$("'.$v.'").fadeToggle();'
    }
    $result .= '});';
}

Try this :

//the array
$arr = array("EAL"   => "Eal",
     "CAZO"  => "Cazo",
     "COS"   => "Cos","key2" =>array("value1","value2")          
    );

$result = '';
foreach ($arr as $key => $value){
$result .= ' $("'.$key.'").click(function() { ';
    if( is_array( $value )){
        foreach( $value as $val )
        {
           $result .='        $("'.$val.'").fadeToggle();'
        }
    }else{
      $result .= '        $("'.$value.'").fadeToggle();'

    }
    $result .= '  });'  
}
$result .= ' ; ';

Need Your Help

Average time between sql edits in php

php mysql

I currently have a system to track inventory items.

Go back in url-pattern (invoking servlet from a jsp page)

html xml jsp

I have made small web-app in jsp with a start page with login and some restricted pages with logout, -everything controlled with filters and servlets. I started making it because I wanted to learn ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.