php - Categorizing my foreach loop result -
my sql result getresult have many rows have different values in each column of header , subheader, wanted categorized, , add total result per each subheader counting getresult rows
$header = ""; $subheader = ""; $getresult = ""; foreach($querymysql row): if($header != $row->headercolumn) { $header = $row->headercolumn."<br />"; $get .= $header; } if($subheader != $row->subheadercolumn) { $subheader = $row->subheadercolumn."<br />"; $get .= $subheader; } $getresult = $row->getresultcolumn."<br />"; $get .= $getresult; endforeach;
result
usa location(header)
ict department(subheader)
1 (emp_name)getresult
2 (emp_name)getresult
3 (emp_name)getresult
total: 3 <- wanted add in output
accounting department(subheader)
1 (emp_name)getresult
2 (emp_name)getresult
3 (emp_name)getresult
4 (emp_name)getresult
total: 4 <- wanted add in output
pakistan(header)
ict department(subheader)
1 (emp_name)getresult
2 (emp_name)getresult
3 (emp_name)getresult
4 (emp_name)getresult
total: 4 <- wanted add in output
india(header)
engineering department(subheader)
1 (emp_name)getresult
2 (emp_name)getresult
3 (emp_name)getresult
4 (emp_name)getresult
total: 4 <- wanted add in output
what understood want calculate column, if no knidly update desired output, if yes use code count records , save result in output:
$grandttoal = 0;$tempheader='';$tempsubheader=''; foreach($querymysql $onerow){ if($tempheader == '' || $tempheader != $onerow->headercolumn) { echo $tempheader = $onerow->headercolumn."<br />"; } if($tempsubheader =='' || $tempsubheader != $onerow->subheadercolumn) { echo $tempsubheader = $onerow->subheadercolumn."<br />"; } for($i=0; $i<mysql_num_fields($onerow);$i++ ) { echo ($i+1).' '.$onerow[$i]->getresultcolumn."<br />"; } echo "total: ".$i; $grandttoal += $i; } echo "grand total: ".$grandttoal;
second option.
$grandttoal = 0;$tempheader='';$tempsubheader=''; foreach($querymysql $onerow){ if($tempheader == '' || $tempheader != $onerow->headercolumn) { echo $tempheader = '<h1>'.$onerow->headercolumn.'</h1>'."<br />"; } if($tempsubheader =='' || $tempsubheader != $onerow->subheadercolumn) { echo $tempsubheader = '</h4>'.$onerow->subheadercolumn.'</h4>'."<br />"; } for($i=0; $i<mysql_num_fields($onerow);$i++ ) { echo ($i+1).' '.$onerow[$i]->getresultcolumn."<br />"; } echo "total: ".mysql_num_fields($onerow); $grandttoal += mysql_num_fields($onerow); } echo "grand total: ".$grandttoal;
Comments
Post a Comment