I have set up the following query based on this question:
"(SELECT users_id, first_name, last_name, 'msg' as type FROM users
WHERE users_id LIKE '%" . $searchQuery ."%'
OR first_name LIKE '%" . $searchQuery ."%'
OR last_name LIKE '%" . $searchQuery ."%')
UNION
(SELECT title, vimeo_id, upload_date, 'topic' as type FROM video
WHERE title LIKE '%" . $searchQuery ."%'
OR vimeo_id LIKE '%" . $searchQuery ."%')"
After the query is executed it should spit out the results like this:
foreach($row as $field) {
$details[] = array(
// Fields uit de users table
'user_id' => $field['users_id'],
'first_name' => $field['first_name'],
'last_name' => $field['last_name'],
//Fields uit de video table
'vimeo_id' => $field['vimeo_id'],
'video_title' => $field['title'],
'upload_date' => $field['upload_date']
);
}
It works but I still encounter a problem. When I run the script with a searchterm that should be within the 'user' table there is no problem. When I run the script with a searchterm that should be within the 'video' table I still get results but it moves the results to the first part of the loop. That means I get the following output:
[{"user_id":"Test","first_name":"151125560","last_name":"2016-01-08 10:49:32","vimeo_id":null,"video_title":null,"upload_date":null}]
While the video id should be 151125560
, the video title should be test
and the upload_date should be 2016-01-08
.
I tried to solve this by adding as type
but I have no clue on how to proceed from here. Could somebody please help me by moving the results to the right field?