So, I have this scenario: I need to query some activities with version numbers. Some of those have only one version, others have multiple. They all come from a form, which already uses jQuery to add new form elements and retrieve their values. PHP Ajax is already working.
I thought about building a the query around a $case
php variable, kinda like:
$query = "SELECT DISTINCT activity_number, version_number, person_id, conclusion_date
FROM table1
WHERE fixed_parameter
$case
";
At first I was struggling with "I need to make the case variable as =
when there's a single version being queried, but I need IN
when there's multiple. Afterwards, it hit me that I can use IN
for both.
But now I don't know if I should try to treat the multiple version cases on the javascript side or the PHP side, and which would be easier/better.
Currently, I'm sending everything, so I get something like this as form data being passed to PHP:
ativities: [{"activity_number":"12044","version_number":"4"},{"activity_number":"12044","version_number":"7"},{"activity_number":"41796","version_number":"0"}]
If this gets sent to PHP, I'll end up with:
AND CASE activity_number
WHEN 12044 THEN version_number IN (4)
WHEN 12044 THEN version_number IN (7)
WHEN 41796 THEN version_number IN (0)
END
and that obviously won't work. So I need to merge them by activity_number, to get something like {"activity_number":"12044","version_number":"4,7"}
. I've dug around, found a couple weird scripts but they were too custom for their answers to easily fit in here.
The furthest I think I've got was using this answer Javascript - Reduce array with multiple-key objects but I don't actually understood it's logic, so I'm having a real hard time adapting it to my needs.