I am trying to add custom fields to my presentation platform and have the following MySQL/PDO query:
SELECT presenters.presenter_name,
presenters.presenter_email,
presenters.presenter_contact,
presentations.presentation_uid,
presentations.presentation_presenter_notes,
presentations.presentation_date,
presentations.presentation_customer_reference,
presentations.presentation_customer_name,
presentations.presentation_customer_email,
customfields.customfield_name,
customfields_data.customfield_data_value
FROM presentations
INNER JOIN presenters ON presentations.presentation_presented_by = presenters.presenter_id
LEFT JOIN customfields ON customfields.customfield_presentation_uid = presentations.presentation_uid
LEFT JOIN customfields_data ON customfields_data.customfield_data_id = customfields.customfield_id
WHERE presentations.presentation_uid = :presentation_id
I execute the query with $presentation = $query->fetchAll(PDO::FETCH_ASSOC);
and then print_r
the results and get the following:
Array (
[0] => Array
(
[presenter_name] => John Doe
[presenter_email] => [email protected]
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => [email protected]
[customfield_name] => Favourite Colour
[customfield_data_value] => Blue
)
[1] => Array
(
[presenter_name] => John Doe
[presenter_email] => [email protected]
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => [email protected]
[customfield_name] => Age
[customfield_data_value] => 26
)
)
What I am trying to achieve is this so that I can iterate through the custom fields and add them in to my view template:
Array (
[presenter_name] => John Doe
[presenter_email] => [email protected]
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => [email protected]
[customfield_name] => Favourite Colour
[customfield_data_value] => Blue
[customfield_name] => Age
[customfield_data_value] => 26
)
or better still:
Array (
[presenter_name] => John Doe
[presenter_email] => [email protected]
[presenter_contact] => 0123456789
[presentation_uid] => esljqpmdh
[presentation_presenter_notes] => Great presentation
[presentation_date] => 2015-06-05 14:17:15
[presentation_customer_reference] => How to make a great presentation.
[presentation_customer_name] => Doe John
[presentation_customer_email] => [email protected]
[customfields] => Array (
[customfield_name] => Favourite Colour
[customfield_data_value] => Blue
[customfield_name] => Age
[customfield_data_value] => 26
)
)
But I'm not sure what to do next, I'm completely stuck and not sure which part of my code is incorrect. The query or the fetchAll.
Any help would be greatly appreciated.
Are you ok with associative arrays? Then create a new array (say
$def_presentation
or something ) and go for something like