jquery - Unable to save Ordering to database - Codeigniter -
i using codeigniter , within trying save ordering of list first drag , drop re-order , click save save reordering. however, list not saved. appreciated.
view file:
<div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <section> <h2>order songs</h2> <p class="alert alert-info">drag order songs , click 'save'</p> <div id="orderresult"></div> <? echo form_button('save', 'save', 'class="btn btn-default" id="save" value="save"')?> </section> </div> </div> </div> <script> $(function() { $.post('<?php echo site_url('admin/songs/order_ajax'); ?>', {}, function(data){ $('#orderresult').html(data); }); $('#save').click(function(){ osortable = $('.sortable').nestedsortable('toarray'); $('#orderresult').slideup(function(){ $.post('<?php echo site_url('admin/songs/order_ajax'); ?>', { sortable: osortable }, function(data){ $('#orderresult').html(data); $('#orderresult').slidedown(); }); }); }); }); </script>
order_ajax file:
<?php echo get_ol($songs); function get_ol ($array, $child = false) { $str = ''; if (count($array)) { $str .= $child == false ? '<ol class="sortable">' : '<ol>'; foreach ($array $item) { $str .= '<li id="list_' . $item['id'] .'">'; $str .= '<div>' . $item['songname'] .'</div>'; // have children? if (isset($item['children']) && count($item['children'])) { $str .= get_ol($item['children'], true); } $str .= '</li>' . php_eol; } $str .= '</ol>' . php_eol; } return $str; } ?> <script> $(document).ready(function(){ $('.sortable').nestedsortable({ handle: 'div', items: 'li', toleranceelement: '> div', maxlevels: 2 }); }); </script>
the controller:
public function order() { $data['sortable'] = true; $data['title'] = 're-order'; $data['module'] = 'admin'; $data['header_file'] = 'admin_header'; $data['nav_file'] = 'admin_nav'; $data['view_file'] = 'song_order'; $data['script_file'] = 'admin_script'; echo modules::run('template/template_admin', $data); } public function order_ajax() { // save order ajax call if (isset($_post['sortable'])) { $this->mdl_admin_songs->save_order($_post['sortable']); } // fetch pages $data['songs'] = $this->mdl_admin_songs->get_nested(); // load view $this->load->view('song_order_ajax', $data); }
the model:
public function get_nested() { $songs = $this->db->get('songrequest')->result_array(); $array = array(); foreach ($songs $song) { if (!$song['parent_id']) { $array[$song['id']] = $song; } else { $array[$song['parent_id']]['children'][] = $song; } } return $array; } public function save_order($songs) { if (count($songs)) { foreach ($songs $order => $song) { if ($song['item_id'] != '') { $data = array('parent_id' => (int) $song['parent_id'], 'order' => $order); $this->db->set($data)->where($this->_primary_key, $song['item_id'])->update($this->_table_name); } } } }
thanks, naveen
Comments
Post a Comment