[英]Is there a better way to count specific values based on more criteria than using multiple FOR?
這是我第一次使用php和javascript,並且僅由於這個出色的網站,我設法將需要做的事情粘合在一起。
現在,我有一個.txt文件,其中包含實際技能列表; 根據文件中的行數,頁面加載具有相同行數的表(我使用數組管理數據,因此使用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
}
}
}
});
但是,只有5個小組(每個小組10個探員)和10個技能,整個2秒鍾的頁面載入時間。 那么,有沒有比使用多個FOR更有效的方法來處理此類任務?
非常感謝您的幫助,我喜歡這個網站; 就像我說的那樣,這是我第一次嘗試php和javasript,我真的很喜歡玩它,我認為自己在使用谷歌搜索方面非常出色,但是我找不到關於此的任何頁面。 干杯!
編輯:由於此腳本,我嘗試不使用它,並且加載IS的時間較長。
如果在發送標題之前使用PHP閱讀文本文件,則在遍歷文本文件時將#skill_log項目創建為PHP字符串,以稍后在頁面中回顯。 使用Jquery進行此操作,您必須先加載文件,然后在代碼甚至開始運行之前先在瀏覽器中加載整個DOM。 您沒有說發生了多少次迭代,但是我認為在PHP中它將更快。
我對您的數組不是很清楚,但是使用像array_search這樣的PHP函數應該可以做到這一點,而無需遍歷代碼中的兩個數組。 也許是這樣的:
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限制了每個步驟的選擇:
這是5 + 6的部分代碼:
if (count( array_keys( $agent_subarray, print_r($skill_subarray['Skill']) )) > 0) {
$count++;
}
感謝您的靈感! 如果您有一些簡單的教程,如何創建Web sql數據庫並將其與php連接,我將不勝感激。 玩得開心!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.