JSON is a very simple, compact, and therefore a widely used format for AJAX applications data exchange. Yii has a few handy ways to work with it. Therefore, let's create a simple application that will show news list and update it every two seconds.
Getting ready
- Create a new application by using the yiic webapp tool.
- Create and setup a new database.
- Add a table named news with at least id, created_on, and title fields:
- Generate a News model using Gii.
CREATE TABLE `news` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `created_on` int(11) unsigned NOT NULL, `title` varchar(255) NOT NULL, PRIMARY KEY (`id`) )
How to do it...
- Create a new controller named protected/controllers/NewsController.php as follows:
- Moreover, create a view named protected/views/news/index.php as follows:
- Now, run the index action of the news controller and try to add a few records into the news database table by running the addRandomNews action. Do not refresh the index page. News added will appear on the index page once every two seconds, as shown in the following screenshot:
render('index'); } public function actionData() { $criteria = new CDbCriteria(); $criteria->order = 'created_on DESC'; $criteria->limit = 10; $news = News::model()->findAll($criteria); echo CJSON::encode($news); } public function actionAddRandomNews() { $news = new News(); $news->title = "Item #" . rand(1, 10000); $news->created_on = date("Y-m-d H:i:s", time()); $news->save(); echo "OK"; } }
Loading…clientScript->registerCoreScript("jquery") ?>
How it works...
The index action does nothing special. It simply renders a view that includes a div container and some JavaScript code. As we are using jQuery, we need to ensure it is included:clientScript->registerCoreScript("jquery")?>Then, we define a function named updateNews and run it every 2,000 milliseconds using the core JavaScript setInterval function. In updateNews, we make an AJAX request to the data action of the same controller, to process the JSON response and replace the current content of placeholder div with formatted data. In actionData, we get the latest news and convert them to JSON format by passing the result to CJSON::encode.
There's more
For further information, refer to the following URLs:
- http://api.jquery.com/category/ajax/
- http://www.yiiframework.com/doc/api/CJSON/
- http://www.yiiframework.com/doc/api/CClientScript/#registerCoreScript-detail
what is the application of clientScript->registerCoreScript("jquery") ?>
ReplyDeletehttps://bayanlarsitesi.com/
ReplyDeleteManisa
Denizli
Malatya
Çankırı
PYQDWC
Antalya
ReplyDeleteKonya
Adana
Ankara
Van
NJH5Mİ
Malatya
ReplyDeleteKırıkkale
Aksaray
Bitlis
Manisa
DWZB8
Antalya
ReplyDeleteAntep
Burdur
Sakarya
istanbul
A37CUC
whatsapp görüntülü show
ReplyDeleteücretli.show
3ED0QZ
https://titandijital.com.tr/
ReplyDeleteamasya parça eşya taşıma
adıyaman parça eşya taşıma
hatay parça eşya taşıma
giresun parça eşya taşıma
SRHRMA
Antep Lojistik
ReplyDeleteYalova Lojistik
Erzincan Lojistik
Tekirdağ Lojistik
Elazığ Lojistik
DQT
FA40C
ReplyDeleteBayburt Parça Eşya Taşıma
Çorlu Lojistik
Bingöl Şehirler Arası Nakliyat
Çerkezköy Fayans Ustası
Afyon Şehir İçi Nakliyat
Ünye Asma Tavan
Düzce Evden Eve Nakliyat
Huobi Güvenilir mi
Ünye Evden Eve Nakliyat
44DFC
ReplyDeleteKaraman Evden Eve Nakliyat
Ağrı Evden Eve Nakliyat
Hatay Evden Eve Nakliyat
Gümüşhane Evden Eve Nakliyat
buy peptides
masteron for sale
for sale dianabol methandienone
Çankırı Evden Eve Nakliyat
order primobolan
38537
ReplyDeletereferanskodunedir.com.tr
C8903
ReplyDeletebinance referans kodu
CDF19
ReplyDeletegörüntülü sohbet siteleri ücretsiz
en iyi sesli sohbet uygulamaları
Niğde Sesli Sohbet Uygulamaları
mardin random görüntülü sohbet
niğde canlı görüntülü sohbet siteleri
gümüşhane görüntülü sohbet yabancı
Afyon Sesli Sohbet
kadınlarla ücretsiz sohbet
Balıkesir Ücretsiz Görüntülü Sohbet
1C220
ReplyDeleteSatoshi Coin Hangi Borsada
Bitcoin Mining Nasıl Yapılır
Fuckelon Coin Hangi Borsada
Ergo Coin Hangi Borsada
Btcturk Borsası Güvenilir mi
Mexc Borsası Kimin
Shinja Coin Hangi Borsada
Bonk Coin Hangi Borsada
Binance Nasıl Oynanır