I would like to use a query on a function dynamically. I would like to use the same function multiple times. How do I call this function based on the $type
variable?
This what I attempted. I know it is not complete, but I don't know what I have to do to get it to work.
function GeneralQuery($connMysqli){
// Query mounting according condition of $type
if(empty($type)){
if($type = "")
{
if($type == "school")
{
$type = 'GROUP BY school
ORDER BY school';
}
else if($type == "class")
{
$type = 'GROUP BY class
ORDER BY class';
}
else
{
$type == "student";
$type = 'GROUP BY class, student
ORDER BY class, avg DESC';
}
}
}
$sql = "SELECT avg(finalgrade) as avg, u.lastname as school, u.department as class, u.id as student from fhrw_grade_grades gg
inner join fhrw_user u on u.id=gg.userid
where gg.rawgrade is not null " . $type . "";
$res = mysqli_query($connMysqli,$sql)or die(mysqli_error($connMysqli));
$list = array();
while($item = mysqli_fetch_assoc($res)){
$list[] = $item;
}
return $list;
}
// I don't know what to do here to call query result for School -
$data = GeneralQuery($connMysqli);
foreach($data as $item){
echo $item['avg'] . ' - ' . $item['School'] . '<br>';
}
// I don't know what to do here to call query result for Class
$data = GeneralQuery($connMysqli);
foreach($data as $item){
echo $item['avg'] . ' - ' . $item['class'] . '<br>';
}
// I don't know what to do here to call query result for Student
$data = GeneralQuery($connMysqli);
foreach($data as $item){
echo $item['avg'] . ' - ' . $item['Student'] . '<br>';
}
// close connection
mysqli_close($connMysqli);