mysql - PHP - Combining Multiple Items Into One -
i attempting create end-user page present different servers available check-out. server @ given time can have either "available" status or "reserved" status. i'm using mysql backend. how doing query:
select *, count(case when status = 'available' 1 else null end) amount products group id
this result get:
id,server_type,status,amount 1,bl460,available,1 2,bl460,available,1 3,bl460,reserved,0 4,bl460,reserved,0 5,bl460,reserved,0 6,dl360,available,1 7,dl360,reserved,0 8,dl360,reserved,0
where reserved
equal 0, , available
equal 1. want end-user able checkout server in available
status.
to question: want in page present list of servers on page in way, available
equal amount:
bl460 - amount: 2 dl360 - amount: 1
how can achieve format in php?
well, easier if directly in sql query:
select server_type, count(*) count products status = 'available' group server_type
this should give table want.
if want in php, easiest solution loop through sql result , count number of available servers per server_type
in associative array server_type
array key:
$amounts = array(); foreach($sql_result $entry) { if($entry['amount'] == 1) { if(isset($amounts[$entry['server_type']])) { $amounts[$entry['server_type']]++; } else { $amounts[$entry['server_type']] = 1; } } } echo $amounts;
edit: in order print values described in question, use following code snippet:
foreach($amounts $name=>$amount) { echo $name + " - amount: " + $amount + "<br>"; }
Comments
Post a Comment