php - Represent Unique Items in Same Category -
i trying create page in present inventory mysql backend. here rough example of table have.
id,server_type,status 1,bl460,available 2,bl460,available 3,bl460,reserved 4,bl460,reserved 5,bl460,reserved 6,dl360,reserved 7,dl360,reserved 8,dl360,reserved 9,dl580,available 10,bl460,available`
to display these items on page, need group each item "server_type." however, grouping item "server_type" in sql query not enough unique id of each item needs preserved , represented.
i want present choices end-user on web page (using above table our data):
bl460 - amount: 3 dl360 - amount: 0 dl580 - amount: 1
note: need items 0 "available" amount display on page well.
to end-user, not matter unique id of server - if he/she chooses 2 bl460s, during checkout 2 bl460s in "available" status need switched on "reserved" status - not matter ones (i thinking of having asc order).
the issue keep having how present page in php format. need way able present each value option in page (even id, though remain hidden on page since not important end-use). tried doing array, don't know enough php use array data , present in page.
does know how accomplished?
edit: let me add more info - i've done query: select *, count(case when status = 'available' 1 else null end) amount products group id
which presents table this:
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,reserved,0 7,dl360,reserved,0 8,dl360,reserved,0 9,dl580,available,1 10,bl460,available,1
and can write array this: $amounts = array(); foreach($results $entry) {if($entry['amount'] == 1) {if(isset($amounts[$entry['server_type']])) {$amounts[$entry['server_type']]++;} else {$amounts[$entry['server_type']] = 1;}}};
which produces result this: array(2) { ["bl460"]=> int(3) ["dl580"]=> int(1) }
but still doesn't give me server_types 0 amount (though supposed define array give desired result), nor know enough php present data in html in format described above. suppose biggest issue putting array html format. know - or...does know of better solution?
build form in php simple query:
"select id, server_type, status table order server_type, id"
then, oversimplified without bothering html layout , details:
$parseditems = []; foreach ($resultset $item) { if (!array_key_exists($item['server_type'], $parseditems)) { // initialize $parseditems[$item['server_type']] = [ "ids" => [], "amount" => 0 ]; } if ($item['status'] === "available") { $parseditems[$item['server_type']]['ids'][] = $item['id']; $parseditems[$item['server_type']]['amount']++; } } foreach ($parseditems $item => $values) { if ($values['amount'] > 0) { echo "<button onclick=\"reserve('".implode(",", $values['ids'])."')\">reserve {$item}</button>"; } else { echo "<button>{$item} out of stock!</button>"; } }
you must define form , reserve() js function:
<form name="myform" id="myform" action="reserve.php" method="post"> <input type="hidden" name="reserve_ids" id="reserve_ids" value=""> </form> <script type="text/javascript"> function reserve(ids) { document.getelementbyid('reserve_ids').value = ids; document.getelementbyid('myform').submit(); } </script>
finally, reserve.php that:
$reserve_ids = explode(",", $_post['reserve_ids']); // pick first id, whatever... $id = mysqli_real_escape_string($dbh, $reserve_ids[0]);
and update table:
"update table set status='reserved' id={$id}"
please, keep in mind it's simple example illustrate possible solution, prepared statements should used, input should filtered, use csrf protection, etc...
you should think concurrency, 2 users can reserve same id approach, should check on reserve.php selected id still available , wrap whole thing in mysql transaction or similar...
hope helps
Comments
Post a Comment