简体   繁体   中英

Is there a better way to count specific values based on more criteria than using multiple FOR?

This is my very first time using php and javascript, and thanks only to this great site, I managed to glue together pieces of what I need it to do.

Now, I have a .txt file with list of actual skills ; based on how many lines there is in the file, the page loads table with same number of rows (I manage data using array, thus foreach).

Then there are 2 other txt files, first with list of all agents, second with list of skills where are currently agents logged in; from this I create second table, where I see listed agents and skill they are logged in (rows) in different teams (columns) (again, all data from .txt I get into arrays, and build the table using foreach).

Now, what I already can do is to be able to count how many agents there are currently logged separately in each skill from different team. Since I installed jquery when I needed some conditioned formating, I also decided to use jquery:

$(document).ready(function(){
  var jtym = $("div.tttt").html();  // this is how I manage to get the total number of teams;
  var jzel = $("div.hhhh").html();  // this is how I manage to get the total number of used skills;

  for ( var j = 1; j <= jtym; j++ ) {  // to loop through all teams, e.g. table columns (they have IDs like team1, team2, team3... created using foreach); 
    for ( var i = 0; i <= jzel; i++ ) {  // to loop through all agents in one team, e.g. rows in one column (they have IDs like skill1, skill2, skill3... created using foreach);
      var v_skill_u =  $( '#team' + j + ':contains("' + $( "[id=skill" + i + "u]" ).html() + '") ').length;  // skill1-10 or more I get from txt file into first table; this is how I get what to count;
      if (v_skill_u > 0){  // avoid writing if the count is 0 to make it more well arranged;
        $( '#skill_log' + i + '_team' + j).text( v_skill_u );  // show the final reasult in table where again column = team and rows are agents logged into skill
      }
    }
  }
});

But it makes the page load for whole 2 seconds with only 5 teams (10 agents each) and 10 skills. So is there any more efficient way to process such task than using multiple FOR?

Thank you so much for help, I love this site; like I said, this is my very first time I try php and javasript and I really love playing with it and I consider myself to be quite good with googling but I could not find any page about this. Cheers!

edit: I tried without it and the length of loading IS because of this script.

If you're reading the text files with PHP before the headers are sent, I'd create your #skill_log items as PHP strings while you iterate through the text files, to later echo in the page. Doing this with Jquery, you're having to load the files first, then load the whole DOM in the browser before your code even starts running. You don't say how many iterations are happening, but I think it would be much faster in PHP.

I'm not entirely clear on your arrays, but using a PHP function like array_search should let you do this without iterating through both arrays in your code. Maybe like this:

foreach ($skill_array as $enumKey => $skill) {
$key_found = array_search($skill, $team_array);
if($key_found !== false){
    $html_output .= '<input id="skill_log' . $enumKey . '_team' . $key_found . ' value="' . $skill . '"';
}
}

Here's answer:

its much faster using php, probably because I have already loaded all the arrays. I did loop all 3 used arrays (teams, skills, logged agents) with FOREACH, using IF to limit the selection on each step:

  1. FOREACH to divide teams
  2. FOREACH to loop skills within each team
  3. FOREACH to loop all agents logged in
  4. IF statement to filter agents by team
  5. IF statement to filter by skill within selected group of agents
  6. count++ :)

Here's the 5+6 part code:

if (count( array_keys( $agent_subarray, print_r($skill_subarray['Skill']) )) > 0) {
   $count++;
}

Thanks for the inspiration! And if you have some easy tutorial how to create web sgl database and connect it with php I'd be grateful. Have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM