code igniter form_dropdown() order - php

I am trying to order the dropdown items in alphabetical order but am unable to do so. I must be missing something obvious..
I assumed ORDER BY type_name would have created the array in alphabetical order
$data['training_types'] = $this->db->query("SELECT * FROM training_types ORDER BY type_name")->result_array();
print_r($training_types);
foreach ($training_types as $type)
{
$options[$type['id']] = $type['type_name'];
echo $options[$type['id']]; //test only: this displays the options in alphabetical order just fine
}
print_r($options);
echo form_dropdown('training_type',$options,'0');
//for some reason when the dropdown is created, the order is not alphabetical, it's not even ordered by id... I have no idea what is ordering it this way.
1st print_r returns:
Array ( [0] => Array ( [id] => 6 [type_name] => Independent Study ) [1] => Array ( [id] => 1 [type_name] => Instructor Lead ) [2] => Array ( [id] => 3 [type_name] => Instructor Lead/Virtual ) [3] => Array ( [id] => 7 [type_name] => Job Aid ) [4] => Array ( [id] => 5 [type_name] => Mentoring ) [5] => Array ( [id] => 2 [type_name] => Virtual ) [6] => Array ( [id] => 4 [type_name] => Web ) )
2nd print_r returns:
Array ( [2] => Virtual [3] => Instructor Lead/Virtual [4] => Web [1] => Instructor Lead [5] => Mentoring [6] => Independent Study [7] => Job Aid )

Can you print_r($data['training_types']) before the foreach loop and print_r($options) after the loop, and post results? This will help give insight as to what is going into the loop and what is coming out, to make sure it isn't the Form Helper form_dropdown() isn't reordering anything.
My suggestion is to just add a simple asort($options); before the form_dropdown() to insure it is alphabetical.

You didn't specify whether it's Ascending or Descending.
ORDER BY type_name ASC or ORDER BY type_name DESC

Related

Array parsing using array value

i have below array,and i have amenities id = 50,i need to show amenities name like 'Express check-out' using amenities id = 50 from this array using php.
Array
(
[amenities] => Array
(
[0] => Array
(
[id] => 0
[name] => Cash machine
[key] => CASHMACHINE
)
[1] => Array
(
[id] => 42
[name] => Express check-in
[key] => EXPRESSCHECKINSERVICE
)
[2] => Array
(
[id] => 50
[name] => Express check-out
[key] => EXPRESSCHECKOUTSERVICE
)
[5] => Array
(
[id] => 3
[name] => Wi-Fi
[key] => WIFISERVICE
)
)
)
There are many ways that your problem can be solved. Easy way can be as follow
function getAmenities($array,$id){
foreach($array['amenities'] as $tmp_arr)
if($tmp_arr['id']==$id)
return $tmp_arr['name'];
}
echo getAmenities($array,50);
I have not checked result but should work fine. Please let me know if this works for you
How do u create this Array?
Can't u just use the id as the key when u create it, like:
$key = $array2['id'];
$array['amenities'][$key] = $array2;

Search by Many2One Field odoo PHP XMLRPC

i'm using this class for accessing odoo through PHP XMLRPC.
Everything works fine almost. I can't search many2one fields just by placing the id of the referenced record. I guess there should be a special way to code the many2one id in the search.
EG: searching in product.supplierinfo by the product_tmpl_id using this code i get emtpy array returned:
$rpc->searchread(array(array("product_tmpl_id","=","3673")),"product.supplierinfo");
Searching for the record by id i get this result:
$rpc->read(array(1),"", "product.supplierinfo");
Array
(
[0] => Array
(
[create_uid] => Array
(
[0] => xxxxx
[1] => xxxxx xxxxx
)
[product_code] =>
[create_date] => 2016-06-22 11:08:00
[name] => Array
(
[0] => 1438
[1] => Provider one
)
[product_uom] => Array
(
[0] => 1
[1] => Unit(s)
)
[sequence] => 1
[product_name] =>
[__last_update] => 2016-06-22 11:42:28
[company_id] => Array
(
[0] => 1
[1] => Company Name
)
[write_uid] => Array
(
[0] => xxxx
[1] => xxxxxx xxxxx
)
[delay] => 1
[write_date] => 2016-06-22 11:42:28
[pricelist_ids] => Array
(
)
[display_name] => Provider One
[min_qty] => 0
[qty] => 0
[product_tmpl_id] => Array
(
[0] => 3673
[1] => Product Name
)
[id] => 1
)
)
How should i code the id of a many2one field?
Any help will be appreciated.
Finally i had the solution. As #czoellner suggested was a problem was the type of the ID was a string and must be an integer. So, this code worked fine.
$rpc->searchread(array(array("product_tmpl_id","=",3673)),"product.supplierinfo");
On the other hand must consider the issue between the id of the product.product and the id of the product.template because they are differents. The table product.supplierinfo uses the id of product.template instead product.product. That causes for searching through the table produc.supplierinfo is necessary to find first the product_tmpl_id of the product to refer to the product. So for finding a all the providers of a product:
#search it by id
$prod_odoo = $rpc->read(array(1),"product.product",array());
#every search returns a 2 dimension array
$prod_suppliers = $rpc->searchread(array(array("product_tmpl_id","=",(int)$prod_odoo[0]["product_tmpl_id"][0])),"product.supplierinfo");
Hope this helps.

Extract sub array resulting key=id and value=name in CakePHP

