To preface, part of what i am attempting to do is separate as much code-behind as possible from front end code (major php functions are not in the middle of html to make it more readable). What i am working on is an 'editor' style page for different 'codes' used on the site, so to get them ready to display on the page, I am creating an array to display them all, which appears as follows:
$CodeListing = array(
array(
'CodeType' => 1,
'CodeDesc' => 'Product',
'CodeShrt' => 'Prod',
'ResultsView' => Array(),
),
array(
'CodeType' => 4,
'CodeDesc' => 'Version',
'CodeShrt' => 'Vers',
'ResultsView' => Array(),
),
array(
'CodeType' => 5,
'CodeDesc' => 'Applications',
'CodeShrt' => 'Apps',
'ResultsView' => Array(),
),
array(
'CodeType' => 7,
'CodeDesc' => 'Resellers',
'CodeShrt' => 'Resell',
'ResultsView' => Array(),
),
array(
'CodeType' => 8,
'CodeDesc' => 'Roles',
'CodeShrt' => 'Roles',
'ResultsView' => Array(),
)
);
All these codes are stored in a single MySQL table, and are uniquely identified by a Primary Key consisting of the CodeType & CodeValue columns. What i am attempting to do is fill in the value of each ResultsView with the associated data from the MySQL table using a foreach loop.
So i have taken a look at a couple of other questions here on Stack: Inserting a multidimensional array into another multidimensional array
Insert array into multidimensional array
I have tried using array_push and array_merge as suggested in them, but neither of those methods worked. when I do a print_r outside the foreach, or run a foreach on the ResultsView on the html side, it returns nothing (basically looks like above without anything defined for a value for any ResultsView keys.)
Currently, my code is as follows:
//Go through each CodeListing and set its ResultView to contain all code entries.
foreach ($CodeListing as $type) {
//Generate the query
$query = "SELECT * FROM code WHERE CodeType = '".$type['CodeType']."'";
//Define order based on CodeType.
//Certain CodeTypes require a unique sorting.
if (in_array($type['CodeType'], array('1','6','7','8'))) {
$query .= " ORDER BY CodeName;";
} else {
$query .= " ORDER BY CodeField1, CodeValue;";
}
//Get the results of query
$result = $db -> select($query);
//set the value of ResultsView.
$type['ResultsView'] = $result;
}
/*
Query results from $db -> select($query) come back in the following format:
[0] => array(
'CodeType' => SomeValue,
'CodeValue' => SomeValue,
'CodeName' => SomeValue,
'CodeField1' => SomeValue,
'CodeField2' => SomeValue,
)
[1] => array(
'CodeType' => SomeValue,
'CodeValue' => SomeValue1,
'CodeName' => SomeValue1,
'CodeField1' => SomeValue1,
'CodeField2' => SomeValue1,
)
...
etc.
*/
Like mentioned above, I am attempting to input the MySQL query results array into the value of a key in a multidimensional array, without any luck. Also, in its current state, my code throws no errors either.
If i check the value of the ResultsView key inside the foreach loop where I set the value, the array shows up when I do a print_r (also telling me that it is reaching the foreach loop in my code). outside that loop, my code acts like I never assigned a value at all to any of the ResultsView keys, and I don't understand why, or what I am missing so the value stays assigned.