Making a dynamic valuation with foreach php - php

I want to make a dynamic valuation. So the idea is like this :
In my database I've user table contains 'score'.
And I've category table contains fields 'minScore' and 'categoryName(bad, good, awesome)'
And In the end I want to get result like this categoryName=>howMuchUserBasedOnScore.
I'm thinking like this :
function valuation(){
$allScore = $this->db->select('score')->get('user')->result();
$category = $this->db->get('categori')->result();
$arrCategori = array();
foreach ($allScore as $value) {
foreach($categori as $row){
if($value->score <= $row->minScore){
array_push($arrCategori, )
//I don't know what should I do
}
}
}
}
can anyone help me?
edit :
Now I add maxScore into my category table field.
and I changed the if statement. it become like this
if($value->score <= $row->minScore && $value->score >= $row->maxScore){
array_push($arrCategori, $arrCategori[$row->Categori] = $value->bsc );
}
I var_dump it. and the result is what I expected. the problem now is that, how can I count how much people that are awesome, bad or good
edit :
here's the var_dump from $allscore (I change bsc into score for the sake simplicity):
array(11) { [0]=> object(stdClass)#42 (1) { ["bsc"]=> string(6) "100.00" } [1]=> object(stdClass)#43 (1) { ["bsc"]=> string(5) "40.00" } [2]=> object(stdClass)#44 (1) { ["bsc"]=> string(6) "100.00" } [3]=> object(stdClass)#45 (1) { ["bsc"]=> string(6) "100.00" } [4]=> object(stdClass)#46 (1) { ["bsc"]=> string(5) "40.00" } [5]=> object(stdClass)#47 (1) { ["bsc"]=> string(5) "40.00" } [6]=> object(stdClass)#48 (1) { ["bsc"]=> string(5) "40.00" } [7]=> object(stdClass)#49 (1) { ["bsc"]=> string(5) "40.00" } [8]=> object(stdClass)#50 (1) { ["bsc"]=> string(5) "40.00" } [9]=> object(stdClass)#51 (1) { ["bsc"]=> string(5) "60.00" } [10]=> object(stdClass)#52 (1) { ["bsc"]=> string(4) "0.00" } }
var_dump from $category (bxnak = maxScore, bxnaw = minScore)
array(9) { [0]=> object(stdClass)#53 (4) { ["id"]=> string(1) "1" ["bxnaw"]=> string(1) "0" ["bxnak"]=> string(2) "25" ["kategori"]=> string(5) "GAGAL" } [1]=> object(stdClass)#54 (4) { ["id"]=> string(1) "2" ["bxnaw"]=> string(2) "26" ["bxnak"]=> string(2) "40" ["kategori"]=> string(16) "DALAM PENGAWASAN" } [2]=> object(stdClass)#55 (4) { ["id"]=> string(1) "3" ["bxnaw"]=> string(2) "41" ["bxnak"]=> string(2) "50" ["kategori"]=> string(18) "PERLU PENGEMBANGAN" } [3]=> object(stdClass)#56 (4) { ["id"]=> string(1) "4" ["bxnaw"]=> string(2) "51" ["bxnak"]=> string(2) "60" ["kategori"]=> string(5) "BURUK" } [4]=> object(stdClass)#57 (4) { ["id"]=> string(1) "5" ["bxnaw"]=> string(2) "61" ["bxnak"]=> string(2) "75" ["kategori"]=> string(11) "KURANG BAIK" } [5]=> object(stdClass)#58 (4) { ["id"]=> string(1) "6" ["bxnaw"]=> string(2) "76" ["bxnak"]=> string(2) "80" ["kategori"]=> string(10) "CUKUP BAIK" } [6]=> object(stdClass)#59 (4) { ["id"]=> string(1) "7" ["bxnaw"]=> string(2) "81" ["bxnak"]=> string(2) "90" ["kategori"]=> string(4) "BAIK" } [7]=> object(stdClass)#60 (4) { ["id"]=> string(1) "8" ["bxnaw"]=> string(2) "91" ["bxnak"]=> string(2) "99" ["kategori"]=> string(11) "SANGAT BAIK" } [8]=> object(stdClass)#61 (4) { ["id"]=> string(1) "9" ["bxnaw"]=> string(3) "100" ["bxnak"]=> string(4) "1000" ["kategori"]=> string(8) "ISTIMEWA" } }

I finally did it!!
so here's the final code of my function
function valuation(){
$allBsc = $this->db->select('bsc')->get('kpi')->result();
$kategori = $this->db->get('kategori')->result();
$arrKategori = [];
foreach ($allBsc as $value) {
foreach($kategori as $row){
if($value->bsc <= $row->bxnak && $value->bsc >= $row->bxnaw){
if(array_key_exists($row->kategori, $arrKategori)){
array_push($arrKategori[$row->kategori], $value->bsc);
}else{
array_push($arrKategori, $arrKategori[$row->kategori] = array($value->bsc));
}
}
}
}
var_dump(count($arrKategori));
die();
}
thank you, for helping me!
$allBsc = $score
$kategori = $category
$arrKategori = $allCategory

Related

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

Codeigniter 2.x, ActiveRecord, var_dump(), what is the expected result?

I am trying to debug some problems I am having with viewing data from a database. I am using codeigniter 2.x and active records. Here is my code:
$data = $this->db->select('country_id', 'country')->from('mhcountry')->get()->result();
var_dump($data);
die;
Var_dump returns the following:
array(20) { [0]=> object(stdClass)#21 (1) { ["country_id"]=> string(1) "1" } [1]=> object(stdClass)#22 (1) { ["country_id"]=> string(1) "2" } [2]=> object(stdClass)#23 (1) { ["country_id"]=> string(1) "3" } [3]=> object(stdClass)#24 (1) { ["country_id"]=> string(1) "4" } [4]=> object(stdClass)#25 (1) { ["country_id"]=> string(1) "5" } [5]=> object(stdClass)#26 (1) { ["country_id"]=> string(1) "6" } [6]=> object(stdClass)#27 (1) { ["country_id"]=> string(1) "7" } [7]=> object(stdClass)#28 (1) { ["country_id"]=> string(1) "8" } [8]=> object(stdClass)#29 (1) { ["country_id"]=> string(1) "9" } [9]=> object(stdClass)#30 (1) { ["country_id"]=> string(2) "10" } [10]=> object(stdClass)#31 (1) { ["country_id"]=> string(2) "11" } [11]=> object(stdClass)#32 (1) { ["country_id"]=> string(2) "12" } [12]=> object(stdClass)#33 (1) { ["country_id"]=> string(2) "13" } [13]=> object(stdClass)#34 (1) { ["country_id"]=> string(2) "14" } [14]=> object(stdClass)#35 (1) { ["country_id"]=> string(2) "15" } [15]=> object(stdClass)#36 (1) { ["country_id"]=> string(2) "16" } [16]=> object(stdClass)#37 (1) { ["country_id"]=> string(2) "17" } [17]=> object(stdClass)#38 (1) { ["country_id"]=> string(2) "18" } [18]=> object(stdClass)#39 (1) { ["country_id"]=> string(2) "19" } [19]=> object(stdClass)#40 (1) { ["country_id"]=> string(2) "20" } }
I have 20 countries entered from 1 to 20 BUT I do not see my country names in the var_dump. It is just dumping the first field. Is this normal or expected?
I tried the same thing on another table and it also just returns the first field.
You should pay attention to the documentation here, meanwhile you can try this :
$this->db->select(array('country_id', 'country'))
// OR
$this->db->select('country_id, country')
$data = $this->db->select('country_id', 'country')->from('mhcountry')->get()->result();
This is wrong. it will get only country_id.
Use this
$data = $this->db->select('country_id , country')->from('mhcountry')->get()->result();

Codeigniter 2.x, Database, Printing contents of results

I have a controller as follows:
$data['query'] = $this->model_admin->search_countries($config['per_page'] , $offset, $sort_by, $sort_order);
$data['templateVersion'] = 'template1';
$data['headerVersion'] = 'header1';
$data['navBarVersion'] = 'navbar1';
$data['main_content'] = 'detail-wrap/admin/country_index'; $this->load->view('detail-wrap/includes/template1', $data);
The search_countries method is in the controller and as follows:
return $this->db->select('country_id, country')->from('mhcountry')->get()->result();
My view is as follows:
<tbody>
<?php
var_dump($query);
foreach ($query['rows'] as $row ) {
?>
<tr>
<td><?php echo $row->country_id; ?></td>
<td><?php echo $row['country']; ?></td>
<td>something</td>
<td>someone</td>
</tr>
<?php } ?>
</tbody>
I cannot get country_id and country to display. I have tried many techniques but no go. The content of var_dump($query); is correct and shows:
Found Countries array(20) { [0]=> object(stdClass)#21 (2) { ["country_id"]=> string(1) "1" ["country"]=> string(6) "Canada" } [1]=> object(stdClass)#22 (2) { ["country_id"]=> string(1) "2" ["country"]=> string(5) "Japan" } [2]=> object(stdClass)#23 (2) { ["country_id"]=> string(1) "3" ["country"]=> string(5) "Korea" } [3]=> object(stdClass)#24 (2) { ["country_id"]=> string(1) "4" ["country"]=> string(1) "0" } [4]=> object(stdClass)#25 (2) { ["country_id"]=> string(1) "5" ["country"]=> string(6) "pakist" } [5]=> object(stdClass)#26 (2) { ["country_id"]=> string(1) "6" ["country"]=> string(6) "ddsdsd" } [6]=> object(stdClass)#27 (2) { ["country_id"]=> string(1) "7" ["country"]=> string(5) "texas" } [7]=> object(stdClass)#28 (2) { ["country_id"]=> string(1) "8" ["country"]=> string(8) "scotland" } [8]=> object(stdClass)#29 (2) { ["country_id"]=> string(1) "9" ["country"]=> string(8) "scotland" } [9]=> object(stdClass)#30 (2) { ["country_id"]=> string(2) "10" ["country"]=> string(8) "scotland" } [10]=> object(stdClass)#31 (2) { ["country_id"]=> string(2) "11" ["country"]=> string(8) "texassss" } [11]=> object(stdClass)#32 (2) { ["country_id"]=> string(2) "12" ["country"]=> string(14) "texassdsdsdsds" } [12]=> object(stdClass)#33 (2) { ["country_id"]=> string(2) "13" ["country"]=> string(11) "cuncuncunuc" } [13]=> object(stdClass)#34 (2) { ["country_id"]=> string(2) "14" ["country"]=> string(1) "d" } [14]=> object(stdClass)#35 (2) { ["country_id"]=> string(2) "15" ["country"]=> string(1) "a" } [15]=> object(stdClass)#36 (2) { ["country_id"]=> string(2) "16" ["country"]=> string(4) "sdsd" } [16]=> object(stdClass)#37 (2) { ["country_id"]=> string(2) "17" ["country"]=> string(9) "scotlands" } [17]=> object(stdClass)#38 (2) { ["country_id"]=> string(2) "18" ["country"]=> string(11) "scotlandsss" } [18]=> object(stdClass)#39 (2) { ["country_id"]=> string(2) "19" ["country"]=> string(6) "asdasd" } [19]=> object(stdClass)#40 (2) { ["country_id"]=> string(2) "20" ["country"]=> string(21) "scotlandsssssssssssss" } }
Any help much appreciated.

Get specific value from PHP array using foreach key value

I have the following array:
array(15) {
[0]=> object(stdClass)#317 (2) { ["id"]=> string(1) "2" ["value"]=> string(1) "1" }
[1]=> object(stdClass)#316 (2) { ["id"]=> string(1) "3" ["value"]=> string(531) "awfaww" }
[2]=> object(stdClass)#315 (2) { ["id"]=> string(1) "4" ["value"]=> string(1) "1" }
[3]=> object(stdClass)#318 (2) { ["id"]=> string(1) "5" ["value"]=> string(1) "1" }
[4]=> object(stdClass)#319 (2) { ["id"]=> string(1) "6" ["value"]=> string(1) "1" }
[5]=> object(stdClass)#320 (2) { ["id"]=> string(1) "7" ["value"]=> string(1) "1" }
[6]=> object(stdClass)#321 (2) { ["id"]=> string(1) "8" ["value"]=> string(1) "1" }
[7]=> object(stdClass)#322 (2) { ["id"]=> string(2) "30" ["value"]=> string(8) "12:30:02" }
[8]=> object(stdClass)#323 (2) { ["id"]=> string(2) "31" ["value"]=> string(8) "18:12:00" }
[9]=> object(stdClass)#324 (2) { ["id"]=> string(2) "11" ["value"]=> string(10) "2014-06-17" }
[10]=> object(stdClass)#325 (2) { ["id"]=> string(2) "12" ["value"]=> string(10) "2014-06-26" }
[11]=> object(stdClass)#326 (2) { ["id"]=> string(2) "14" ["value"]=> string(1) "2" }
[12]=> object(stdClass)#327 (2) { ["id"]=> string(2) "15" ["value"]=> string(1) "2" }
[13]=> object(stdClass)#328 (2) { ["id"]=> string(2) "16" ["value"]=> string(1) "4" }
[14]=> object(stdClass)#329 (2) { ["id"]=> string(2) "17" ["value"]=> string(1) "5" }
}
I would like to get a specific value from this array using the ID. For example, if the ID: 11 is found in the array I want to retrieve its value. How can I do this?
Try something like this:
<?php
function findById($array, $id) {
foreach ($array as $value) {
if ($value->id == $id) {
return $value->value;
}
}
return null;
}
$result = findById($yourArray, 11);
?>
if the array is static for each run - i'd suggest changing the array into "key"=>"value" array, using this:
$new_arr = array();
foreach($original_array as $object) {
$new_arr[$object->id] = $object->value;
}
and then you can just use $new_arr[id], instead of searching the whole original array each time.
You can use array_filter:
array_filter($arr, function($i) { return $i->id == '11'; });
See Documentation

Secondary Drop Down with Preloaded options

I have Sub-Categories that are related to a Main Category. My sub cat table has the main category id as a foreign key. I'd like to preload all subcategories using javascript and avoid an extra php file or db query if possible.
Both dropdowns are listing all the records but...
I'd like my second dropdown list of sub-categories to change dynamically onChange (select) of the first dropdown based on the first dropdown's value which is the main cat id.
Example,
$data = $this->Category->MainCategory->findall();
$this->set('data',$data);
$data2 = $this->Category->SubCategory->findall();
$this->set('data2',$data2);
My arrays look like this:
var_dump ($data) =
array(5) {
[0]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(10) "Accounting" ["doctype"]=> string(1) "2" } }
[1]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(15) "Human Resources" ["doctype"]=> string(1) "2" } }
[2]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "4" ["name"]=> string(5) "Clubs" ["doctype"]=> string(1) "2" } }
[3]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(1) "8" ["name"]=> string(16) "Service" ["doctype"]=> string(1) "2" } }
[4]=> array(1) { ["MainCategory"]=> array(3) { ["id"]=> string(2) "10" ["name"]=> string(9) "Safety" ["doctype"]=> string(1) "2" } } }
var_dump($data2) =
array(20) {
[0]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "1" ["name"]=> string(17) "Application Forms" ["main_category_id"]=> string(1) "2" } }
[1]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "2" ["name"]=> string(19) "Benefit Claim Forms" ["main_category_id"]=> string(1) "2" } }
[2]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "3" ["name"]=> string(22) "Evaluations" ["main_category_id"]=> string(1) "2" } }
[3]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "4" ["name"]=> string(11) "Leave Forms" ["main_category_id"]=> string(1) "2" } }
[4]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "5" ["name"]=> string(13) "Payroll" ["main_category_id"]=> string(1) "2" } }
[5]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "6" ["name"]=> string(17) "Recruitment" ["main_category_id"]=> string(1) "2" } }
[6]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "7" ["name"]=> string(24) "Training" ["main_category_id"]=> string(1) "2" } }
[7]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "8" ["name"]=> string(10) "Accounting" ["main_category_id"]=> string(1) "1" } }
[8]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(1) "9" ["name"]=> string(13) "Staff" ["main_category_id"]=> string(1) "2" } }
[9]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "10" ["name"]=> string(14) "Codes" ["main_category_id"]=> string(2) "3" } }
[10]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "11" ["name"]=> string(28) "Reports" ["main_category_id"]=> string(2) "3" }
[11]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "12" ["name"]=> string(14) "Plan" ["main_category_id"]=> string(2) "4" } }
[12]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "13" ["name"]=> string(21) "Charts" ["main_category_id"]=> string(2) "4" } }
[13]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "14" ["name"]=> string(11) "Travel" ["main_category_id"]=> string(1) "4" } }
[14]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "15" ["name"]=> string(15) "Financials" ["main_category_id"]=> string(1) "4" } }
[15]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "16" ["name"]=> string(19) "Event Planning" ["main_category_id"]=> string(1) "4" } }
[16]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "17" ["name"]=> string(14) "Resources" ["main_category_id"]=> string(1) "4" } }
[17]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "18" ["name"]=> string(11) "Basics" ["main_category_id"]=> string(1) "4" } }
[18]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "19" ["name"]=> string(9) "News" ["main_category_id"]=> string(1) "4" } }
[19]=> array(1) { ["SubCategory"]=> array(3) { ["id"]=> string(2) "20" ["name"]=> string(12) “Funding" ["main_category_id"]=> string(1) "4" } } }
--html form ….
<td>Category: </td>
<td><select id="main_cat">
<option value=""> </option>
<?php foreach($data as $row){
echo "<option value=".$row['MainCategory']['id'].">" .$row['MainCategory']['name']. "</option>";
} ?>
</select></td>
<td>Sub Category: </td>
<td><select id="sub_cat">
<option value=""> </option>
<?php foreach($data2 as $row){
echo "<option value=".$row[‘SubCategory']['id'].">" .$row['SubCategory']['name']. "</option>";
} ?>
</select></td>
…..
Create a two dimensional javascript array loaded with the sub-categories by category id, then reload the second drop down when the first changes. Highly simplified jsfiddle example.
For the php, you'll only need to worry about generating the subCats array. Something along the lines of:
<script type="text/javascript>
var subCats = [
<?php foreach($data2 as $row){
echo "[";
foreach($row as $subcat) {
echo "\"" . $subcat . "\",";
}
echo "]";
} ?>
</script>
just.another.programmer got me on the right track and with some other examples from Jonathan Kuhn, this is the result http://jsfiddle.net/wprLD/9/
<script type="text/javascript">
var subCats =
<?php
$js = array();
foreach($data2 as $sub){
//get the parent id (main_category_id)
$parent = $sub['SubCategory']['main_category_id'];
//if the parent doesn't exist, add it
if(!isset($js[$parent])){
//add array with name and id
$js[$parent] = array(array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']));
//parent does exist
} else {
//append this entry name and id
$js[$parent][] = array('id'=>$sub['SubCategory']['id'],'name'=>$sub['SubCategory']['name']);
}
}
$js = array_values($js);
echo json_encode($js);
?>
;
function makeSubCatHtml(catId) {
var subCatHtml = "",
i;
//check if the subcats object has this cat Id.
if(subCats.hasOwnProperty(catId)){
for (i in subCats[catId]) {
subCatHtml += "<option value='"+subCats[catId][i].id+"'>" + subCats[catId][i].name + "</option>";
}
}
return subCatHtml;
}
$(document).ready(function () {
$("#MainCategory").bind("change", function () {
$("#SubCategory").html(
makeSubCatHtml(this.selectedIndex));
});
});
</script>

Categories