【问题标题】:Dynamically generate PHP variables to populate morris.js charts from databases using codeigniter MVC使用 codeigniter MVC 动态生成 PHP 变量以从数据库填充 morris.js 图表
【发布时间】:2018-02-03 11:52:53
【问题描述】:

我正在使用最新版本的 codeigniter MVC 并从我的数据库中生成一些报告,我的问题是,如果其中一位管理员添加了一个新国家/地区,我必须进入 CMS 并手动添加该国家/地区。我试图通过从我的表中创建变量然后将它们发送到我的输出表单来使这个动态化,我目前正在生成一个变量数组,如下所示:

$vars = array();
$misc = 0;
$count = count($country_names);
for ($i = 0; $i <= $count; $i++) {
    ${"var$i"} = 0;
    array_push($vars, ${"var$i"});
}
array_push($vars, $misc);

这给了我以下数组:

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 )

[13]$misc 变量, 我现在需要遍历不同的表并匹配国家/地区,以计算该县有多少人使用了我们的服务,我尝试了这个并没有高兴,因为相关的变量计数没有增加:

$misc = 0;
$count = count($country_names);
for ($i = 0; $i <= $count; $i++) {
    ${"var$i"} = 0;
    array_push($vars, ${"var$i"});
    foreach ($data as $item) {
        if ($item['country'] === "UK") {
            ${"var$i"}++;
        } else {
            $misc++;
        }
        array_push($vars, ${"var$i"});
    }
}
array_push($vars, $misc);

这段代码仍然给我相同的数组输出,有什么想法吗?我可以在for 循环中使用foreach 循环吗? $data 是我的Search_model 发送的一个数组:

$result->select('country')
    ->from('results')
    ->like('country', $countries)
    ->order_by('country', 'ASC');
$results = $result->get()->result_array();
return $results;

【问题讨论】:

  • 这些变量变量的意义何在?据我所知,它们完全是多余的和/或可以被一个数组替换。
  • 它们是根据我的国家/地区表中的条目数量生成的,因此,如果我们扩展到另一个国家/地区(像目前在 12 中一样迅速扩展,并计划进一步扩展)并且我不想继续进入我的代码并修改这些,一旦我破解这个代码将被重新用于其他功能(测试,测试名称等)
  • 每当您认为需要可变变量时,您真正想要的是数组!比较:${"var$i"}++$countries[$i]++
  • 嗨,刚刚试过$countries[$i]++;,但变量计数都没有增加,第一个索引[0]应该变成31,其他[13]应该是3,因为在我的测试数据库中有31名患者来自英国和 3 个来自其他国家。 $vars 是一个变量数组
  • 忘记回复对不起,你们都对,答案是从我的数据库中的表填充的数组,在这种情况下,“国家”我将国家名称作为数组索引并分配了一个首字母每个实例的值为 0,然后使用 foreach 循环遍历不同的表并将匹配索引增加 1。谢谢。当我有机会时,我会用代码提供答案。山姆

标签: php mysql arrays codeigniter for-loop


【解决方案1】:

这是我的解决方案,我使用了一个以国家名称作为索引的数组,并分配了一个初始值 0。这是来自我的控制器的代码:

/* Create a string based on the countries array */
$new_country_names = array(); // An array of countries with numbers as indexes, EG: [0]=>Botswana etc
foreach ($country_names as $country) {
    $new_country_names[] = $country['name'];
}

/* Create a dynamic array of variables from the countries table, using the country name as the index EG: [UK]=>0 etc: */
$country_names_array = array(); // An array with country names as the index and 0 assigned to each one EG: [Botswana]=>0
foreach ($country_names as $country){
    $country_names_array[$country['name']] = 0;
}

/* Loop through each instance of the results table and create a count for all matching instances */
$misc = 0;
$countries_count = count($new_country_names);
$country_count = 0;
while($country_count < $countries_count) {
    foreach ($data as $item) {
        if ($item['country'] === $new_country_names[$country_count]) {
                                $country_names_array[$new_country_names[$country_count]] = $country_names_array[$new_country_names[$country_count]] + 1;
    } else {
            $misc++;
        }
    }
    $country_count++;
    }
$country_names_array = str_replace('_', ' ', $country_names_array); // Lose the underscores
$country_names_array = array_filter($country_names_array); // Filter out NULL values
$data = json_encode($country_names_array); // JSON encode the array to populate charts in the view

这是输出的 SS,带有图表和 JSON 数据的打印:

非常感谢所有做出贡献的人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2020-04-13
    • 1970-01-01
    • 2014-01-10
    • 2011-02-23
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多