Welcome Guest, Not a member yet? Register   Sign In
How to sor the cart result on different key
#1

[eluser]Unknown[/eluser]
Hello everybody,
I just know CodeIgniter today , and really get excited from its amazing functions. Especially the cart class.
Now I have a silly question.
How Can I sort the $cart to let the webpage display results from its rows as the key I want it sort with?
for example, I have userid, username, and useraddress in $cart, I want use useraddress to sort the $cart, how to?

really appreciate if you can put on the real code for it.
#2

[eluser]Unknown[/eluser]
I have figure out it by myself(learning from google)
cuz cart is an array, so what I found is the following codes to sort the array, and after I put the following codes and load it during insert new item into items. the sort is done.

unction sysSortArray($ArrayData,$KeyName1,$SortOrder1 = "SORT_ASC",$SortType1 = "SORT_REGULAR")
{
// Get args number.
$ArgCount = func_num_args();
// Get keys to sort by and put them to SortRule array.
for($I = 1;$I < $ArgCount;$I ++)
{
$Arg = func_get_arg($I);
$KeyNameList[] = $Arg;
$SortRule[] = '$'.$Arg;
}
// Get the values according to the keys and put them to array.
foreach($ArrayData AS $Key => $Info)
{
foreach($KeyNameList AS $KeyName)
{
${$KeyName}[$Key] = $Info[$KeyName];
}
}
// Create the eval string and eval it.
$EvalString = 'array_multisort('.join(",",$SortRule).',$ArrayData);';
eval ($EvalString);
return $ArrayData;
}


and use
$items= $this->sysSortArray($items,'category','name');
/*before*/
$this->set_cart($items);

then the results shows my need.
#3

[eluser]1ONE[/eluser]
I had similar problem, so I made one more row in my products table called ORDER.

This is how I fill up the array:

Code:
$data = array();
foreach ($query->result() as $row):

$data[] = array('id' => $row->id, 'qty' => '1', 'price' => $row->price, 'name' => $row->name, 'order' => $row->order);
endforeach;


and let's sort the array:

Code:
function subval_sort($a,$subkey) {
    foreach($a as $k=>$v) {
        $b[$k] = strtolower($v[$subkey]);
    }
    asort($b);
    foreach($b as $key=>$val) {
        $c[] = $a[$key];
    }
    return $c;
}
$this->cart->insert(subval_sort($data, 'order'));

I hope this helps!




Theme © iAndrew 2016 - Forum software by © MyBB