php - Appending SQL to existing associative array -
learning php , creating associative arrays.
trying create associative array describes activity. activity example 354 , 355.
this current code:
$query = $db->prepare('select item_id kh_program_item_info '. $where); $query->execute(); if($query->rowcount() > 0){ while($john = $query->fetch(pdo::fetch_obj)){ $key1 = $john->item_id; $report-> $key1= array(); } foreach ($report $key=>$johnval) { $where = 'item_id = ' . $key; $query = $db->prepare('select activity_desc, date_format(activity_date,"%d/%m/%y") activitydate kh_program_items '. $where); $query->execute(); $results = $query->fetch(pdo::fetch_obj); $report->$key = $results; echo '<pre>'; echo print_r($report); echo '</pre>'; } } else { echo '<p>there no activities</p>'; }
this yield results.
stdclass object ( [354] => stdclass object ( [activity_desc] => <p>send activity</p> [activitydate] => 08/11/2016 ) [355] => stdclass object ( [activity_desc] => <p>send activity 2</p> [activitydate] => 11/11/2016 ) )
i trying add more information 354 , 355.
have revisited code , here new code:
$query = $db->prepare('select item_id kh_program_item_info '. $where); $query->execute(); if($query->rowcount() > 0){ while($john = $query->fetch(pdo::fetch_obj)){ $key1 = $john->item_id; $report-> $key1= array(); } foreach ($report $key=>$johnval) { $where = 'item_id = ' . $key; $query = $db->prepare('select activity_desc, date_format(activity_date,"%d/%m/%y") activitydate kh_program_items '. $where); $query->execute(); $results = $query->fetch(pdo::fetch_obj); $report->$key = $results; $query = $db->prepare('select info_value kh_program_item_info '. $where .' , info_type="program_category"'); $query->execute(); $info_value = $query->fetch(pdo::fetch_obj); $where = 'category_id = ' . $info_value->info_value; $query = $db->prepare('select category_name kh_program_categories '. $where); $query->execute(); $actcategories = $query->fetch(pdo::fetch_obj); $report->$key = $actcategories; echo '<pre>'; echo print_r($report); echo '</pre>'; } } else { echo '<p>there no activities</p>'; }
this overwrite have created , yield following results:
stdclass object ( [354] => stdclass object ( [category_name] => parent input ) [355] => stdclass object ( [category_name] => children's menu ) )
the problem $myactivity->$key = $actcategories;
.
how append end without overwriting information?
i need yield result
stdclass object ( [354] => stdclass object ( [activity_desc] => <p>send activity</p> [activitydate] => 08/11/2016 [category_name] => parent input ) [355] => stdclass object ( [activity_desc] => <p>send activity 2</p> [activitydate] => 11/11/2016 [category_name] => children's menu ) )
thanks in advance.
you'll need build array first , then, send object.
i.e. :
foreach ($report $key=>$johnval) { $where = 'item_id = ' . $key; $query = $db->prepare('select activity_desc, date_format(activity_date,"%d/%m/%y") activitydate kh_program_items '. $where); $query->execute(); // build first array $result_first_query = $query->fetch(pdo::fetch_obj); $query = $db->prepare('select info_value kh_program_item_info '. $where .' , info_type="program_category"'); $query->execute(); $info_value = $query->fetch(pdo::fetch_obj); $where = 'category_id = ' . $info_value->info_value; $query = $db->prepare('select category_name kh_program_categories '. $where); $query->execute(); // build second array $result_second_query = $query->fetch(pdo::fetch_obj); // put them in $report->$key $report->$key = $result_first_query + $result_second_query; echo '<pre>'; print_r($report); echo '</pre>'; }
hope helps.
Comments
Post a Comment