Codeigniter Calendar template modification of content - codeigniter

I'm using the CI Calendar. The db that I've connected it with has columns for: date, hours, category, and notes. I started out with the same format as in this tutorial from net tuts+. I can get the calendar to display the notes on the appropriate day, but I'm not sure how to split up the content section into hours, category and notes. I know I have to modify the generate calendar in the system file, but not sure how to do it.
Here's my code:
Controller:
function index($year = null, $month = null)
{
if (!$year) {
$year = date('Y');
}
if (!$month) {
$month = date('m');
}
$this->load->model('calendar_model');
if ($day = $this->input->post('day')) {
$this->calendar_model->add_calendar_data(
"$year-$month-$day",
$this->input->post('hours'),
$this->input->post('category'),
$this->input->post('notes')
);
}
$data['calendar'] = $this->calendar_model->generate($year, $month);
// Load a view in the content partial
$this->template->content->view('includes/user_navigation');
$this->template->content->view('dashboard', $data);
// Display the template
$this->template->publish();
}
Model:
<?php
class Calendar_model extends CI_Model {
var $conf;
function Calendar_model()
{
parent::__construct();
}
function get_calendar_data($year, $month) {
$query = $this->db->select()->from('calendar')
->like('date', "$year-$month", 'after')->get();
$cal_data = array();
foreach ($query->result() as $row) {
$cal_data[substr($row->date,8,2)] = $row->notes;
/* Testing purposes */
echo "<p>" . $row->date . "</p>";
echo"<p>" . $row->hours . "</p>";
echo "<p>" . $row->category . "</p>";
echo "<p>" . $row->notes . "</p>";
}
return $cal_data;
}
function add_calendar_data($date, $hours, $category, $notes) {
if ($this->db->select('date')->from('calendar')
->where('date', $date)->count_all_results()) {
$this->db->where('date', $date)
->update('calendar', array(
'date' => $date,
'hours' => $hours,
'category' => $category,
'notes' => $notes
));
} else {
$this->db->insert('calendar', array(
'date' => $date,
'hours' => $hours,
'category' => $category,
'notes' => $notes
));
}
}
function generate ($year, $month) {
$this->load->library('calendar');
$cal_data = $this->get_calendar_data($year, $month);
return $this->calendar->generate($year, $month, $cal_data);
}
}
Calendar template:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| Calendar configuration
|--------------------------------------------------------------------------
| This file will contain the settings for the calendar template library.
|
*/
$config['day_type'] = 'long';
$config['show_next_prev'] = true;
$config['next_prev_url'] = base_url('index.php/calendar/display');
$config['template'] = '
{table_open}
<table class="calendar">
{/table_open}
{heading_row_start}<tr>{/heading_row_start}
{heading_previous_cell}<th><<</th>{/heading_previous_cell}
{heading_title_cell}<th colspan="{colspan}">{heading}</th>{/heading_title_cell}
{heading_next_cell}<th>>></th>{/heading_next_cell}
{heading_row_end}</tr>{/heading_row_end}
{week_day_cell}
<th class="day_header">{week_day}</th>
{/week_day_cell}
{cal_row_start}<tr class="days">{/cal_row_start}
{cal_cell_start}<td class="day">{/cal_cell_start}
{cal_cell_content}
<div class="day_num">{day}</div>
<div class="content">
<div class="category">{category}
<div class="hours">{hours}
<div class="notes">{notes}
{content}
</div>
</div>
</div>
</div>
{/cal_cell_content}
{cal_cell_content_today}
<div class="today"><div class="day_num">{day}</div>
<div class="content">{content}</div></div>
{/cal_cell_content_today}
{cal_cell_no_content}
<div class="day_num">{day}</div>
{/cal_cell_no_content}
{cal_cell_no_content_today}
<div class="today"><div class="day_num">{day}</div></div>
{/cal_cell_no_content_today}
';
js code in my view:
$(document).ready(function(){
var date;
// === Prepare calendar === //
$('.calendar .day').click(function() {
day_num = $(this).find('.day_num').html();
$("#activityModal").modal('toggle');
$('#add-event-submit').click(function(){
var hrs = document.getElementById('activityHrs').value;
var note = document.getElementById('activityNotes').value;
var cat = document.getElementById('activityCats');
var selectedCat = cat.options[cat.selectedIndex].text;
var message = "Date: " + date + "\n";
message += "hrs: " + hrs + "\n";
message += "category: " + selectedCat + "\n";
message += "note: " + note;
message += "day num: " + day_num;
//alert(message);
if (hrs != null && selectedCat !=null && note !=null) {
$.ajax({
url: window.location,
type: 'POST',
data: {
day: day_num,
hours: hrs,
category: selectedCat,
notes: note
},
success: function(msg) {
location.reload();
}
});
}
});
});
});

