Order of multi select

edited May 2011 in Modules
Hi all,
I'm new of fuel, and code igniter too, but I'm quite expert about mysql management, and following your tutorials I'm developing my first CMS as well.
I have a question about the multi list. Following your tutorials http://www.getfuelcms.com/user_guide/modules/tutorial
I'm trying to create a multiple choice select, because I need to add a field to the table models form to allow me to associate one or more songs to a setlist's gig. Your example about "Associating Categories to Articles" fit to me, except that I need to keep the record in the right order.

I get the array from the table with:

$songs_setlist = (!empty($values['id_gig'])) ? ($CI->songs_to_gigs_model->find_all_array_assoc('id_song', array('id_gig' => $values['id_gig']),"")) : array();

In my song_to_gig_model I have a method _common_query like the following:

$this->db->select('tbl_songs_gigs.*, tbl_songs.name_song');
$this->db->join('tbl_gigs', 'tbl_songs_gigs.id_gig = tbl_gigs.id_gig', 'left');
$this->db->join('tbl_songs', 'tbl_songs_gigs.id_song = tbl_songs.id_song', 'left');
$this->db->order_by('tbl_songs_gigs.number_song','ASC');

It seems that the songs in my tbl_song_gigs aren't sorted in the right order, which is the field "number_song".

Can you help me with some tips to solve this problem ?

Thank you

Paolo

Comments

  • edited 11:17PM
    Does it sort it correctly if you use just find_all_array to set $song_setlist? Also, you can debug the query after you call it by using $CI->songs_to_gigs_model->debug_query() (I use that all the time).
  • edited 11:17PM
    Hi,
    thank you for the answer.
    By using $CI->songs_to_gigs_model->debug_query() the query seems correct:

    SELECT `tbl_songs_gigs`.*, `tbl_songs`.`name_song` FROM (`tbl_songs_gigs`) LEFT JOIN `tbl_gigs` ON `tbl_songs_gigs`.`id_gig` = `tbl_gigs`.`id_gig` LEFT JOIN `tbl_songs` ON `tbl_songs_gigs`.`id_song` = `tbl_songs`.`id_song` WHERE `tbl_songs_gigs`.`id_gig` = '166' ORDER BY `tbl_songs_gigs`.`number_song` ASC

    I also tried to print the associative array $songs_setlist and the array is perfectly sorted (look at the key "number song"):

    Array ( [35] => Array ( [id_song_gig] => 995 [id_gig] => 166 [id_song] => 35 [number_song] => 1 [note_song] => [name_song] => Caravan girl ) [9] => Array ( [id_song_gig] => 994 [id_gig] => 166 [id_song] => 9 [number_song] => 2 [note_song] => [name_song] => Believer ) [11] => Array ( [id_song_gig] => 993 [id_gig] => 166 [id_song] => 11 [number_song] => 3 [note_song] => [name_song] => Alive ) )

    but the sort in the filled multiselect list is still wrong. Maybe I have to modify something in the javascript assets ?
  • edited 11:17PM
    Possibly. There is an autoSort parameter that gets passed to that multi select plugin that will automatically sort the right hand values and by default it is set to true. See around line 370 in the fuel/modules/fuel/assets/js/fuel/controller/BaseFuelController.js file. In your model, you can add the parameter 'sorting' => TRUE for that particular field in your form_fields method and will turn off auto sorting as well as allow you to sort the right hand side.
  • edited 11:17PM
    Good! I've added 'sorting'=>true in my method, and in the js file I've turned isSortable to 'TRUE'. Now the order of the items match with the order of record in my table. And, surprisely, the items are also draggable. But is it possible to update the order in the db simply by drag the items in the list ? It could be very useful! Is this feature documented ? Thank you

    Paolo
  • edited 11:17PM
    Ok. I changed the on_after_save method and now all work as I need. Thank you for your help!
  • edited 11:17PM
    That's exactly how to do it. Glad it worked!
Sign In or Register to comment.