Reputation: 407
please let me know if you have any suggestions whatsoever as I am completely stuck on this! Really need any help. Let me know if I can make anything more clear!
My Model:
Query #1 -- Retrieves list of CampaignIds from the session
public function retrieve_campaign($CompanyId)
{
$this->db->select('*');
$this->db->from('Campaign');
$this->db->join('Product', 'Campaign.ProductId=Product.ProductId');
$this->db->where('Product.CompanyId', $CompanyId);
$query = $this->db->get();
return $query->result_array();
}
Query #2 -- Collects number of hits on a website per day
public function week_data($i, $CampaignId, $StartDate) {
$query = $this->db->query('SELECT COUNT(*) AS MyCount
FROM Record
WHERE CampaignId = ' . $CampaignId . '
AND DATE(TimeStamp) = ' . $StartDate . ' + interval ' . $i . ' day');
return $query;
}
My Controller: As you can see, I set the session data, plug it into query #1. That data is then used in a foreach loop to query #2 seven times (one for each day of the week). This data is then stored in an array $data['test']
public function index()
{
// Set Session Varaible
$CompanyId = $this->session->userdata('CompanyId');
$StartDate = "'2014-06-23'";
// Assign query result to array to be used in view
$data['campaigns'] = $this->model_record->retrieve_campaign($CompanyId);
foreach($data['campaigns'] as $item) {
for($x = 0;$x > 6;$x++) {
echo $item['CampaignId'];
$data['test'] = $this->model_record->week_data($x,$item,$StartDate);
}
}
// Load views, pass query result array
$this->load->view('templates/header');
$this->load->view('dashboard/index', $data);
$this->load->view('templates/footer');
}
My View: Here is where I am having trouble-- I would like to loop through the CampaignId's echoing them, then echo all the associated data from query #2 that relates to that CampaignId.
Example:
1) Retrieve CampaignIds, query each CampaignId 7 times for each day of the week, then print something on the view similar to this:
CampaignId: 1 Web Hits: 55,63,32,39,22,33,61
etc...
PLEASE suggestions will help, I have been working on this for 3 days now and hit a wall. Thank you SO much all!
EDIT
Thank you so much to Scrowler--helping me through every step. Working EDIT:
Controller:
public function index()
{
// Set Session Varaible
$CompanyId = $this->session->userdata('CompanyId');
$StartDate = "'2014-06-23'";
// Assign query result to array to be used in view
$data['campaigns'] = $this->model_record->retrieve_campaign($CompanyId);
foreach($data['campaigns'] as $item) {
for($x = 0; $x < 7; $x++) {
$data['CampaignData'][$item['CampaignId']][] = $this->model_record->week_data($x, $item['CampaignId'], $StartDate);
}
}
// Load views, pass query result array
$this->load->view('templates/header');
$this->load->view('dashboard/index', $data);
$this->load->view('templates/footer');
}
View:
<?php
foreach($CampaignData as $campaign_id => $week_data) {
echo 'Campaign: ' . $campaign_id . ', web hits: ' . implode(', ', $week_data) . PHP_EOL;
}
?>
Upvotes: 3
Views: 1598
Reputation: 1573
Your for loop statement looks incorrect. If you want to loop 7 times, change it to
for($i = 0; $i <= 6; $i++)
Your final code should probably look like this
foreach($data['campaigns'] as $item) {
$data[$item['CampaignId']] = array();
for($x = 0;$x <= 6;$x++) {
$data[$item['CampaignId']][] = $this->model_record->week_data($x,$item,$StartDate);
}
}
Upvotes: 0
Reputation: 24406
You can add your week data to an array during your for
loop, identified by the campaign ID as the array key, then pass that to your view:
foreach($data['campaigns'] as $item) {
for($x = 0; $x < 7; $x++) {
$data['CampaignData'][$item['CampaignId']][] = $this->model_record->week_data($x, $item, $StartDate);
}
}
Then you can output your data like this:
foreach($data['CampaignData'] as $campaign_id => $week_data) {
echo 'Campaign: ' . $campaign_id . ', web hits: ' . implode(', ', $week_data) . PHP_EOL;
}
You can of course format your output better than that, but that example should give you what you want, e.g.:
Campaign: 1, web hits: 55, 63, 32, 39, 22, 33, 61
Upvotes: 1