How To Sort The Drop Down List Without Altering The Table Data

hi, how to sort the data let’s say I wanna put the “Others” at the bottom of the drop down list of subcategory.

here’s the sample data of the subcategory table




subid      subcat

76         women bags

77         woman accessories

78         men apparels

79         women shoes & heels

80         men accessories

81         woman apparels

82         others

143        men shoes



and here’s the code that calls the subcategory





$subcatOps = array();


if (!empty($selectedMainCategory)) {


     $subCat = subcat::model()->findAll('MAINCATID =:mainCatId', array('mainCatId' => $selectedMainCategory));

    $subCatOps = CHtml::listdata($subCat, 'SUBID', 'SUBCAT');


}


echo $form->dropDownList($model, 'SUBID',$subCatOps, array('empty' => ' Select ', 'class' => 'subSelect', 'style' => ''));

echo $form->error($model, 'SUBID');




here’s what i did




$subcatOps = array();


if (!empty($selectedMainCategory)) {


$others = '';


foreach($subCatOps as $key => $item) {

 if($item === "Others"){

    $others = $subCatOps[$key];

    unset($subCatOps[$key]);

    }

 }

 $subCatOps[] .= $others;


}

echo $form->dropDownList($model, 'SUBID',$subCatOps, array('empty' => ' Select ', 'class' => 'subSelect', 'style' => ''));

echo $form->error($model, 'SUBID');




but nothing happens …it didn’t reflect in the sub category dropdown , what to do?

by the way , the SUBID should not change because it is in the db table…i suspect that’s the reason

why it doesn’t reflect in the sub category dropdown

I think the problem is that you are appending raw value “Others” to the array and not it’s: id => “Others” representation.


$othersValue = "Others";

$othersKey = '';

foreach($subCatOps as $key => $item) {

 if($item === $othersValue ){

    $othersKey = $key;

    unset($subCatOps[$othersKey]);

    }

 }

 $subCatOps[$othersKey] = $othersValue;

I’m not sure if its gonna work because I there is something missing in code you provided but, try to change it this way.

this didn’t work either, i wonder what’s the best way to solve this …driving me nuts

Dear Sasori

Would you please try the following.




$subCat = subcat::model()->findAll('MAINCATID =:mainCatId', array('mainCatId' => $selectedMainCategory));

$subCatOps = CHtml::listData($subCat, 'SUBID', 'SUBCAT');


foreach($subCatOps as $i=>$j)

  {

	  if($j=='others')

	  { 

             unset($subCatOps[$i]);

	     $subCatOps[$i]=$j;

          }

  }


echo $form->dropDownList($model, 'SUBID',$subCatOps);



Regards.