jQuery AJAX in Zend Framework

Posted Jul 4th, 2008 by askie in in zend framework

研究一篇老外的文章:在zend framework中使用jquery ,原文见

    http://blog.ekini.net/2007/08/28/jquery-ajax-in-zend-framework/

I have posted a sample code on my wiki on how to submit variables to an action controller. The sample code contains a niffty jQuery plugin - the jQuery Calendar.

Click here for the tutorial.

Update (05-07-2008): Since this post has been getting a lot of traffic and the tutorial to my wiki is not that good, I have decided to update this page and post a simpler and shorter tutorial.

So here it is…

The Javascript will look something like this:

//For Edit Action
f u n c t i o n editnote(notes_id)
{
//a sample loading image thingy
$(‘#notes_entry_id’).prepend("

“);
//Get notes from database - in ZF, notes = controller and getnoteforedit = action
$.post(baseUrl+”/notes/getnoteforedit”,{
notes_id: notes_id
}, function(data){
$(‘#loading’).remove();
obj = window.eval(data);
$(‘#textarea_id’).val(obj[‘notes’]);
}, “json”); //this returns JSON.
}

Then my controller would look something like this:

/**
* Get note for action
*
*/
public function getnoteforeditAction()
{
$this->_helper->layout->disableLayout(); //disable layout
$this->_helper->viewRenderer->setNoRender(); //suppress auto-rendering
//the 2 lines above are very important. this action would return html tags from the layout and will look for a phtml file. we disable the layout and suppress auto-rendering of the phtml view files SO that our JSON will be echoed properly to the Javascript…
require_once(‘models/Notes.php’);
require_once(‘models/UsersNotes.php’);
try {
if (!$this->_request->isPost()) {
throw new Exception(‘Invalid action. Not post.’);
}
$Notes = new Notes();
$data = array();
if ($result = $Notes->fetchRow("notes_id=".$Notes->getAdapter()->quote($_POST[‘notes_id’]).”")->toArray()) {
$data[‘notes_id’] = $result["notes_id"];
$data[‘notes’] = $result["notes"];
$data[‘datetime_posted’] = $result["datetime_posted"];
$data[’status’] = $result["status"];
$json = Zend_Json::encode($data); //basically, $data array will also be available in the JS.
} else {
throw new Exception(‘Note ID not found.’);
}
echo $json; //this will echo JSON to the Javascript
unset($json);
unset($data);
} catch (Exception $e) {
echo $e->getMessage();
}
}

That’s about it… :D
The $data that was encoded using Zend_JSON in the controller/action can now be accessed in the Javascript after you call the eval().

相关文章:

中文关键字:framework jquery zend ajax php vi html javascript zend framework in notes the data this action

WordPress SEO 中文插件 V0.4发布!

Posted Jul 3rd, 2008 by askie in in Wordpress

今天发布WordPress SEO 中文插件 V0.4版本,本版本增加了如下功能:

1. 增加关键词前缀和颜色控制,用户可以自由定义
2. 增加相关文章功能,用户可以定义相关文章前缀、文章数量

关于相关文章功能这里解释一下设计思路:

相关文章是根据pkphp关键建议的结果进行关联的,首先在发布了文章后pkphp关键建议的分词已经存入你的本地数据库了,关于这个分词的设计思路可以参见《Wordpress SEO 中文插件设计细节公布》。取得文章的pkphp关键建议的分词,然后遍历每个关键词,按照文章标题和文章内容查询数据,这样每个关键词能够查出一定数量的文章数组,对所有的关键词的文章数组进行统计,按照文章出现的次数进行排序,由次数多到少取出用户定义数量的文章。

这样的设计能够照顾到每个关键词并能够找出包含关键比较多的文章来,大大增强了文章的相关度!

下载:WordPress SEO 中文插件 V0.4

相关文章:

中文关键字:seo wordpress 关键词 插件 中文 分词 细节 设计 php 0.4 文章 关键 前缀 数多 数量