I have two tables in my database:
webHistory
searchTerm | userId | date | hasLocation | historyId
LocationHistory.
userId | date | lat | lng | amount | historyId
I'm queuing each as so.
Web History:
$stmt_hour = $conn->prepare("SELECT HOUR(date) as hr, COUNT(*) as num_rows FROM
webHistory WHERE userId=? AND date BETWEEN (SYSDATE() - INTERVAL 1 DAY)
AND SYSDATE() GROUP BY HOUR(date);");
$stmt_hour->bind_param("s", $userid);
$stmt_hour->execute();
$stmt_hour->bind_result($a, $b, $c);
while($stmt_hour->fetch()) {
$hourArr[$a] = $c;
}
Location History
$stmt_hour = $conn->prepare("SELECT HOUR(date) as hr, COUNT(*) as num_rows FROM
locationHistory WHERE userId=? AND date BETWEEN (SYSDATE() - INTERVAL 1 DAY)
AND SYSDATE() GROUP BY HOUR(date);");
$stmt_hour->bind_param("s", $userid);
$stmt_hour->execute();
$stmt_hour->bind_result($a, $b, $c);
while($stmt_hour->fetch()) {
$hourLocArr[$a] = $c;
}
when a user searches with a word, it a row into webHistory, so to get all of the searches a user made in the last 24 hours I count the rows between two dates.
When a user searches by location, database is checked to see if the user has already searched in that location, if so the amount is incremented, if not the row is added with a value of one. I then get the SUM of this from the second query.
Lastly if a user searched by word & query both operations happen adding the row into webHistory and adding/incrementing in locationHistory.
I need to add the response of query 1 to the response of query two, in the format hour => amount. So:
Query 'Web History':
10 => 9
16 => 4
20 =>2
Query location History
4 => 45
10 => 2
22 => 12
Sum of query + location
4 => 45
10 => 11
16 => 4
20 => 2
22 => 12
Lastly if searched by word & query I will only need to take the values of location, I would guess I would do this through a unique Id. So far I do have a unique Id field. But as the locationHistory increments the amount field thus leading to the unique id to only be the most recent ID and redundant for this issue, I'm not sure how to handle it.
I was thinking for combining the to arrays to simply use array_marge()? But would would be the best way to go about this issue and most importantly how would I deal with the latter half of the issue.
Cheers