php - How to minimise code and avoid repetition? -
i learning php.
i want calculate number of absent students in class in subjects in examination.
i have used following code extract data 1 class , 2 subjects.
i want calculate number of absent students 4 classes , 27 subjects how achieve using minimum code , avoiding repetition?
<?php /*query calculate number of absent students in fyba in 6 subjects. counting studid (student id) economics = marks1e*/ $e = mysql_query('select count(studid) examdbf1 exam_id = 14153 , faculty = 1 , sem = 1 , repeater = 1 , marks1e = \'aa\''); $ea = mysql_fetch_array($e); echo '<br>'. $ea['count(studid)'] . '<br>'; /*marathi optional = marks2e*/ $mo = mysql_query('select count(studid) examdbf1 exam_id = 14153 , faculty = 1 , sem = 1 , repeater = 1 , marks2e = \'aa\''); $moa = mysql_fetch_array($mo); echo '<br>'. $moa['count(studid)'] . '<br>';
using code comment, change ***marks1e***
$m[$x]
-
$m = array("marks1e", "marks2e", "marks3e", "marks4e", "marks5e", "marks6e"); $mcount = count($m); for($x = 0; $x < $mcount; $x++) { $e = mysql_query('select count(studid) examdbf1 exam_id = 14153 , faculty = 1 , sem = 1 , repeater = 1 , '.$m[$x].' = \'aa\''); $ea = mysql_fetch_array($e); echo '<br>'.$m[$x].': '. $ea['count(studid)'] . '<br>'; }
similarly, since changing value $m
, foreach()
-
$m = array("marks1e", "marks2e", "marks3e", "marks4e", "marks5e", "marks6e"); foreach($m $n) { $e = mysql_query('select count(studid) examdbf1 exam_id = 14153 , faculty = 1 , sem = 1 , repeater = 1 , '.$n.' = \'aa\''); $ea = mysql_fetch_array($e); echo '<br>'.$n.': '. $ea['count(studid)'] . '<br>'; }
Comments
Post a Comment