简体   繁体   中英

Store values in 2d array and retrieve in 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: 在此处输入图片说明

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

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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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