Related

Filter custom post with taxonomy - Add ALL button

I am setting up a custom post with a front-office display filter via taxonomy. After several hours of research and tests, I finally managed to get what I want but I still have 2 little things on which I am stuck... I would have liked to add an ALL button to re-display all my archives if we clicked on another filter before.
I also can’t add a class to my filter button when it is activated... Do you have a lead to refer me please?
Below what I did:
archive-work.php
<div id="work-filter" class="col-md-12">
<?php get_work_filters(); ?>
</div>
<div class="work-results animated fadeIn">
<?php $res = my_get_posts();
echo $res['response']; ?>
</div>
functions.php
/***************** filter work ****************/
function ajax_filter_posts_scripts() {
// Enqueue script
wp_register_script('afp_script', get_template_directory_uri() .
'/assets/js/work.js', false, null, false);
wp_enqueue_script('afp_script');
wp_localize_script( 'afp_script', 'afp_vars', array(
'afp_nonce' => wp_create_nonce( 'afp_nonce' ), // Create nonce which we later will use to verify AJAX request
'afp_ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action('wp_enqueue_scripts', 'ajax_filter_posts_scripts', 100);
$result = array();
// Script for getting posts
function ajax_filter_get_posts( $work_item ) {
// Verify nonce
if( !isset( $_POST['afp_nonce'] ) ||
!wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ))
die('Permission denied');
$work_item = $_POST['expertises'];
$result = json_encode(my_get_posts($work_item, true));
echo $result;
die();
}
function my_get_posts($work_item = '', $ajax = false){
// WP Query
$args = array(
'expertises' => $work_item,
'post_type' => 'work',
'posts_per_page' => -1,
);
// If taxonomy is not set, remove key from array and get all posts
if( !$work_item ) {
unset( $args['expertises'] );
}
$query = new WP_Query( $args );
$html = '';
$items = array();
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
$res = '<div class="works">'.
'<a href="'.get_permalink().'">'.
'<article class="panel panel-default" id="post-'.get_the_id().'">'.
'<div class="panel-body">'.
'<div class="panel-cover">'.
'<h3>'.get_the_title().'</h3>'.
get_the_excerpt().
'</div>'.
'<div class="imgworkarch">'.
get_the_post_thumbnail( $post = null, $size = 'archivework' ).
'</div>'.
'</div>'.
'</article>'.
'</a>' .
'</div>';
$ajax ? $items[] = $res : $html .= $res;
endwhile;
$result['response'] = $ajax ? $items : $html;
$result['status'] = 'success';
else:
$result['response'] = '<h2>No posts found</h2>';
$result['status'] = '404';
endif;
wp_reset_postdata();
return $result;
}
add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
//Get Work Filters
function get_work_filters()
{
$work_items = get_terms('expertises');
$filters_html = false;
$count = count( $work_items );
if( $count > 0 ):
foreach( $work_items as $work_item )
{
$work_item_id = $work_item->term_id;
$work_item_name = $work_item->name;
$filters_html .= '<a href="' .
get_term_link( $work_item ) .
'" class="btn work-filter" title="' .
$work_item->slug . '">' . $work_item->name . '</a> ';
}
echo $filters_html;
endif;
}
work.js
$(document).ready(function(){
// work filters
$('.work-filter').click( function(event) {
// Prevent default action - opening tag page
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
// Get tag slug from title attirbute
var expertises = $(this).attr('title');
data = {
action: 'filter_posts', // function to execute
afp_nonce: afp_vars.afp_nonce, // wp_nonce
post_type: "work", // selected tag
expertises: expertises,
};
$.ajax({
type: "post",
dataType: "json",
url: afp_vars.afp_ajax_url,
data: data,
success: function(data, textStatus, XMLHttpRequest) {
console.log(data);
// Restore div visibility
$('.work-results').fadeOut()
.queue(function(n) {
$(this).html(data.response);
n();
}).fadeIn();
},
error: function( XMLHttpRequest, textStatus, errorThrown ) {
/*console.log( MLHttpRequest );
console.log( textStatus );
console.log( errorThrown );*/
$('.work-results').fadeOut()
.queue(function(n) {
$(this).html("No items found. ");
n();
}).fadeIn();
}
});
});
});
Maybe it's too late to answer this, but I hope it helps someone.
You just have to add another button into your work-filter block with a different title like 'reset', then remove the taxonomy filter on the query in your my_get_posts when the value matches the title. Here is the code:
function my_get_posts($taxonomy = '', $ajax = false)
{
if($taxonomy != 'reset'){
$args = array(
'service' => $taxonomy,
'post_type' => 'projects',
'posts_per_page' => -1,
);
}else{
$args = array(
'post_type' => 'projects',
'posts_per_page' => -1,
);
}
if (!$taxonomy) {
unset($args['service']);
}
$query = new WP_Query($args);
$html = '';
$items = array();
if ($query->have_posts()) :
while ($query->have_posts()) :
$query->the_post();
$res = '<div class="col-lg-4">' .
'<a href="' . get_permalink() . '">' .
'<article class="panel panel-default" id="post-' . get_the_id() . '">' .
'<div class="panel-body">' .
get_the_post_thumbnail() .
'<div class="panel-cover">' .
'<h3>' . get_the_title() . '</h3>' .
get_the_content() .
'</div>' .
'</div>' .
'</article>' .
'</a>' .
'</div>';
$ajax ? $items[] = $res : $html .= $res;
endwhile;
$result['response'] = $ajax ? $items : $html;
$result['status'] = 'success';
else :
$result['response'] = '<h2>No posts found</h2>';
$result['status'] = '404';
endif;
wp_reset_postdata();
return $result;
}
function reset_all(){
echo 'All ';
}

Call to undefined method Product_filter_model::fetch_data() | 500 Internal Server Error | Codeigninter

I am new to CodeIgniter and I have been struggling with this error for the past 3 hours.
Call to undefined method Product_filter_model::fetch_data() error in the following code.
You can view the error in Inspect > Network Tab at the following link:
http://admin.millionkidstoschool.org/index.php/product_filter
I have confirmed the names of the model, controller, and functions. I have also tried putting $this->load->model('product_filter_model'); inside the function fetch_data() and it did not help.
Controller
class Product_filter extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('product_filter_model');
$this->load->helper('url');
}
function fetch_data(){
$minimum_price = $this->input->post('minimum_price');
$maximum_price = $this->input->post('maximum_price');
$brand = $this->input->post('brand');
$ram = $this->input->post('ram');
$storage = $this->input->post('storage');
$output = array(
/*'pagination_link' => $this->pagination->create_links(),*/
'product_list' => $this->product_filter_model->fetch_data($minimum_price, $maximum_price, $brand, $ram, $storage)
);
echo json_encode($output);
}
View
$(document).ready(function(){
filter_data();
function filter_data(){
$('#filter_data').html("<div id='loading'></div>");
var action = 'fetch_data';
var minimum_price = $('#hidden_minimum_price').val();
var maximum_price = $('#hidden_maximum_price').val();
var brand = get_filter('brand');
var ram = get_filter('ram');
var storage = get_filter('storage');
$.ajax({
url:"<?php echo base_url(); ?>index.php/product_filter/fetch_data",
method:"POST",
dataType:"JSON",
data:{action:action, minimum_price:minimum_price,maximum_price:maximum_price, brand:brand, ram:ram, storage:storage},
success:function(data){
$('.filter_data').html(data.product_list);
/*$('#pagination_link').html(data.pagination_link);*/
}
})
}
}
Model
function fetch_data($minimum_price, $maximum_price, $brand, $ram, $storage)
{
$query = $this->make_query($minimum_price, $maximum_price, $brand, $ram, $storage);
/*$query .= ' LIMIT '.$start.', ' . $limit;*/
$data = $this->db->query($query);
$output = '';
if($data->num_rows() > 0)
{
foreach($data->result_array() as $row)
{
$output .= '
some data
';
}
}
else
{
$output = '<h3>No Data Found</h3>';
}
return $output;
}
Thank you for your time.
UPDATE:
I copied both the model methods' code in controller function where ajax request is posted. It worked. But I am unable to understand why the Controller function is unable to access Model Methods?
public function fetch_data(){
$this->load->model('product_filter_model');
$minimum_price = $this->input->post('minimum_price');
$maximum_price = $this->input->post('maximum_price');
$brand = $this->input->post('brand');
$ram = $this->input->post('ram');
$storage = $this->input->post('storage');
//$output = array(
/*'pagination_link' => $this->pagination->create_links(),*/
// 'product_list' => $this->product_filter_model->fetch_data//($minimum_price, $maximum_price, $brand, $ram, $storage)
//);
/*$query = $this->product_filter_model->make_query($minimum_price, $maximum_price, $brand, $ram, $storage);*/
/*$query .= ' LIMIT '.$start.', ' . $limit;*/
$query = "
SELECT * FROM product
WHERE product_status = '1'
";
if(isset($minimum_price, $maximum_price) && !empty($minimum_price) && !empty($maximum_price))
{
$query .= "
AND product_price BETWEEN '".$minimum_price."' AND '".$maximum_price."'
";
}
if(isset($brand)){
$brand_filter = implode("','", $brand);
$query .="
AND product_brand IN('".$brand_filter."')
";
}
if(isset($ram)){
$ram_filter = implode("','", $ram);
$query .="
AND product_ram IN('".$ram_filter."')
";
}
if(isset($storage)){
$storage_filter = implode("','", $storage);
$query .="
AND product_storage IN('".$storage_filter."')
";
}
/*
return $query;*/
$data = $this->db->query($query);
$result = '';
if($data->num_rows() > 0)
{
foreach($data->result_array() as $row)
{
$result .= '
<div class="col-sm-4 col-lg-3 col-md-3">
<div style="border:1px solid #ccc; border-radius:5px; padding:16px; margin-bottom:16px; height:450px;">
<img src="'.base_url().'images/'. $row['product_image'] .'" alt="" class="img-responsive" >
<p align="center"><strong>'. $row['product_name'] .'</strong></p>
<h4 style="text-align:center;" class="text-danger" >'. $row['product_price'] .'</h4>
<p>Camera : '. $row['product_camera'].' MP<br />
Brand : '. $row['product_brand'] .' <br />
RAM : '. $row['product_ram'] .' GB<br />
Storage : '. $row['product_storage'] .' GB </p>
</div>
</div>
';
}
$output = array('product_list' => $result);
}
else
{
$result = '<h3>No Data Found</h3>';
}
echo json_encode($output);

Wordpress Ajax pagination returning 0 after second time

I am attempting to paginate my posts with an ajax.
I set an attribute "data-page" to the page link and extract it with jQuery to pass it with Ajax.
The first time a click on the link everything works fine but then if I click on a different link the ajax call returns 0.
Here's my code in my functions.php:
wp_register_script('load_post_ajax', get_template_directory_uri() . '/includes/ajax/load_post_ajax.js',array( 'jquery' ), 1.1, true);
$php_array = array( 'admin_ajax' => admin_url( 'admin-ajax.php' ) );
wp_localize_script( 'load_post_ajax', 'php_array', $php_array );
wp_enqueue_script( 'load_post_ajax' );
/*
* PAGINATION
*/
function hs_pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
if(isset($_POST['paged'])){
$paged = $_POST['paged'];
}else{
$paged = get_query_var( 'paged', 1 );
}
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages." </span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."' data-page=1>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<a href=\"#\" class=\"current\" data-page=$i>".$i."</a>":"<a href='".get_pagenum_link($i)."' class=\"inactive\" data-page=$i>".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\" data-page=$paged + 1>Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."' data-page=$pages>Last »</a>";
echo "</div>\n";
}
}
/*
* AJAX POSTS LOADER
*/
add_action( 'wp_ajax_load_post_ajax', 'load_post_ajax_init' );
add_action( 'wp_ajax_nopriv_load_post_ajax', 'load_post_ajax_init' );
function load_post_ajax_init() {
$total_posts_per_page = get_option('posts_per_page');
if($_POST['paged']){
$paged = $_POST['paged'];
}else{
$paged = get_query_var( 'paged', 1 );
}
$args = array(
'posts_per_page' => $total_posts_per_page,
'paged' => $paged,
'post_type' => 'post'
);
$posts = query_posts($args);
if($posts){
$count =0;
foreach($posts as $post){
?>
<article class="post">
<?php
if($paged < 2){
if(has_post_thumbnail($post->ID) ){
the_post_thumbnail($post->ID,'big',array('class'=>'img-responsive center-block'));
}else{
echo "no thumb";
}
}else{
echo "not first post";
}
?>
<p class="post_info hs_color"><?php echo $post->post_title . " Posted on: " . $post->post_date . " Author: ". get_the_author_meta( 'display_name',$post->post_author );?></p>
<p><?php echo $post->post_content;?></p>
<div class="post_link clearfix">
<a href="<?php the_permalink($post->ID);?>">
<h6 class="text-uppercase text-center">Read more</h6>
<div class="hs_square hs_square_right hs_bg"></div>
</a>
</div>
</article>
<?php
$count++;
}
}else {
echo "no posts found";
}
//PAGINATION
hs_pagination();
//PAGINATION
}
/*
*AJAx CALL
*/
jQuery( document ).ready(function() {
$('#post-section .container .pagination a').on('click',function(e){
/** Prevent Default Behaviour */
e.preventDefault();
var page = $(this).attr('data-page');
/** Ajax Call */
$.ajax({
cache: false,
timeout: 8000,
url: php_array.admin_ajax,
type: "POST",
data: ({
action:'load_post_ajax',
paged:page,
}),
beforeSend: function() {
},
success: function( data,response ){
$( '#post-section .container' ).addClass('animated FadeIn');
$( '#post-section .container' ).html( data);
},
error: function( jqXHR, textStatus, errorThrown ){
console.log( 'The following error occured: ' + textStatus, errorThrown );
},
});
});
});

Ajax Pagination on User Meta Data

How can I set a pagination link in my AJAX if my script search user depends on user meta-data. The pagination works fine but the problem is with the pagination link not working. Can someone please help me figure out how to set the pagination link properly?
This is my AJAX script:
function nanny_search_ajax1() {
$search_in = $_POST['search_in'];
$search_type = $_POST['search_type'];
$start_date = $_POST['start_date'];
$keyword = $_POST['keyword'];
if($search_in=='nanny'){
$search_in ='interested';
}
else{
$search_in ='lookgin';
}
$start_value = $start_date;
$start_compare = '=';
$nanny_result = '';
$nanny_error = '';
$number = 2;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $number;
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'user_start',
'value' => $start_value,
'compare' => $start_compare
)
),
'offset'=>$offset,
'number'=>$number,
);
$users = get_users();
$query = get_users($args);
$total_users = count($users);
$total_query = count($query);
$total_pages = intval($total_users / $number) + 1;
$query = get_users($args);
if (!empty($query)) {
foreach ($query as $user) {
$profile_picture = wp_get_attachment_url(get_user_meta($user->ID, 'be_custom_avatar', true));
$profile_picture = get_avatar($user->ID, 100);
if($search_in=='nanny'){
$interested = get_user_meta($user->ID, 'interested', true);
}
else{
$interested = get_user_meta($user->ID, 'lookgin', true);
}
$user_date = get_user_meta($user->ID, 'user_start', true);
$aboutme = get_user_meta($user->ID, 'aboutme', true);
$user_link = get_author_posts_url($user->ID);
$nanny_result .= "<div class='nanny-babysiter-box'>";
if ($profile_picture != '') {
$nanny_result .="<div class='nanny-babysiter-box-img'>$profile_picture</div>";
}
$nanny_result .="<div class='user-icons'><ul><li><a href='#'><i class='fa fa-circle-o'></i></a></li><li><a href='#'><i class='fa fa-envelope-o'></i></a></li><li><a href='#'><i class='fa fa-star'></i></a></li></ul></div>";
$nanny_result .="<div class='user-details'><div class='details-sub search-title'><a target='_blank' href='$user_link'><span>$user->display_name</span></a></div>";
$nanny_result .="<div class='details-sub'>$interested</div>";
if ($user_date != '') {
$nanny_result .="<div class='details-sub'><span>Start Date : </span>$user_date</div>";
}
if ($aboutme != '') {
$nanny_result .="<div class='details-sub'><span>About Me : </span></div>";
}
$nanny_result .="</div>";
if ($aboutme != '') {
$nanny_result .="<div class='details-sub'>$aboutme</span></div>";
}
$nanny_result .="</div>";
}
if ($total_users > $total_query) {
$nanny_result.='<div id="pagination" class="clearfix">';
$nanny_result.='<span class="pages">Pages:</span>';
$current_page = max(1, get_query_var('paged'));
$nanny_result.= paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => $current_page,
'total' => $total_pages,
'prev_next' => false,
'type' => 'list',
));
$nanny_result.= '</div>';
}
} else {
$nanny_error = '<div>Unfortunately at present there is no one matching your exact search criteria. You may like to consider other candidates.</div>';
}
echo json_encode(array("nanny_result" => $nanny_result, "nanny_error" => $nanny_error));
die;
}
add_action('wp_ajax_nopriv_nanny_search_ajax1', 'nanny_search_ajax1');
add_action('wp_ajax_nanny_search_ajax1', 'nanny_search_ajax1');
Thank in advance.
I suspect your pagination links should be ajaxified as well, so you may be targeting them in javascript with smth like $('.prev-page').click(function(){ ... });. If this is your case - your pagination links are added to DOM after the page load and DOM parsing, so your click event is not binded to the target element. To bind javascript events to dynamically created/added DOM elements, use on() or delegate() methods, like $('.prev-page').on('click', function(){ ... }). See docs for details.

