-1

I have a table that stores subjects offered and a table that stores the results scored by the students in an exam, in displaying the results using a view table, I want the system to display a (-) where a student does not take that subject and a score where he is taking the subject.

The sample array of the 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 that contains results of the student to 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 a solution where the view displays a (-) where the subcode in the subjects array is not found in the results array.

1
  • foreach($subjects as $subject) { $code = $subject->subCode; foreach ($subjectresults as $subresult) { $subCode = $subresult->subCode; if (!$code) { echo "<td>"; echo $res = $subresult->score; echo "</td>"; } else { echo "<td>"; echo "-"; echo "</td>"; } } }
    – samlebo
    Commented May 13, 2013 at 7:00

2 Answers 2

0
<?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 as $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 as $subj){
    echo "<td>".$subj['subName']."</td>";
}
?>
</tr>
<?php
foreach($students as $s){
    echo "<tr>";
    echo "<td>".$s."</td>";
    foreach($subjects as $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 to optimize this codes

0

Combine both array and sort according to subcode, then the elements which are only passed once in the final array is what you are looking for.

You can use comparable or comparator to sort according to one instance in of your array if you are using java.

4
  • am using php codeigniter framework and i dont understand how i can loop through the two arrays comparing
    – samlebo
    Commented May 13, 2013 at 7:05
  • you dont have to loop, tru two arrays, i dont have any knowledge on php, but in java, i can do that combine both array and you will have new array now. sort it according to subCode, i dont know how you will do it in php. when you sort it you will have the following: arr[0]=101; arr[1]=101; arr[2]=102; arr[3]=102;... As you see, the common elements will come, one after the other
    – smttsp
    Commented May 13, 2013 at 10:10
  • that would too work well if i can get an array function to combine the two arrays.
    – samlebo
    Commented May 13, 2013 at 10:54
  • you should learn how to use google. This is basic array operation. http://php.net/manual/en/function.array-merge.php. Find something like that, but it should combine and protect ascending order while sorting, this is not supposed to be hard problem
    – smttsp
    Commented May 14, 2013 at 6:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.