简体   繁体   English

是否有比使用多个FOR更好的基于更多条件来计算特定值的方法?

[英]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. 这是我第一次使用php和javascript,并且仅由于这个出色的网站,我设法将需要做的事情粘合在一起。

Now, I have a .txt file with list of actual skills ; 现在,我有一个.txt文件,其中包含实际技能列表; 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). 根据文件中的行数,页面加载具有相同行数的表(我使用数组管理数据,因此使用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; 然后还有2个其他txt文件,第一个包含所有座席列表,第二个包含当前座席当前登录技能列表。 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). 由此,我创建了第二个表,在这里我看到列出的代理和他们在不同团队(列)中登录(行)的技能(同样,.txt中的所有数据都进入数组,并使用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: 由于我在需要某种条件格式时安装了jquery,因此我也决定使用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. 但是,只有5个小组(每个小组10个探员)和10个技能,整个2秒钟的页面载入时间。 So is there any more efficient way to process such task than using multiple FOR? 那么,有没有比使用多个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. 就像我说的那样,这是我第一次尝试php和javasript,我真的很喜欢玩它,我认为自己在使用谷歌搜索方面非常出色,但是我找不到关于此的任何页面。 Cheers! 干杯!

edit: I tried without it and the length of loading IS because of this script. 编辑:由于此脚本,我尝试不使用它,并且加载IS的时间较长。

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. 如果在发送标题之前使用PHP阅读文本文件,则在遍历文本文件时将#skill_log项目创建为PHP字符串,以稍后在页面中回显。 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. 使用Jquery进行此操作,您必须先加载文件,然后在代码甚至开始运行之前先在浏览器中加载整个DOM。 You don't say how many iterations are happening, but I think it would be much faster in PHP. 您没有说发生了多少次迭代,但是我认为在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. 我对您的数组不是很清楚,但是使用像array_search这样的PHP函数应该可以做到这一点,而无需遍历代码中的两个数组。 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. 使用php可以更快,可能是因为我已经加载了所有数组。 I did loop all 3 used arrays (teams, skills, logged agents) with FOREACH, using IF to limit the selection on each step: 我确实使用FOREACH循环了所有3个使用过的数组(团队,技能,已记录的业务代表),并使用IF限制了每个步骤的选择:

  1. FOREACH to divide teams 划分团队
  2. FOREACH to loop skills within each team 传授每个团队中的技能
  3. FOREACH to loop all agents logged in FOREACH循环登录的所有座席
  4. IF statement to filter agents by team IF语句按团队筛选代理
  5. IF statement to filter by skill within selected group of agents IF语句,用于根据所选代理组中的技能进行过滤
  6. count++ :) count ++ :)

Here's the 5+6 part code: 这是5 + 6的部分代码:

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. 如果您有一些简单的教程,如何创建Web sql数据库并将其与php连接,我将不胜感激。 Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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