I have this nested array:
Array
(
[id] => 1
[name] => Group 1
[0] => Array
(
[id] => 1
[name] => Group 1
)
[1] => Array
(
[id] => 2
[name] => Group 2
)
[2] => Array
(
[id] => 7
[name] => Group 7
)
)
And I would like to extract sub arrays [0], [1], and [2] in one single array BUT following this format:
array(
[id] => [name]
)
In other words I would like to have this result:
Array
(
[1] => Group 1
[2] => Group 2
[7] => Group 7
)
*Note: I tried with Set::classicExtract($my_array['Group'], '{n}.name'); but I can't figure out how to get group.id as a key for my array. Any guidance will be appreciated.
This should work for you:
(Here I first array_filter() all values out, which don't have a numeric key. After this I simply array_combine() the id column with the name column which I get with array_column())
<?php
$result = array_filter($arr, function($k){
return is_numeric($k);
}, ARRAY_FILTER_USE_KEY);
$result = array_combine(array_column($result, "id"), array_column($result, "name"));
print_r($result);
?>
output:
Array ( [1] => Group 1 [2] => Group 2 [7] => Group 7 )
I figured out in case if someone else will be interested. Thanks a lot for the quick answers but I was looking on a solution using CakePHP.
Set::combine($my_array, '{n}.id', '{n}.name'); did the trick, cheers!

Searching php arrays

I have a PHP array question regarding searching which I'm hoping some kind person can help me with...
The array shown below is an collection of arrays, e.g. order items. As I loop a separate array of orderIds I would like to return the appropriate array of products.
For example, if I request an orderId of 98305 it would return the arrays with the indexes of 2 & 3.
Are there any PHP functions to do this? I could loop each array and check the value and break out when it matches, but I feel this brings quite an overhead of performing multiple loops per orderId lookup.
Array
(
[0] => Array
(
[orderId] => 98303
[product] => Product A
)
[1] => Array
(
[orderId] => 98304
[product] => Product B
)
[2] => Array
(
[orderId] => 98305
[product] => Product C
)
[3] => Array
(
[orderId] => 98305
[product] => Product D
)
[4] => Array
(
[orderId] => 98306
[product] => Product A
)
[5] => Array
(
[orderId] => 98306
[product] => Product B
)
)
Any help appreciated.
D
array_filter()
$output = array_filter($input,function($a) {
return $a['orderId'] == 98305;
});
Replace 98305 with the desired ID.

mysql select all, group by orderid

I was wondering if anyone could help me with selecting information from my table, but grouping the results depending on the order id. I'm sure this is quite simple, but I can't seem to get the code working.
Here's my attempt - which is only showing 1 result, instead of 6:
4 results with orderid 55542
2 results with orderid 55543
SQL:
SELECT *
FROM #__users_orders
WHERE userid = 22
GROUP BY orderid
ORDER BY date DESC
Any help would be appreciated :)
EDIT:
I'd like to acheive this (or something similar)
Array[55542]
(
[0] => stdClass Object
(
[id] => 6
[userid] => 66
[orderid] => 55542
[date] => 2011-08-05 16:30:24
[code] => 121021
[title] => 7 STAR CHICKEN A/KING 71198 1.3KG
[units] => 2
[ctns] =>
)
[1] => stdClass Object
(
[id] => 1
[userid] => 66
[orderid] => 55542
[date] => 2011-08-05 16:06:12
[code] => 302371
[title] => ANCHOVY FILL 730GM
[units] => 2
[ctns] =>
)
[2] => stdClass Object
(
[id] => 6
[userid] => 66
[orderid] => 55542
[date] => 2011-08-05 16:30:24
[code] => 121021
[title] => 7 STAR CHICKEN A/KING 71198 1.3KG
[units] => 2
[ctns] =>
)
[3] => stdClass Object
(
[id] => 1
[userid] => 66
[orderid] => 55542
[date] => 2011-08-05 16:06:12
[code] => 302371
[title] => ANCHOVY FILL 730GM
[units] => 2
[ctns] =>
)
)
Array[55543]
(
[0] => stdClass Object
(
[id] => 6
[userid] => 66
[orderid] => 55543
[date] => 2011-08-05 16:30:24
[code] => 121021
[title] => 7 STAR CHICKEN A/KING 71198 1.3KG
[units] => 2
[ctns] =>
)
[1] => stdClass Object
(
[id] => 1
[userid] => 66
[orderid] => 55543
[date] => 2011-08-05 16:06:12
[code] => 302371
[title] => ANCHOVY FILL 730GM
[units] => 2
[ctns] =>
)
)
SELECT *
FROM #__users_orders
WHERE userid = 22
ORDER BY orderid DESC
Just select your items like this and create your object/array hierarchy in the frontend by iterating over the results and creating a new array for every new orderid that comes by.
SELECT orderid, COUNT(*)
FROM #__users_orders
WHERE userid = 22
GROUP BY orderid
ORDER BY date DESC
Normally, you have to use an aggregate (eg COUNT, SUM) and GROUP BY matched. So that columns in the SELECT but not in the COUNT or SUM are in the GROUP BY
Only MySQL allows you to not follow this rule. Other DB engines would give an error.
the query seems ok, maybe your extracting the results the wrong way
SELECT * FROM table GROUP BY field1;
should return same number of rows than
SELECT field1, field2 FROM table GROUP BY field1;
but different number than
SELECT * FROM table;
You should not select "*" in this query.
When you group by "some columns". You can only select "some columns" or some_aggregate_function(other columns).
e.g. If you want to get the aggregate ordersize and latest date for each order id, you would do something like -
SELECT orderid, sum(ordersize), max(date) FROM #__users_orders WHERE userid = 22 GROUP BY orderid ORDER BY max(date) DESC

Categories