yii2: call controller action in sortable jquery with ajax

I am using jquery sortable plugin to drag and drop items and setting their order. But I am unable to get the response from Ajax. I want to call the controller action from the js so that when I drag the item and drop it, then a response should come. The drag and drop functionality is working fine.
I tried this:
My View:
<div class="status-index info">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Status', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<ul class="sortable_status">
<?php
$items = StatusType::find()->orderBy('order')->all();
//var_dump($items);exit;
//$content = array();
foreach ($items as $item) {
echo '<li class="text-items"><label for="item'.$item->order.'"><span class="items-number">'.$item->order.'</span></label>
<label id="item'.$item->order.'" />'.$item->title.'</label>
<br/>
</li>';
}
?>
</ul>
</div>
JS:
$(function () {
var LI_POSITION = 'li_position';
$("ul.sortable_status").sortable({
update: function(event, ui) {
//create the array that hold the positions...
var order = [];
//loop trought each li...
$('.sortable_status li').each( function(e) {
//add each li position to the array...
// the +1 is for make it start from 1 instead of 0
order.push( $(this).attr('id') + '=' + ( $(this).index() + 1 ) );
});
// join the array as single variable...
var positions = order.join(';')
//use the variable as you need!
alert( positions );
// $.cookie( LI_POSITION , positions , { expires: 10 });
}
/*handle : '.handle',
update : function () {
var order = $('.sortable_status').sortable('serialize');
$(".info").load("process-sortable.php?"+order);
} */
});
});
Controller:
public function actionSortable()
{
/* foreach ($_GET['text-items'] as $position => $item)
{
$sql[] = "UPDATE `status_type` SET `order` = $position WHERE `id` = $item";
}*/
if ( isset( $_COOKIE['li_position'] ) ) {
//explode the cockie by ";"...
$lis = explode( ';' , $_COOKIE['li_position'] );
// loop for each "id_#=#" ...
foreach ( $lis as $key => $val ) {
//explode each value found by "="...
$pos = explode( '=' , $val );
//format the result into li...
$li .= '<li id="'.$pos[0].'" >'.$pos[1].'</li>';
}
//display it
echo $li;
// use this for delete the cookie!
// setcookie( 'li_position' , null );
} else {
// no cookie available display default set of lis
echo '
empty
';
}
}
why don't use an ajax call on stop ?
$("ul.sortable_status").sortable({
stop: function(event, ui) {
$.ajax({
type: "POST",
url: myUrl
data: {my-data: "any-value"}
})
.success(function(data) {
//your logic here
})
.fail(function() {
//your logic here
});
}
});
I used to define in my layout on the head tag of my html my variables as following
<head>
<script>
var myUrl = "<?php echo Url::to(['controller/action']); ?>";
</script>
</head>

Resources