1

I want to insert some data into sub in json data with my data in database

here is my code:

$sql = "SELECT * FROM pharma_data";
$result = mysqli_query($conn, $sql);


$data = array();
while ($row = mysqli_fetch_assoc($result)){
    $row_array['id'] = $row['phar_id'];
    $row_array['name'] = $row['name'];
    $row_array['batch'] = $row['batch_no'];
    $row_array['category'] = $row['category'];
    $row_array['measurement'] = $row['measurement'];
    $row_array['stock'] = $row['stock'];
    $row_array['price'] = $row['price'];
    $row_array['date'] = $row['date_added'];
    $row_array['expiration'] = $row['expiration_date'];
    $row_array['type'] = $row['medicin_type'];
    $row_array['supplier'] = $row['supplier'];
    array_push($data,$row_array);
}

$convert = json_encode($data);
echo $convert;

but the output of my code is:

[{"id":"8","name":"Test Product Name Pharmacy","batch":"Test Product Name Pharmacy","category":"Test Category Pharmacy","measurement":"1mg","stock":"2","price":"15","date":"February 18, 2021, 11:36 am","expiration":"2021-02-18","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"},{"id":"9","name":"Alaxan","batch":"Test user medicine Supplies","category":"Test Category Pharmacy","measurement":"1mg","stock":"66","price":"63.43","date":"February 28, 2021, 4:45 pm","expiration":"2021-02-28","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"},{"id":"10","name":"Medicol","batch":"987","category":"Test Category Pharmacy","measurement":"1mg","stock":"15","price":"123.23","date":"February 28, 2021, 5:42 pm","expiration":"2021-02-28","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"},{"id":"11","name":"test","batch":"test","category":"Test Category Pharmacy","measurement":"213","stock":"32","price":"123.78","date":"February 28, 2021, 6:43 pm","expiration":"2021-02-28","type":"Test Medicine Type Pharmacy","supplier":"Nicole Gonzaga"}]

Want output like this:

{
  "total": 800,
  "totalNotFiltered": 800,
  "rows": [
    {
      "id": 0,
      "name": "Item 0",
      "price": "$0"
    },
    {
      "id": 1,
      "name": "Item 1",
      "price": "$1"
    }
  ]
}

enter image description here

2
  • what is the logic to calculate "total": 800, "totalNotFiltered": 800,? Commented Feb 28, 2021 at 13:28
  • Add echo "<pre>" right before echo $convert and it should beautify the code as you want. Commented Feb 28, 2021 at 13:41

2 Answers 2

1

You need to filter out $data before sending it to the response so it means we need a function myFilter to filter the $data elements and we can filter all the elements in the array using array_map(). The total and totalFiltered column seems to give the total number of rows in $data hence the count(). Try the code below:

function myFilter($a) {
    $row['id'] = $a['id'];
    $row['name'] = $a['name'];
    $row['price'] = $a['price'];
   return $row;
};

$filterData = array(
    'total' => count($data),
    'totalNotFiltered' => count($data),
    'rows' => array_map("myFilter", $data)
);

$convert = json_encode($filterData);
echo $convert;

Demo code would be here: https://ideone.com/pZCUbG

Sign up to request clarification or add additional context in comments.

Comments

0

You need to convert the returned data. Something like:

$data = array();
while ($row = mysqli_fetch_assoc($result)){
    $row_array['id'] = $row['phar_id'];
    $row_array['name'] = $row['name'];
    // $row_array['batch'] = $row['batch_no'];
    // $row_array['category'] = $row['category'];
    // $row_array['measurement'] = $row['measurement'];
    // $row_array['stock'] = $row['stock'];
    $row_array['price'] = $row['price'];
    // $row_array['date'] = $row['date_added'];
    // $row_array['expiration'] = $row['expiration_date'];
    // $row_array['type'] = $row['medicin_type'];
    // $row_array['supplier'] = $row['supplier'];
    $data[] = $row_array; // Same as array_push($data,$row_array);
}

$final_data = array(
    'total' => count($data),
    'totalNotFiltered' => '',
    'rows' => $data
);

$convert = json_encode($final_data);
echo $convert;

Commented out the data that isn't needed in this context (maybe you need it to use elsewhere, not sure. If so just uncomment back)

Not sure with totalNotFiltered as I don't know how you are calculating this. Maybe you can work with that and this gives you an idea to work with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.