to fetch array key and compare wih a key in a different array in php -
i have table stores subjects offered , table stores results scored students in exam, in displaying results using view table, want system display (-) student not take subject , score taking subject.
the sample array of subjects stored is:
array ( [0] => stdclass object ( [subcode] => 101 [subname] => english ) [1] => stdclass object ( [subcode] => 102 [subname] => kiswahili ) [2] => stdclass object ( [subcode] => 121 [subname] => mathematics ) [3] => stdclass object ( [subcode] => 231 [subname] => biology ) [4] => stdclass object ( [subcode] => 232 [subname] => physics ) [5] => stdclass object ( [subcode] => 233 [subname] => chemistry ) [6] => stdclass object ( [subcode] => 311 [subname] => history ) [7] => stdclass object ( [subcode] => 312 [subname] => geography ) [8] => stdclass object ( [subcode] => 313 [subname] => cre ) [9] => stdclass object ( [subcode] => 443 [subname] => agriculture ) [10] => stdclass object ( [subcode] => 565 [subname] => business studies ) )
the array contains results of student display is:
array ( [0] => stdclass object ( [admno] => 2129 [subcode] => 101 [score] => 78 ) [1] => stdclass object ( [admno] => 2129 [subcode] => 102 [score] => 80 ) [2] => stdclass object ( [admno] => 2129 [subcode] => 121 [score] => 70 ) [3] => stdclass object ( [admno] => 2129 [subcode] => 231 [score] => 76 ) [4] => stdclass object ( [admno] => 2129 [subcode] => 233 [score] => 76 ) [5] => stdclass object ( [admno] => 2129 [subcode] => 311 [score] => 85 ) [6] => stdclass object ( [admno] => 2129 [subcode] => 313 [score] => 90 ) [7] => stdclass object ( [admno] => 2129 [subcode] => 565 [score] => 80 ) [8] => stdclass object ( [admno] => 4093 [subcode] => 101 [score] => 60 ) [9] => stdclass object ( [admno] => 4093 [subcode] => 102 [score] => 70 ) [10] => stdclass object ( [admno] => 4093 [subcode] => 121 [score] => 85 ) [11] => stdclass object ( [admno] => 4093 [subcode] => 231 [score] => 80 ) [12] => stdclass object ( [admno] => 4093 [subcode] => 232 [score] => 80 ) [13] => stdclass object ( [admno] => 4093 [subcode] => 233 [score] => 80 ) [14] => stdclass object ( [admno] => 4093 [subcode] => 312 [score] => 95 ) [15] => stdclass object ( [admno] => 4093 [subcode] => 565 [score] => 86 ) )
i need solution view displays (-) subcode in subjects array not found in results array.
<?php $subjects = array ( array ( 'subcode' => 101, 'subname' => 'english' ), array ( 'subcode' => 102, 'subname' => 'kiswahili' ), array ( 'subcode' => 121, 'subname' => 'mathematics' ), array ( 'subcode' => 231, 'subname' => 'biology' ), array ( 'subcode' => 232, 'subname' => 'physics' ), array ( 'subcode' => 233, 'subname' => 'chemistry' ), array ( 'subcode' => 311, 'subname' => 'history' ), array ( 'subcode' => 312, 'subname' => 'geography' ), array ( 'subcode' => 313, 'subname' => 'cre' ), array ( 'subcode' => 443, 'subname' => 'agriculture' ), array ( 'subcode' => 565, 'subname' => 'business studies' ) ) ; $scores = array ( '0' => array ( 'admno' => 2129, 'subcode' => 101, 'score' => 78 ), '1' => array ( 'admno' => 2129, 'subcode' => 102, 'score' => 80 ), '2' => array ( 'admno' => 2129, 'subcode' => 121, 'score' => 70 ), '3' => array ( 'admno' => 2129, 'subcode' => 231, 'score' => 76 ), '4' => array ( 'admno' => 2129, 'subcode' => 233, 'score' => 76 ), '5' => array ( 'admno' => 2129, 'subcode' => 311, 'score' => 85 ), '6' => array ( 'admno' => 2129, 'subcode' => 313, 'score' => 90 ), '7' => array ( 'admno' => 2129, 'subcode' => 565, 'score' => 80 ), '8' => array ( 'admno' => 4093, 'subcode' => 101, 'score' => 60 ), '9' => array ( 'admno' => 4093, 'subcode' => 102, 'score' => 70 ), '10' => array ( 'admno' => 4093, 'subcode' => 121, 'score' => 85 ), '11' => array ( 'admno' => 4093, 'subcode' => 231, 'score' => 80 ), '12' => array ( 'admno' => 4093, 'subcode' => 232, 'score' => 80 ), '13' => array ( 'admno' => 4093, 'subcode' => 233, 'score' => 80 ), '14' => array ( 'admno' => 4093, 'subcode' => 312, 'score' => 95 )/*, '15' => array ( 'admno' => 4093, 'subcode' => 565, 'score' => 86 )*/); $students = array(2129,4093); print_r($students); function took_test($scores,$admno, $subj){ $r = false; foreach($scores $score){ if($score['subcode'] == $subj && $score['admno'] == $admno){ $r = "<td>".$score['score']."</td>"; } } return $r; } ?> <table border="1"> <tr> <td>student admno</td> <?php foreach($subjects $subj){ echo "<td>".$subj['subname']."</td>"; } ?> </tr> <?php foreach($students $s){ echo "<tr>"; echo "<td>".$s."</td>"; foreach($subjects $subj){ if(!took_test($scores,$s,$subj['subcode'])){ echo "<td>-</td>"; }else{ echo took_test($scores,$s,$subj['subcode']); } } echo "</tr>"; } ?> </table> <pre><?php print_r($subjects); print_r($scores); ?></pre>
please take time optimize codes
Comments
Post a Comment