我正在学习PHP。如何最小化代码并避免重复?
我想计算所有科目在考试中缺课学生的人数。
我已经使用下面的代码来提取一个类和2个主题的数据。
我想计算四个班级和27个科目的缺席学生人数如何通过使用最小代码和避免重复来实现?
<?php
/*Query to calculate number of absent students in FYBA in all 6 subjects.
by counting studid (student id)
Economics = marks1e*/
$e = mysql_query('select count(studid) from examdbf1 where exam_id = 14153 and faculty = 1 and sem = 1 and repeater = 1 and marks1e = \'AA\'');
$ea = mysql_fetch_array($e);
echo '<br>'. $ea['count(studid)'] . '<br>';
/*Marathi Optional = marks2e*/
$mo = mysql_query('select count(studid) from examdbf1 where exam_id = 14153 and faculty = 1 and sem = 1 and repeater = 1 and marks2e = \'AA\'');
$moa = mysql_fetch_array($mo);
echo '<br>'. $moa['count(studid)'] . '<br>';
使用mysqli_而不是mysql_阅读http://php.net/manual/en/function.mysql-query.php – user1844933
(1)找到你的可重复性与变化的数据,(2)把你的数据改变成一个数组,(3)把你的可重复数据放在一个循环中,即。 * for()*,* foreach()*,* while()*或函数,用可变数据的占位符,(4)使用数组运行循环/函数,(5)更新数据库库* mysql_ *到* mysqli *或* PDO * – Sean
@Sean按照您的建议创建了数组和循环。无法理解如何为可变数据添加占位符。 '$ m = array(“marks1e”,“marks2e”,“marks3e”,“marks4e”,“marks5e”,“marks6e”); $($ x = 0; $ x <$ mcount; $ x ++){ $ e = mysql_query('select countdb(studid)from examdbf1 where exam_id = 14153和faculty = 1和sem = 1,repeater = 1和*** marks1e *** = \'AA \''); $ ea = mysql_fetch_array($ e); echo'
'。 $ ea ['count(studid)']。 '
';' –