I want to run a foreach loop from database, but I dont know how to start. I have an array which I have generated from a while loop:
/* mysql query for geting leave_type ID */
$leaveType = mysql_query("
SELECT `leave`.leave_type_id_leave_type,
`leave`.staff_leave_application_staff_id_staff,
`leave`.date,
`leave`.date_updated,
`leave`.active
FROM `leave`
WHERE `leave`.staff_leave_application_staff_id_staff = $iid
GROUP BY `leave`.leave_type_id_leave_type
");
/* Now put all leave Type ID in an array */
echo "<table>";
$types = array();
while($leaveFW = mysql_fetch_array( $leaveType )){
$types[] = $leaveFW['leave_type_id_leave_type'];
}
print_r($types);
Now I want to run a foreach loop which will query below code for each ID in array. :
$leaveQ = mysql_query("SELECT Count(*) as total, monthname(date) as
month FROM `leave`
WHERE `leave`.staff_leave_application_staff_id_staff = $iid
and `leave`.leave_type_id_leave_type = $type");
I want to show $leaveQ['month']
and $leaveQ['total']
in foreach loop.
May be my foreach like this, but how to get $type['month']
and $type['total']
:
foreach ($types as $type)
{
$leaveQ = mysql_query("SELECT Count(*) as total, monthname(date) as month
FROM `leave` WHERE `leave`.staff_leave_application_staff_id_staff = $iid
and `leave`.leave_type_id_leave_type = $type");
}
May youwant something like this
foreach($types as $result)
{
$iid = $result['staff_leave_application_staff_id_staff'];
$type = $result['leave_type_id_leave_type'];
$leaveQ = mysql_query("SELECT Count(*) as total, monthname(date) as
month FROM `leave`
WHERE `leave`.staff_leave_application_staff_id_staff = $iid
and `leave`.leave_type_id_leave_type = '".$leaveFW['leave_type_id_leave_type']."'");
while($row = mysql_fetch_assoc($leaveQ))
{
echo $row['month']."<br>";
echo $row['total']."<br>";
}
}
foreach($types as $type){
$leaveQ = mysql_query("SELECT Count(*) as total, monthname(date) as
month FROM `leave`
WHERE `leave`.staff_leave_application_staff_id_staff = $iid
and `leave`.leave_type_id_leave_type = $type");
while($leave = mysql_fetch_assoc($leaveQ)){
var_dump($leave['total'] , $leave['month']);
}
}
Don't do this!
The proper way to do this is using JOIN operations, and instead of a query for each line, only one query to get all the data.
Blindly following that advice, not changing too much, just merging the two queries, your query should look like this:
SELECT Count(*) as total, monthname(date) as month, types.leave_type_id_leave_type
FROM `leave`
JOIN (SELECT DISTINCT `leave`.leave_type_id_leave_type
FROM `leave`
WHERE `leave`.staff_leave_application_staff_id_staff = $iid) as types
ON leave.leave_type_id_leave_type = types.leave_type_id_leave_type
WHERE `leave`.staff_leave_application_staff_id_staff = $iid
GROUP BY monthname(date), types.leave_type_id_leave_type
Differences to your approach:
To make this even better:
SELECT Count(*) as total, monthname(date) as month, leave_type_id_leave_type
FROM `leave`
WHERE `leave`.staff_leave_application_staff_id_staff = $iid
GROUP BY monthname(date), leave_type_id_leave_type
Differences now:
EDIT
If I understand correctly you want to loop thru the types found from your block of code and then issue more database queries and extract more data.
You can basically reuse your first block of code again, wrapped with a foreach loop:
foreach($types as $type){
$query = mysql_query("YOUR_SQL_USING_$TYPE");
while($row = mysql_fetch_array( $query )){
print_r($row);
// or print($row['COLUMN_NAME']);...
}
}