PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create table dishes (dish_id int, userId int, name text); insert into dishes values (1, 10, 'Super dish'); insert into dishes values (2, 10, 'Super dish'); create table dish_images (id int, dish_id int, image text); insert into dish_images values (1, 1, 'Super dish pict 1'), (2, 1, 'Super dish pict 2'); insert into dish_images values (3, 2, 'Super dish pict 3'), (4, 2, 'Super dish pict 4');
Copy Clear
Copy Format Clear
<?php $db = &$pdo; $userId = 10; //////////////// $sth = $db->prepare( "SELECT dishes.dish_id, dishes.name, dishes.date_create, json_arrayagg(image) images FROM `dishes` INNER JOIN `dish_images` ON dishes.dish_id = dish_images.dish_id WHERE `userId` = ? GROUP BY dishes.dish_id, dishes.name" ); $sth->execute([$userId]); $dishes = $sth->fetchAll(PDO::FETCH_ASSOC); foreach ($dishes as $key => $value) { $pics = $value["images"]; $pics = substr($pics, 1, -1); $pics = str_replace('"', "", $pics); $pics = str_replace(', ', ",", $pics); $pics_arr = explode(',', $pics); $dishes[$key]["images"] = $pics_arr; } $response = [ 'status' => true, 'message' => 'return user dishes', 'result' => $dishes, ]; echo json_encode(['data' => $response]); //return http_response_code(400);
Show:  
Copy Clear