Monday, 15 August 2011

Store values in 2d array and retrieve in PHP -



Store values in 2d array and retrieve in PHP -

i have array stores section names a,b,c , in each section there questions 'a' section contains q1,q2,q3. first sections mydb , pass section id's find questions query.

and each question has score. want question has 0 achieved score have applicable score. want sections , questions 1 one.

this piece of code working fine.

see below

<?php $sectionids = $obj->getsectionids($formatid); //section id array starting 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 got issues table row. how can on rid of problem. got sec echo each time. want echo 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 got table row instead of getting 1 1 sections , question. possible solution problem? should alter array simple array or should alter display section? help! in advance

first off, retrieval code not same output image retrieval code show greenish section bar every question aswell.

second, suggest setting array in different way. current structure:

array[0] => ( array[0] => (['sectionname'] = "skills evaluation") array[1] => (['questionname'] = "did staff offer other product?") ) array[1] => ( array[0] => (['sectionname'] = "skills evaluation") array[1] => (['questionname'] = "was conversation closed professionaly?") )

this means whenever have multiple questions per section store section name multiple times. current code assumes questions sorted correctly. if array not @ point sorted correctly, code not display questions per section.

you go construction utilize section key array 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 construction fetch output this:

foreach ($final_result_arr $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 $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 have adjust code fill array though, if want store achieved , applicable score instead create $question array , have store values.

filling array:

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

or if want scores aswell:

if($score_achieved == 0 && $score_applicable != 0) { $final_result_arr[$sectionname[$i]][] = array($questionname[$j], 'score', 'score'); }

php arrays

No comments:

Post a Comment