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

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

本文关键字:于更多 标准 计算 方法 是否 更好 FOR      更新时间:2023-09-26

这是我第一次使用php和javascript,多亏了这个伟大的网站,我成功地把我需要它做的事情粘合在一起。

现在,我有一个.txt文件,其中包含实际技能列表;根据文件中有多少行,页面加载具有相同行数的表(我使用array管理数据,因此使用foreach)。

然后还有另外2个txt文件,第一个是所有代理的列表,第二个是技能列表,其中当前登录的代理;由此,我创建了第二个表,其中列出了不同团队(列)中登录的代理和技能(行)(同样,我将.txt中的所有数据放入数组,并使用foreach构建表)。

现在,我已经可以做的是能够计算目前有多少代理分别登录不同团队的每个技能。因为我在需要一些条件格式时安装了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
      }
    }
  }
});

但是它使页面加载整整2秒,只有5个团队(每个10个代理)和10个技能。那么,还有比使用多个FOR更有效的方法来处理这样的任务吗?

谢谢你这么多的帮助,我喜欢这个网站;就像我说的,这是我第一次尝试php和javascript,我真的很喜欢玩它,我认为自己很擅长用谷歌搜索,但我找不到任何关于这个的页面。干杯!

编辑:我试过没有它,加载的长度是由于这个脚本。

如果您在发送头文件之前使用PHP读取文本文件,我会在您迭代文本文件时将#skill_log项创建为PHP字符串,以便稍后在页面中回显。在使用Jquery时,您必须首先加载文件,然后在代码开始运行之前加载浏览器中的整个DOM。你没有说发生了多少次迭代,但我认为在PHP中会快得多。

我不完全清楚你的数组,但使用PHP函数,如array_search应该让你做到这一点,而不用在你的代码中遍历两个数组。也许像这样:

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 . '"';
}
}

答案如下:

使用php要快得多,可能是因为我已经加载了所有的数组。我确实用FOREACH循环了所有3个使用过的数组(团队、技能、日志代理),使用IF来限制每一步的选择:

  1. FOREACH划分团队
  2. FOREACH在每个团队中循环技能
  3. FOREACH循环所有登录
  4. 的代理
  5. IF语句按团队筛选代理
  6. IF语句按所选座席组内的技能进行筛选
  7. 计数+ +:)

这是5+6部分的代码:

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

谢谢你的灵感!如果你有一些简单的教程如何创建web sgl数据库,并与php连接,我将不胜感激。玩得开心!