doujia4041 2014-11-10 05:52
浏览 29
已采纳

将值存储在2d数组中并在PHP中检索

I have an array which stores Some section Names Like a,b,c and in each section there are questions like 'a' section contains q1,q2,q3. First i get those sections from myDB and then pass those section ID's to find questions query.

And Each Question has Score. Now what i want is the question which has 0 achieved score but have some applicable Score. i want those sections and their questions one by one.

This piece of code is working fine.

See Below

<?php
     $sectionIDs = $obj->getSectionIDs($formatID); //section ID array starting from 225
     $sectionNames = $obj->getSectionNames($formatID);

     for($i=0; $i<count($sectionIDs); $i++)
     {
        $section_name_print_check ="" ;
        $question_ids = $obj->getQuestionID($formatID,$sectionIDs[$i]);
        $questionName = $obj->getQuestionName($formatID,$sectionIDs[$i]);
        for($j=0; $j<count($question_ids); $j++)
        {
           $score_achieved = $obj->getScore_least($question_ids[$j],$formatID,$last_waveID,$received_ID);
           $score_applicable = $obj->getScore_least_applicabe($question_ids[$j],$formatID,$last_waveID,$received_ID);
           if($score_achieved == 0 && $score_applicable != 0)
           {
               if($section_name_print_check == "" )
               {
                   $final_result_arr[$k]["SectionName"] = $sectionNames[$i];
                   $section_name_print_check = $sectionNames[$j];
               }
               $final_result_arr[$k]["QuestionName"] = $questionName[$j];
               $k++;
           }
        }
     }
?>

But in Retrieval i got some issues like extra table row. How can i get over rid of this problem. I got second echo each time. What i want is to get this echo only when new section comes.

Retrieval Code:

for($i=0; $i<count($final_result_arr); $i++)
{
   echo '<tr style="background-color:#6F6; font-weight:bold;"><td style="width:700px;" colspan="4">'.$final_result_arr[$i]["SectionName"].'</td></tr>';  
   echo '<tr><td style="width:15px;">Sr.</td><td width="399px"></td><td style="width:143px;" align="center">Achieved Score</td><td style="width:143px;" align="center">Applicable Score</td></tr>';
   echo '<tr><td style="width:15px;">'.++$serial.'</td><td style="width:399px;">'.$final_result_arr[$i]["QuestionName"].'</td><td align="center" style="width:143px;">0%</td><td align="center" style="width:143px;">100%</td></tr>';
   echo '<tr><td colspan="5" style="height:25px;"></td></tr>';  
}

The Output is: enter image description here

Here i got Extra Table Row instead of getting one by one sections and question. What could be possible solution for this problem? Should i change my array into simple array or should i change my display section? Any Help! Thanks in Advance

  • 写回答

1条回答 默认 最新

  • duai3681 2014-11-10 09:48
    关注

    First off, your retrieval code is not the same as your output picture as your retrieval code would also show the green section bar for every question aswell.

    Second, I suggest setting up your array in a different way. Your current structure:

    array[0] => (
        array[0] => (['SectionName']  = "Skills Evaluation")
        array[1] => (['QuestionName']  = "Did staff offer any other product?")
    )
    array[1] => (
        array[0] => (['SectionName']  = "Skills Evaluation")
        array[1] => (['QuestionName']  = "Was the conversation closed professionaly?")
    )
    

    This means that whenever you have multiple questions per section you store the section name multiple times. Your current code assumes that the questions will all be sorted correctly. If your array is not at any point sorted correctly, your code will not display the questions per section.

    You could go for a structure where you use the section as a key to an array with questions.

    $final_result_arr['SectionName'][$indexOfQuestion] = "Question";
    
    array['Skills Evaluation'] => (
        array['Skills Evaluation'][0] = "Question"
        array['Skills Evaluation'][1] = "Another question"
    )
    array['Branch environment'] => (
        array['Branch enviroment'][0] = "Question"
    )
    

    With a structure like this you could then fetch the output like this:

    foreach ($final_result_arr as $section => $questions)
    {
        echo '<tr style="background-color:#6F6; font-weight:bold;"><td style="width:700px;" colspan="4">'.$section.'</td></tr>';  
        echo '<tr><td style="width:15px;">Sr.</td><td width="399px"></td><td style="width:143px;" align="center">Achieved Score</td><td style="width:143px;" align="center">Applicable Score</td></tr>';
        foreach ($questions as $question)
        {
            echo '<tr><td style="width:15px;">'.++$serial.'</td><td style="width:399px;">'.$question.'</td><td align="center" style="width:143px;">0%</td><td align="center" style="width:143px;">100%</td></tr>';
            echo '<tr><td colspan="5" style="height:25px;"></td></tr>';  
        }
    }
    

    You'll obviously have to adjust your code to fill up the array though, if you then want to also store the achieved and applicable score you could instead make $question an array and have it store those values.

    Filling up the array:

    if($score_achieved == 0 && $score_applicable != 0)
    {
        $final_result_arr[$sectionName[$i]][] = $questionName[$j];
    }
    

    Or if you want the scores aswell:

    if($score_achieved == 0 && $score_applicable != 0)
    {
        $final_result_arr[$sectionName[$i]][] = array($questionName[$j], 'score', 'score');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里