Welcome Guest, Not a member yet? Register   Sign In
Wrong result sorting in Codeigniter(array and sorting)
#1

[eluser]isran_85[/eluser]
i have function for sorting on my page:
model :

function sorting_data($table,$idx,$sort){
$this->db->select('0,1,2,3,4,5,6');
$this->db->order_by($idx,$sort);
$this->db->from($table);
$sql= $this->db->get();
return $sql->result_array();
}
function sort_data($sort_by,$username){

switch($sort_by){
case 'bonus':
$idx =0; $sort ="asc";
$cl_data=$this->Model_Statement->sorting_data($username,$idx,$sort);
break;
case 'profit':
$idx =1; $sort ="asc";
$cl_data=$this->Model_Statement->sorting_data($username,$idx,$sort);
break;

controller:
$sort_by = $this->input->post('sorting');

$cl_data = $this->Model_Directions->sort_closed_data($sort_by,$username);

but the result like thisSadfrom biggest to smallest)
> 1000
> -3.48
> -2.32
> -2.19
> -2.06
> -1.7
> -1.35
> -0.83
> -0.04
is that wrong (correct result is : 1000,-0.04,0.83,-1.35)
i use varchar type in my field,if i use float type result is correct,but change data type isn't allowed.
someone help me please !!
i've some solution here but how to create model in ci,i have php code:
<?
$moviedata = array(
array('movie'=>'Home Alone', 'genre'=>'Comedy', 'revenue'=>-285),
array('movie'=>'The Godfather', 'genre'=>'Drama', 'revenue'=>268),
array('movie'=>'Finding Nemo', 'genre'=>'Animation', 'revenue'=>866),
array('movie'=>'Star Wars', 'genre'=>'Sci-Fi', 'revenue'=>797),
array('movie'=>'Grease', 'genre'=>'Musical', 'revenue'=>387),
array('movie'=>'Ghostbusters', 'genre'=>'Comedy', 'revenue'=>238),
array('movie'=>'Titanic', 'genre'=>'Drama', 'revenue'=>-600)
);

$cols = array();
foreach($moviedata as $row) {
foreach($row as $key => $value) {
if( !isset($cols[$key]) )
$cols[$key] = array();
$cols[$key][] = $value;
}
}
$data = $cols;
array_multisort( $data['revenue'],SORT_ASC,$data['genre'],$data['movie']);
print_r($data);echo "<br>";
?&gt;
this code will work for sorting but i can't to convert to codeigniter code.

here is my model code in codeigniter:
function sort_pips($base){
$i = 0;
$cols = array();
foreach ($base as $row){
foreach($row as $key => $value){
if( !isset($cols[$key]) )
$cols[$key] = array();
$cols[$key][] = $value;}
$data = $base[$i];

//$data = $cols;
array_multisort($data['13'],SORT_ASC,$data['1']);
$replacements = array(13 => $data13);

$basket[] = array_replace($data, $replacements);

$i++;
}
return $basket;
}
Please help me!!!!
#2

[eluser]Samus[/eluser]
[quote author="isran_85" date="1334054809"]i have function for sorting on my page:
model :

function sorting_data($table,$idx,$sort){
$this->db->select('0,1,2,3,4,5,6');
$this->db->order_by($idx,$sort);
$this->db->from($table);
$sql= $this->db->get();
return $sql->result_array();
}
function sort_data($sort_by,$username){

switch($sort_by){
case 'bonus':
$idx =0; $sort ="asc";
$cl_data=$this->Model_Statement->sorting_data($username,$idx,$sort);
break;
case 'profit':
$idx =1; $sort ="asc";
$cl_data=$this->Model_Statement->sorting_data($username,$idx,$sort);
break;

controller:
$sort_by = $this->input->post('sorting');

$cl_data = $this->Model_Directions->sort_closed_data($sort_by,$username);

but the result like thisSadfrom biggest to smallest)
> 1000
> -3.48
> -2.32
> -2.19
> -2.06
> -1.7
> -1.35
> -0.83
> -0.04
is that wrong (correct result is : 1000,-0.04,0.83,-1.35)
i use varchar type in my field,if i use float type result is correct,but change data type isn't allowed.
someone help me please !!
i've some solution here but how to create model in ci,i have php code:
&lt;?
$moviedata = array(
array('movie'=>'Home Alone', 'genre'=>'Comedy', 'revenue'=>-285),
array('movie'=>'The Godfather', 'genre'=>'Drama', 'revenue'=>268),
array('movie'=>'Finding Nemo', 'genre'=>'Animation', 'revenue'=>866),
array('movie'=>'Star Wars', 'genre'=>'Sci-Fi', 'revenue'=>797),
array('movie'=>'Grease', 'genre'=>'Musical', 'revenue'=>387),
array('movie'=>'Ghostbusters', 'genre'=>'Comedy', 'revenue'=>238),
array('movie'=>'Titanic', 'genre'=>'Drama', 'revenue'=>-600)
);

$cols = array();
foreach($moviedata as $row) {
foreach($row as $key => $value) {
if( !isset($cols[$key]) )
$cols[$key] = array();
$cols[$key][] = $value;
}
}
$data = $cols;
array_multisort( $data['revenue'],SORT_ASC,$data['genre'],$data['movie']);
print_r($data);echo "<br>";
?&gt;
this code will work for sorting but i can't to convert to codeigniter code.

here is my model code in codeigniter:
function sort_pips($base){
$i = 0;
$cols = array();
foreach ($base as $row){
foreach($row as $key => $value){
if( !isset($cols[$key]) )
$cols[$key] = array();
$cols[$key][] = $value;}
$data = $base[$i];

//$data = $cols;
array_multisort($data['13'],SORT_ASC,$data['1']);
$replacements = array(13 => $data13);

$basket[] = array_replace($data, $replacements);

$i++;
}
return $basket;
}
Please help me!!!![/quote]
try wrap your code in tags, it's hard to read in just plain text
#3

[eluser]CroNiX[/eluser]
Create a new column with type = float in your database. Then, select all of your numbers and insert them into the new column. Then, rename your new column to the old column and drop the old column.

You can use this to copy all of the data once you have created the new column.
Code:
UPDATE `table_name` SET `new_column` = `old_column`;

#4

[eluser]isran_85[/eluser]
your mind like this:
Model:
Code:
function sorting_data($table,$idx,$sort){
$this->db->select(‘0,1,2,3,4,5,6’);
$this->db->order_by($idx,$sort);
$this->db->from($table);
$sql= $this->db->get();
return $sql->result_array();
}
Code:
function sort_data($sort_by,$username){

switch($sort_by){
case ‘bonus’:
$idx =0; $sort =“asc”;
$cl_data=$this->Model_Statement->sorting_data($username,$idx,$sort);
break;
case ‘profit’:
$idx =1; $sort =“asc”;
$cl_data=$this->Model_Statement->sorting_data($username,$idx,$sort);
break;
......
....

Controller:
Code:
$sort_by = $this->input->post(‘sorting’);

$cl_data = $this->Model_Directions->sort_closed_data($sort_by,$username);

but the result like thisSadasc)
Quote:> 1000
> -3.48
> -2.32
> -2.19
> -2.06
> -1.7
> -1.35
> -0.83
> -0.04
is that wrong (correct result is : 1000,-0.04,0.83,-1.35)
i use varchar type in my field,if i use float type result is correct,but change data type isn’t allowed.
someone help me please !!
i’ve some solution here but how to create function model in ci,i have php code:
Code:
&lt;?
$moviedata = array(
array(‘movie’=>‘Home Alone’,  ‘genre’=>‘Comedy’, ‘revenue’=>-285),
array(‘movie’=>‘The Godfather’, ‘genre’=>‘Drama’, ‘revenue’=>268),
array(‘movie’=>‘Finding Nemo’,  ‘genre’=>‘Animation’, ‘revenue’=>866),
array(‘movie’=>‘Star Wars’,    ‘genre’=>‘Sci-Fi’, ‘revenue’=>797),
array(‘movie’=>‘Grease’,      ‘genre’=>‘Musical’, ‘revenue’=>387),
array(‘movie’=>‘Ghostbusters’,  ‘genre’=>‘Comedy’, ‘revenue’=>238),
array(‘movie’=>‘Titanic’,    ‘genre’=>‘Drama’, ‘revenue’=>-600)
);

$cols = array();
foreach($moviedata as $row) {
foreach($row as $key => $value) {
  if( !isset($cols[$key]) )
  $cols[$key] = array();
  $cols[$key][] = $value;
}
}
$data = $cols;
array_multisort( $data[‘revenue’],SORT_ASC,$data[‘genre’],$data[‘movie’]);
print_r($data);echo “
“;
?&gt;

this code will work for sorting but i can’t to convert to codeigniter code.

here is my function on model code in codeigniter:

Code:
function sort_pips($base){
$i = 0;
$cols = array();
        foreach ($base as $row){
          foreach($row as $key => $value){
              if( !isset($cols[$key]) )
          $cols[$key] = array();
          $cols[$key][] = $value;}
$data = $base[$i];

//$data = $cols;
array_multisort($data[‘13’],SORT_ASC,$data[‘1’]);
$replacements = array(13 => $data13);

  $basket[] = array_replace($data, $replacements);

$i++;
}
return $basket;
}
but it's still error please the suggest
#5

[eluser]CroNiX[/eluser]
I did. Use the proper datatype for this. It would literally take you less than 5 minutes to fix and be a lot more efficient than what you are trying to do.
#6

[eluser]isran_85[/eluser]
i'm not allowed to modify data type for this,,
#7

[eluser]CroNiX[/eluser]
Strange that someone wouldn't want you to use the proper datatype. I suppose you can cast the varchar as a float in the ORDER BY in your query.
#8

[eluser]isran_85[/eluser]
i've tried but error,
#9

[eluser]isran_85[/eluser]
if use cast as float how create it,




Theme © iAndrew 2016 - Forum software by © MyBB