如果您有任何建议,请告诉我,因为我完全坚持这个!真的需要任何帮助。如果我能说得更清楚,请告诉我!
我的模特:
查询#1 - 从会话中检索CampaignIds列表
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();
}
查询#2 - 每天收集网站点击次数
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;
}
我的控制器: 如您所见,我设置会话数据,将其插入查询#1。 然后在foreach循环中使用该数据七次查询#2(一周中的每一天一次)。然后将这些数据存储在数组$ 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');
}
我的观点:这是我遇到麻烦的地方 - 我想循环播放CampaignId,然后回复来自查询#2的与该CampaignId相关的所有相关数据。
示例:
1)检索CampaignIds,为每周的每一天查询每个CampaignId 7次,然后在视图上打印类似于此的内容:
CampaignId:1 网页点击次数:54,63,32,39,22,33,61
等...
请提出建议会有所帮助,我已经为此做了3天的工作并且碰壁了。 非常感谢你!
修改
非常感谢Scrowler - 帮助我完成每一步。工作编辑:
控制器:
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');
}
查看:
<?php
foreach($CampaignData as $campaign_id => $week_data) {
echo 'Campaign: ' . $campaign_id . ', web hits: ' . implode(', ', $week_data) . PHP_EOL;
}
?>
答案 0 :(得分:1)
您可以在for
循环期间将星期数据添加到数组中,并将广告系列ID标识为数组键,然后将其传递给您的视图:
foreach($data['campaigns'] as $item) {
for($x = 0; $x < 7; $x++) {
$data['CampaignData'][$item['CampaignId']][] = $this->model_record->week_data($x, $item, $StartDate);
}
}
然后您可以输出这样的数据:
foreach($data['CampaignData'] as $campaign_id => $week_data) {
echo 'Campaign: ' . $campaign_id . ', web hits: ' . implode(', ', $week_data) . PHP_EOL;
}
你当然可以更好地格式化你的输出,但是那个例子可以给你你想要的东西,例如:
广告系列:1,网络点击次数:55,63,32,39,22,33,61
答案 1 :(得分:0)
您的for循环语句看起来不正确。如果要循环7次,请将其更改为
for($i = 0; $i <= 6; $i++)
你的最终代码可能看起来像这样
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);
}
}