Yii widget reload via ajax - php

I need to reload a content of my Yii widget via AJAX every XX seconds.
This is my widget code:
class UserOnlineWidget extends CWidget
{
public function init(){
}
public function run(){
$userOnline=User::getOnlineUser();
$this->render("userOnLineWidget", array('userOnline'=>$userOnline));
}
}
And this is the view userOnLineWidget.php:
<div class="userOnline">
<h5>User Online</h5>
<ul>
<?php
foreach($userOnline as $user) {
?>
<li>
<ul>
<li><?php echo CHtml::link($user['username'], array('/site/view/'.$user['id'])); ?></li>
<li><?php echo ($user['online'] != null)? 'online' : 'offline'; ?></li>
</ul>
</li>
<?php
}
?>
</ul>
</div>
How can I do this ? I use Yii 1.1.

Here a small example. I hope this help you. Widget which show time from server every second.
//SiteController
public function actionTest()
{
$this->render('my_view'); //just rendering view
}
public function actionContent()
{
$this->widget('time'); //return html of widget. use for ajax
}
//my_view.php
<script src="<?php echo $this->assetsBase ?>/js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function() {
$.get('/site/content', {}, function(data) {
$('#widget').html(data);
});
}, 1000); //update widget content every second
});
</script>
<div id="widget">
<?php $this->widget('time'); // render widget ?>
</div>
//widget
class Time extends CWidget
{
public function init(){
}
public function run(){
$date = new DateTime();
$this->render("test", array('time'=>$date->format('H:i:s')));
}
}
//view for widget test.php
<p><?= $time ?></p>

Related

undefined variable: id while clicking a dynamic image in homepage using CodeIgniter

Controller:
public function latestnews()
{
$data['news'] = $this->New_model->getById($id);
$this->load->view('news',$data);
}
Model:
public function getById($id)
{
return $this->db->get_where($this->_table, ["new_id" => $id])->row();
}
View:
<?php
if (isset($news) and $news) {
foreach($news as $new) {
?>
<div class="col-sm-12">
<div class="section">
<img src="<?php echo site_url('uploads/'.$new->image); ?>" />
</div>
<p><?php echo $new->description;?> </p>
</div>
<?php
}
}
?>
How and where to define the variable id?
While clicking a dynamic image,it should be opened in another page containing details but instead showing Undefined variable: id
In the latestnews() function you have no $id defined.
Try to call the latestnews() function with the right $id parameter.
you can write function like this,
function latestnews($id)
{
Controller:
function single_news($id) { // This $id variable comes from URL Ex: example.com/single_news/15. So $id = 15
$this->data['news_data'] = $this->new_model->getNewsByID($id);
$this->load->view('single-news', $this->data); // Create New View file named single-news.php
}
Model:
function getNewsByID($id) {
$this->db->where('id', $id);
$q = $this->db->get('news');
if($q->num_rows() > 0) {
return $q->row();
}
return false;
}
View: (Single News Page)
<div>
<img src="<?php echo site_url('uploads/'.$news_data->image); ?>" />
</div>

How to plugin anchor tag with jquery

I have an existing anchor tag - how do I add a call to a JavaScript function to it, which will run when clicked? I have created pagination dynamically, and would like to call a JavaScript function in the href of the anchor, to both run the function and go to a new page.
Below is an example of the page I would like to display.
This is my html code:
<nav aria-label="Page navigation">
<ul class="pagination" id="pagination">
<?php for ($b=1; $b <= $a ; $b++) {?>
<li>
<a class="anchorClass" id="anch" href="<?php echo base_url('attandance/staff_time_depart/'.$depart.'/'.$date_from.'/'.$date_to.'/'.$b) ?>"> <?php echo $b ?> </a>
</li>
<?php } ?>
</ul>
</nav>
This is my JavaScript code:
<script type="text/javascript">
var b = '<?php echo $b; ?>';
var a_href = $("#anch").attr("href");
$(function () {
window.pagObj = $('#pagination').twbsPagination({
totalPages: b,
visiblePages: 5,
href: 'true',
// anchorClass: a_herf,
onPageClick: function (event, page) {
console.info(page + ' (from options)');
}
}).on('page', function (event, page) {
console.info(page + ' (from event listening)');
});
});
</script>

Can't load index.php in views

I can't load index.php file in view folder. The error displayed is
"Unable to load the requested file: calendar/index.php"
Here is the code
controller(Calendar.php):
*public function __construct() {
Parent::__construct();
$this->load->model("calendar_model");
}
public function index()
{
$this->load->view("calendar/index.php", array());
}*
model(Calendar_model.php):
*?php
class Calendar_Model extends CI_Model
{
}
?>*
view (index.php):
*<div class="container">
<div class="row">
<div class="col-md-12">
<h1>Calendar</h1>
<div id="calendar">
<script type="text/javascript">
$(document).ready(function() {
$('#calendar').fullCalendar({
});
});
</script>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
});
</script>*
Better rename your index.php like V_index.php
for this case remove .php from your code
public function __construct()
{
Parent::__construct();
$this->load->model("calendar_model");
}
public function index()
{
$this->load->view("calendar/index", array());
}

