我在我的网站上使用PHP,MySQL,Smarty,jQuery,AJAX等.目前,我从MySQL数据库中获取大量数据(匹配问题ID),对其进行处理,将此数据分配给Smarty模板并将其打印在网页上.由于要获取的数据量太大而且正在进行进一步处理,因此获取最终输出数据需要花费太多时间.反过来,向用户显示整个数据需要花费太多时间.
我有一种方法,但无法实现它.我的方法是运行获取单个匹配question_id
并同时将其显示到浏览器的两个过程,并重复此循环,直到获取并显示所有匹配的问题ID.当显示单行的加载数据时,加载器图像应显示在该显示的记录下.当所有数据都被打印出来时,加载器图像应该消失.
但我面临的主要问题是我应该如何继续将数据分配给Smarty模板并显示模板,因为Smarty模板引擎首先加载所有内容,并且只有在完全拥有将其打印到浏览器的内容之后.
为了您的参考,我将控制器,模型和视图中的所有现有代码放在下面:
Controller(match_question.php)的PHP代码如下:
GetSimilarQuestionsBySubjectIdTopicId($subject_id, $topic_id); $smarty->assign('all_match_questions', $all_match_questions); $smarty->display("match-question.tpl") ?>
Model(QuestionMatch.php)的PHP代码如下:
mDb = $gDb; $this->mValidator = $gFormValidation; $this->mTableName = TBL_QUESTIONS; } /** * This function is used to get all the questions from the given subject id and topic id */ function GetSimilarQuestionsBySubjectIdTopicId($subject_id, $topic_id) { /*SQL query to find out questions from given subject_id and topic_id*/ $sql = " SELECT * FROM ".TBL_QUESTIONS." WHERE question_subject_id=".$subject_id; $sql .= " AND question_topic_id=".$topic_id; $this->mDb->Query($sql); $questions_data = $this->mDb->FetchArray(); /*Same array $questions_data is assigned to new array $questions to avoid the reference mismatching*/ $questions = $questions_data; /*Array of words to be excluded from comparison process *For now it's a static array but when UI design will be there the array would be dynamic */ $exclude_words = array('which','who','what','how','when','whom','wherever','the','is','a','an','and','of','from'); /*This loop removes all the words of $exclude_words array from all questions and converts all *converts all questions' text into lower case */ foreach($questions as $index=>$arr) { $questions_array = explode(' ',strtolower($arr['question_text'])); $clean_questions = array_diff($questions_array, $exclude_words); $questions[$index]['question_text'] = implode(' ',$clean_questions); } /*Now the actual comparison of each question with every other question stats here*/ foreach ($questions as $index=>$outer_data) { /*Logic to find out the no. of count question appeared into tests*/ $sql = " SELECT count(*) as question_appeared_count FROM ".TBL_TESTS_QUESTIONS." WHERE test_que_id="; $sql .= $outer_data['question_id']; $this->mDb->Query($sql); $qcount = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE); $question_appeared_count = $qcount['question_appeared_count']; $questions_data[$index]['question_appeared_count'] = $question_appeared_count; /*Crerated a new key in an array to hold similar question's ids*/ $questions_data[$index]['similar_questions_ids_and_percentage'] = Array(); $outer_question = $outer_data['question_text']; $qpcnt = 0; //foreach ($questions as $inner_data) { /*This foreach loop is for getting every question to compare with outer foreach loop's question*/ foreach ($questions as $secondIndex=>$inner_data) { /*This condition is to avoid comparing the same questions again*/ if ($secondIndex <= $index) { /*This is to avoid comparing the question with itself*/ if ($outer_data['question_id'] != $inner_data['question_id']) { $inner_question = $inner_data['question_text']; /*This is to calculate percentage of match between each question with every other question*/ similar_text($outer_question, $inner_question, $percent); $percentage = number_format((float)$percent, 2, '.', ''); /*If $percentage is >= $percent_match only then push the respective question_id into an array*/ if($percentage >= 85) { $questions_data[$index]['similar_questions_ids_and_percentage'][$qpcnt]['question_id'] = $inner_data['question_id']; $questions_data[$index]['similar_questions_ids_and_percentage'][$qpcnt]['percentage'] = $percentage; /*$questions_data[$secondIndex]['similar_questions_ids_and_percentage'][$qpcnt]['question_id'] = $outer_data['question_id']; $questions_data[$secondIndex]['similar_questions_ids_and_percentage'][$qpcnt]['percentage'] = $percentage;*/ /*Logic to find out the no. of count question appeared into tests*/ $sql = " SELECT count(*) as question_appeared_count FROM ".TBL_TESTS_QUESTIONS." WHERE test_que_id="; $sql .= $inner_data['question_id']; $this->mDb->Query($sql); $qcount = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE); $question_appeared_count = $qcount['question_appeared_count']; $questions_data[$index]['similar_questions_ids_and_percentage'][$qpcnt]['question_appeared_count'] = $question_appeared_count; $qpcnt++; } } } } } //} /*Logic to create the return_url when user clicks on any of the displayed matching question_ids*/ foreach ($questions_data as $index=>$outer_data) { if(!empty($outer_data['similar_questions_ids_and_percentage'])) { $return_url = ADMIN_SITE_URL.'modules/questions/match_question.php?'; $return_url .= 'op=get_question_detail&question_ids='.$outer_data['question_id']; foreach($outer_data['similar_questions_ids_and_percentage'] as $secondIndex=>$inner_data) { $return_url = $return_url.','.$inner_data['question_id']; } $questions_data[$index]['return_url'] = $return_url.'#searchPopContent'; } } /*This will return the complete array with matching question ids*/ return $questions_data; } } ?>
View(match-question.tpl)的代码如下:
Que ID | Matching Que IDs | Percentage(%) |
---|---|---|
QUE{$qstn.question_id}{if $qstn.question_appeared_count gt 0}-Appeared({$qstn.question_appeared_count}){/if} | {foreach from=$qstn.similar_questions_ids_and_percentage item=question key=q_no} {if $counter gt 1}||
{/if} | {if $question.question_id!=''} QUE{$question.question_id}{if $question.question_appeared_count gt 0}-Appeared({$question.question_appeared_count}){/if} {if $question.question_appeared_count eq 0} Delete{/if} {/if} | {if $question.percentage!=''}{$question.percentage}{/if} {assign var=counter value=$counter+1} |
No Questions Available |
感谢您花了一些宝贵的时间来理解我的问题.