<?php error_reporting(E_ERROR | E_PARSE); include('includes/config/config.php'); include('includes/functions.php'); if (!empty($_POST['item_id'])) { $item_id = $_POST['item_id']; $sql_med = "select group_concat(salt_id) as ids from tbl_items a left join item_salt b on a.item_id = b.item_id where a.item_id = '" . $item_id . "' "; error_log("query " . $sql_med); if (!$result_med = @mysqli_query($conn, $sql_med)) { error_log("error in getting medicine " . mysqli_error($conn)); die(mysqli_error($conn)); } $row_med = mysqli_fetch_assoc($result_med); $salt_id_arr = explode(",", $row_med['ids']); error_log("salt ids " . $salt_id_arr); $item_ids = []; $query = "select item_id from item_salt where salt_id in (" . implode(",", $salt_id_arr) . ") group by item_id having count(distinct salt_id) = " . count($salt_id_arr) . " and item_id != '" . $item_id . "'"; error_log("query for alternate salt med " . $query); if (!$result_salt = mysqli_query($conn, $query)) { error_log("error " . mysqli_error($conn)); } else { while ($row_salt = mysqli_fetch_assoc($result_salt)) { $stock = "select sum(stock_qty) as qty from item_stock where item_id = '" . $row_salt['item_id'] . "' and stock_qty > 0 and ohc_type_id = '" . $_SESSION['current_ohcttype'] . "' and expiry_date > curdate()"; error_log("query for stock qty med " . $stock); if (!$result_stock = mysqli_query($conn, $stock)) { error_log("error in stock " . $stock); } else { $row_stock = mysqli_fetch_assoc($result_stock); $item_id = $row_salt['item_id']; $item_name = getFieldFromTable('item_name', 'tbl_items', 'item_id', $item_id); $is_prescribed = getFieldFromTable('is_prescription', 'tbl_items', 'item_id', $item_id); if ($row_stock['qty'] == null) { $item_ids[] = array( 'item_name' => $item_name, 'item_id' => $item_id, 'is_prescribed' => $is_prescribed, 'qty' => 0 ); } else { $item_ids[] = array( 'item_name' => $item_name, 'item_id' => $item_id, 'is_prescribed' => $is_prescribed, 'qty' => $row_stock['qty'] ); } } } } error_log("data " . print_r($item_ids, true)); } echo json_encode($item_ids);