On AJAX call session is destroying WordPress plugin

I am calling an AJAX function from WordPress plugin through admin-ajax.php. But when the request is send, its destroying the session.
I have an array where data is saved on every request, but on every request previous data is destroyed.
AJAX code i am calling:
function cartbox()
{
var url = {
'action':'my_cart_action',
'rnd':Math.random(),
};
$.ajax({
type:'POST',
data:url,
url: "http://localhost/vmlogic/wp-admin/admin-ajax.php",
success : function(response)
{
$("#cartdiv").html(response);
}
});
}
On plugin index file i am calling this:
class cloud_calculator{
// Call a cation on link click
function cloud_calculator()
{
add_action('init' , array($this,'cloud_init'));
}
public function cloud_init()
{
add_action('wp_head' , array($this, 'plugin_jquery_func'));
add_action('admin_menu', array($this,'my_menu'));
add_shortcode('cloud_calculator', array($this,'my_calculator'));
add_action('the_content', array($this,'the_content_action'));
add_action( 'wp_ajax_my_unique_action', array($this,'my_unique_action_callback'));
add_action( 'wp_ajax_nopriv_my_unique_action', array($this,'my_unique_action_callback'));
add_action( 'wp_ajax_my_cart_action', array($this,'my_cart_action_callback'));
add_action( 'wp_ajax_nopriv_my_cart_action', array($this,'my_cart_action_callback'));
}
public function plugin_jquery_func()
{
$plugins_url = plugins_url().'/cloud-calculaor/';
?>
<script type="application/javascript">
var PluginUrlJs = '<?php echo $plugins_url ;?>';
</script>
<?php }
public function the_content_action($content)
{
$content = str_replace('[cloud_calculator]',do_shortcode('[cloud_calculator]') ,$content );
return $content;
}
// Menu call function
public function my_menu() {
add_menu_page('Cloud Calculator', 'Cloud Calculator', 'manage_options', 'cloud-calculaor/cloud-calculaor.php', array($this, 'my_function'));
}
// action to call on admin menu
public function my_function() {
wp_enqueue_style('style', '/wp-content/plugins/cloud-calculaor/style/admincalculator.css');
echo
'<div class="wrap">
<h2>Cloud Calculator</h2>
<div class="plugincode">
<div style=" padding:10px 10px 10px 50px;">
<p style="margin:5px 0px">
To Insert <strong>CLOUD CALCULATOR</strong> shortcode inside a page or post:<br>
<br>
Use: <code><strong>[cloud_calculator]</strong></code><br>
<br>
For more queries contact the administrator!<br>
</p>
</div>
</div>
</div>
';
}

codeigniter ajax load table

I have write a some codeigniter code, when I click button add a data, and the table will ajax reload, not reload the page. How to modify my code? Thanks
This is my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Big_category extends CI_Controller {
function __construct() {
...
}
function index() {
if($this->session->userdata('logged_in')) {
$this->page(0);
} else {
show_404('page');
}
}
function page($page) {
....
$data['pagelist']=$this->pagination->create_links();
$data["main_content"] = "big_category_view";
$this->load->view('template', $data);
}
This is my model
class Big_category_model extends CI_Model
{
...
function get_big_category($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->get_where('category1', array('c1_status' => 1)); //SELECT * FROM category1
return json_encode($query->result());
}
This is my view
...
<? $data = json_decode($all_big_category); ?>
<? foreach($data as $key => $value): ?>
<tr class="modify_tr">
<td><?=($key+1)?></td>
<td id="td_id_<?=$value->c1_id?>" class="modify_td">
<span id="c1_id_<?=$value->c1_id?>"><?=$value->c1_name?></span>
<form class="form-search td_id_<?=$value->c1_id?>">
<input type="text" name="input_c1_id" id="input_c1_id_<?=$value->c1_id?>">
<button class="btn modify" id="button_c1_id_<?=$value->c1_id?>" c1-id="<?=$value->c1_id?>" type="button"><i class="icon-edit"></i></button></td>
</form>
<td><button class="btn btn-danger"><i class="icon-remove-sign"></i></button></td>
</tr>
<? endforeach ?>
</table>
<?=$pagelist?>
...
Here is a simple technique of how you can achieve this.
Layout(Template)
<html>
<body>
<div><!--other stuff here--></div>
<div id = 'form_id'>
<?php echo $content?>
</div>
</body>
</html>
Controller
function index()
{
if($this->input->is_ajax_request()){
//load form here
$this->load->view('form');
}else{
//this will load the form for the first time
//pass third parameter as TRUE so it will returned as string
//assigned to index content which will be displayed in layout
$data['content'] = $this->load->view('form',$data , TRUE);
//load template here
$this->load->view('template', $data);
}
}
JQuery
$.ajax({
type : 'POST',
url : '<?php echo site_url('controllername/index')?>',
data : '' // query string
success : function(formagain){
$('#form_id').html(formagain);
//this will replace the content of div with new form
}
});

Categories