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>
Related
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
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();
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
I want to get data from an array using PHP loop like FOR or FOREACH.
when I print the content of the array using var_dump it looks like this:
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "query1"
["fql_result_set"]=>
array(13) {
[0]=>
array(3) {
["aid"]=>
string(19) "2944783819003364347"
["name"]=>
string(16) "Profile Pictures"
["photo_count"]=>
string(2) "37"
}
[1]=>
array(3) {
["aid"]=>
string(19) "2944783820076875780"
["name"]=>
string(7) "only me"
["photo_count"]=>
string(1) "2"
}
[2]=>
array(3) {
["aid"]=>
string(19) "2944783819003517141"
["name"]=>
string(35) "Ways To Tie Your Shoelaces. Nice :)"
["photo_count"]=>
string(1) "8"
}
[3]=>
array(3) {
["aid"]=>
string(19) "2944783819003490957"
["name"]=>
string(12) "Cover Photos"
["photo_count"]=>
string(2) "12"
}
[4]=>
array(3) {
["aid"]=>
string(19) "2944783819003481818"
["name"]=>
string(14) "Mobile Uploads"
["photo_count"]=>
string(2) "55"
}
}
}
what should I do to get: ["aid"] , ["name" ] and ["photo_count"]?
like this
foreach ($array[0]['fql_result_set'] as $record) {
echo "{$record['aid']}, {$record['name']}";
}
I have this array :
array(2) {
[1]=>
array(4) {
["name"]=>
string(14) "Les Contenants"
["ordre"]=>
string(1) "3"
[9]=>
array(1) {
["name"]=>
string(20) "Corbeilles unitaires"
}
[10]=>
array(1) {
["name"]=>
string(6) "Mannes"
}
}
[6]=>
array(3) {
["name"]=>
string(7) "L'utile"
["ordre"]=>
string(1) "1"
[133]=>
array(3) {
["name"]=>
string(7) "Paniers"
[192]=>
array(1) {
["name"]=>
string(13) "à provisions"
}
[193]=>
array(2) {
["name"]=>
string(13) "anses mobiles"
[201]=>
array(1) {
["name"]=>
string(19) "non doublés tissus"
}
}
}
}
}
I need to sort this array on this key : array[$i]['ordre'] on an ascending order.
The results must be :
array(2) {
[6]=>
array(3) {
["name"]=>
string(7) "L'utile"
["ordre"]=>
string(1) "1"
[133]=>
array(3) {
["name"]=>
string(7) "Paniers"
[192]=>
array(1) {
["name"]=>
string(13) "à provisions"
}
[193]=>
array(2) {
["name"]=>
string(13) "anses mobiles"
[201]=>
array(1) {
["name"]=>
string(19) "non doublés tissus"
}
}
}
}
[1]=>
array(4) {
["name"]=>
string(14) "Les Contenants"
["ordre"]=>
string(1) "3"
[9]=>
array(1) {
["name"]=>
string(20) "Corbeilles unitaires"
}
[10]=>
array(1) {
["name"]=>
string(6) "Mannes"
}
}
}
Have you an idea to do it ?
usort($array,function($a,$b) {return $a['ordre']-$b['ordre'];});
Or, if your version of PHP doesn't support lambda-functions:
usort($array,create_function('$a,$b','return $a["ordre"]-$b["ordre"];'));