2017-08-25 8 views
-1

を使用して、データベースからmorris.jsチャートを取り込むPHP変数を動的に生成します。コードギターのMVC最新リリースを使用して、データベースからレポートを生成しています。 CMSに入り、手動でこの国を追加します。 Imは私のテーブルから変数を作成し、私の出力形式、イムは、現在、そのような変数の配列を生成し、それらを送信することにより、これを動的にしようとしている:codeigniter MVC

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) 

$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); 

これは私に、以下の配列を与えます

[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; 
+0

これらの変数のポイントは正確に何ですか?それらは完全に余分であり、私が見る限り、1つの配列に置き換えることができます。 – deceze

+0

彼らは私の国のテーブルのエントリの量から生成されるので、私たちが別の国に拡張すると(すぐに拡張を計画して現在12であったようにすぐにexppandingだった)、私は自分のコードに入り、一度私はこのコードを他の関数(テスト、テスト名など)で再利用されます – Sam

+0

あなたはあなたが本当に欲しいものは、配列です可変変数を必要と思う!比較する: '$ {" var $ i "} ++'と '$ countries [$ i] ++'。 – deceze

答えて

0

は私の解決策であり、Iは指標として国名とアレイを使用し、ここで0の割り当てられた初期値は私のコントローラからのコードである。

enter image description here

:ここ
/* 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 

チャート& JSONデータの印字出力のSSであります3210

貢献したすべての人に大きな感謝