2008年8月16日
#
原文地址:http://www.xoops.org/modules/mediawiki/index.php?title=Dev:search_function&curid=355&oldid=1067
注意:我是一个新的xoops开发者,请时刻留意这一点
记录一下我开发myguestBook模块时候学到的搜索功能
1. 第一,向xoops_version.php文件加入以下的代码
Search $modversion['hasSearch'] = 1;
$modversion['search']['file'] = "include/search.inc.php";
$modversion['search']['func'] = "guestbook_search";
2.第二,按照以上的设定添加文件,并写代码:
=====
翻译者:Surance Yin (Suranceyin@yahoo.com.cn)
©:http://www.fltek.com.cn
=====

Code
<?php
// 这些代码是从newbb借鉴的
// 函数名是在xoops_version.php里面定义的
// 加入参数 "$queryarray, $andor, $limit, $offset, $userid"
// 注意: $andor 默认值为 AND, 可以设置为 'AND', 'OR' or 'exact', 取决于高级搜索界面的三个选项。你的搜索逻辑必须考虑以上的三个值
function guestbook_search($queryarray, $andor, $limit, $offset, $userid){
global $xoopsDB;
// 开始构造sql语句
$sql = "SELECT id,name,title,message,time FROM ".$xoopsDB->prefix("myguestbook")."";
// 如果$queryarray不是一个数组,count()函数也会返回1. 因此要使用is_array()检查是否是数组
if ( is_array($queryarray) && $count = count($queryarray) ) {
$sql .= " WHERE ((name LIKE '%$queryarray[0]%' OR title LIKE '%$queryarray[0]%' OR message LIKE
%$queryarray[0]%')";
for($i=1;$i<$count;$i++){
$sql .= " $andor ";
$sql .= "(name LIKE '%$queryarray[$i]%' OR title LIKE '%$queryarray[$i]%' OR message LIKE
'%$queryarray[$i]%')";
}
$sql .= ") ";
} // end if
$sql .= "ORDER BY id DESC";
// 因为Gusetbook的index.php显示了我需要知道的实体
// 既然用index.php代码
$query = $xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix("myguestbook")." WHERE id>0");
list($numrows) = $xoopsDB->fetchrow($query);
// 假设sql获取到数据
$result = $xoopsDB->query($sql,$limit,$offset);
$ret = array();
$i = 0;
// 使用搜索结果创建到query的连接对象
while($myrow = $xoopsDB->fetchArray($result)){
// 你可以用任何图片
$ret[$i]['image'] = "images/url.gif";
// 设置结果查看明细的信息
$ret[$i]['link'] = "index.php?start=".($numrows-$myrow['id']);
$ret[$i]['title'] = $myrow['name'];
$ret[$i]['time'] = $myrow['time'];
// guestbook中,id没有用
$ret[$i]['uid'] = "";
$i++;
}
return $ret;
}
// 注意: 象上面声明的一样, $ret 是一个多维数组,这个多维数组的第一维是一个顺序号,第二维是图片、连接、标题、uid和时间。这些属性指向搜索到的一个实体。 image, uid and time 是可选的,time 是一个unix的标准时间戳
// 参见xoops中的 search.php . 路径为: kernel/module.php 调用了各个模块的搜索方法?>
2008年5月25日
#
标题:最轻量级的ORM(含实例)
下载地址:http://www.cnblogs.com/Files/xxpyeippx/EasyDBOperationV3.0.rar
作者介绍:
Surance Yin
个人主页: www.fltek.com.cn
Cnblogs: www.cnblogs.com/xxpyieppx/
邮箱:suranceyin@yahoo.com.cn
基本信息:
类型:类库
协议:GPL
环境:.net framework 2.0
数据库:Sqlserver 2000/2005
最好配合CodeSurance一起使用
特性:
1.最轻量级的ORM。
2.实现了字符串过滤
3.支持分页(不需要存储过程)
升级记录(2.0-3.0)
增加了字符串过滤
增机阿拉分页支持
额外说明:
1.最好结合aspnetPager
2.ConsoleApplicationTest里面包含了使用范例
到底有多简单:
p.Add();
p.Edit();
manager3.Delete();
ProductInfo info = (ProductInfo)manager2.GetOne((object)id);
操作过程:
1.生成实体类(可以使用CodeSurance来生成)
2.继承CommonOperation,建立对操作类。(主要是要指定表名)
3.在客户端调用操作类即可。
2008年4月19日
#
由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。
看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。
应该是我看的太快了,要学而时习之啊。因此翻译在这里。
==============
作者:Surance Yin
邮箱:Suranceyin@yahoo.com.cn
主页:http://www.fltek.com.cn
=================
第八章――数据库操作和类
CRUD 增删改查
数据库操作有4个基本功能,分别是: Create, Read, Update, Delete; 缩写为 CRUD. 之前我们演示了增加和读取,现在我们来演示一下.
Create 增加
|
$query="Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";
$res=$xoopsDB->query($query);
if(!$res){
echo"error: $query";
}
|
Read 读取
|
$query=$xoopsDB->query(' SELECT * FROM '.$xoopsDB->prefix('tutorial_myform')" WHERE id='$myid' ");
$myrow=$xoopsDB->fetchArray($query);
$name=$myrow['name'];
$ad
|
由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。
看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。
应该是我看的太快了,要学而时习之啊。因此翻译在这里。
==============
作者:Surance Yin
邮箱:Suranceyin@yahoo.com.cn
主页:http://www.fltek.com.cn
=================
第四章――xoops区块
Part 1 Building a simple module
Part 2 - using Smarty templates in a module
Part 3 - Building an AJAX module
Xoops中,区块是一个非常重要的概念, 不过开发区块并不是很容易,尤其是options,所以我写了这个教程。
一个xoops区块包含2部分:
1- 一个php 函数,用来控制区块内容
2- 一个smarty模板,用来控制区块的显示.
以下是 xoops_version.php 中的定义,我们从这里讲起
第一步- 设置 xoops_version.php
打开 tutorial/xoops_version.php. 在?>之前输入:
|
// Blocks
$modversion['blocks'][1]['file'] = "tutorial_block.php";
$modversion['blocks'][1]['name'] = 'Block for Tutorial';
$modversion['blocks'][1]['description'] = 'This is a Block for the tutorial module';
$modversion['blocks'][1]['show_func'] = "tut_blockList";
$modversion['blocks'][1]['template'] = 'tutorial_block.html';
|
$modversion['blocks'][1]['file'] = "tutorial_block.php";第一行告诉xoops 那个文件来控制区块内容. 1个文件可以有很多个函数。我习惯把所有的函数都放在同一个文件里面,除非函数太庞大了。这样的话,可以很方便的通过文件来找函数。
$modversion['blocks'][1]['name'] = 'Block for Tutorial';这里是区块的名称。最好采用语言文件来定义这些东西,不过为了简单起见,我直接写在这里了。
$modversion['blocks'][1]['description'] = 'This is a Block for the tutorial module';非常容易明白,这里是区块的描述。会显示在区块管理界面的。
$modversion['blocks'][1]['show_func'] = "tut_blockList";这里是用来控制区块的内容的函数名称。我的经验是,尽量在函数名称前面,加上区块名称前缀,这样不会和xoops自带的函数混淆。我以前出现过这样的问题,你也要提起注意哦.
$modversion['blocks'][1]['template'] = 'tutorial_block.html';这里是区块用到的 smarty 模板。
小贴士
当创建一个模块的时候, xoops_version.php中的数字都是这样的:
|
// Templates
$modversion['templates'][1]['file'] = 'tut_form.html';
$modversion['templates'][1]['description'] = '';
$modversion['templates'][2]['file'] = 'tut_client_list.html';
$modversion['templates'][2]['description'] = '';
$modversion['templates'][3]['file'] = 'tut_main.html';
$modversion['templates'][3]['description'] = '';
|
如果只有两三个模板,数字的控制还相对容易,但是如果有40多个的时候,控制数字就成了一个非常繁琐的工作。怎么解决这个问题呢?很简单,使用一个计数变量。
|
// Templates
$i=1;
$modversion['templates'][$i]['file'] = 'tut_form.html';
$modversion['templates'][$i]['description'] = '';
$i++;
$modversion['templates'][$i]['file'] = 'tut_client_list.html';
$modversion['templates'][$i]['description'] = '';
$i++;
$modversion['templates'][$i]['file'] = 'tut_main.html';
$modversion['templates'][$i]['description'] = '';
$i++;
|
这样就可以很方便的创建模板了,不用担心序号了。因为变量$i负责计数。它从1开始,然后每次加1
第二步- 创建必须的文件
创建2个文件夹:
tutorial/blocks
tutorial/templates/blocks
在tutorial/blocks中,创建一个文件tutorial_block.php ,输入以下代码:
|
<?php
function tut_blockList(){
$block=array();
$block['mytext']="Hello world! This is my new Block!";
return $block;
}
?>
|
注意事项:
- 这个函数必须返回一个名为 $block的变量
- $block必须为一个数组.
- 可以向这个数组内放任何东西,包括另外一个关联数组。
- 区块可以在模块之外的地方调用,因此如果你包含了其他的文件,务必保证区块可以知道被包含文件的确切位置。
在 tutorial/templates/blocks创建一个文件tutorial_block.html ,输入以下代码
重新安装一下这个模块,就可以在区块管理页面中看到 'Block for Tutorial'这个区块了,使它可见,然后到首页去看一下效果。.
恭喜你,已经创建了第一个区块!
第三步- 创建options(参数)
这个机制可以让我们创建更加灵活的区块。
打开 tutorial/xoops_version.php输入如下红色的内容:
|
// Blocks
$modversion['blocks'][1]['file'] = "tutorial_block.php";
$modversion['blocks'][1]['name'] = 'Block for Tutorial';
$modversion['blocks'][1]['description'] = 'This is a Block for the tutorial module';
$modversion['blocks'][1]['show_func'] = "tut_blockList";
$modversion['blocks'][1]['template'] = 'tutorial_block.html';
$modversion['blocks'][1]['edit_func'] = "tut_blockList_edit";
$modversion['blocks'][1]['options'] = 'Hello|1';
|
第一行告诉Xoops 区块的编辑函数是什么。这个函数必须也在tutorial_block.php中。第二行指定了参数默认值。这个区块中,有2个 options, 默认值分别为 "Hello" 和 "1". 采用'|'分隔.
打开 tutorial/blocks/tutorial_block.php
Replace all code with this:
|
<?php
function tut_blockList($options){
$block=array();
$block['textOne']=$options[0];
$block['textTwo']=$options[1];
return $block;
}
function tut_blockList_edit($options){
$form = "Option 1: <input type='text' size='9' name='options[0]' value='$options[0]' />";
$form .= "<br />";
$form .= "Option 2: <input type='text' size='1' name='options[1]' value='$options[1]' />";
$form .= "<br />";
return $form;
}
?>
|
现在创建了一个新的函数 (tut_blockList_edit) ,这个函数用来设置区块。函数的声明表明 tut_blockList 它接受xoops传过来的参数 ($options)
注意事项
- 这个函数必须返回一个叫做$form的变量.
- $form必须是一个字符串,而不是数组。
- 可以有多个参数。
- Xoops 会自行存储这些参数到xoops的数据库,所以你不用担心保存、更新这些参数的值。
- $form中必须有参数命名的表单内容.
- options(参数)必须从0开始,比如 name='options[0]'.
打开 tutorial/templates/blocks/tutorial_block.html输入
|
<p>My First option is: <{$block.textOne}></p>
<p>My Second option is: <{$block.textTwo}></p>
|
好,为了让参数正常运行,我们需要重新安装这个模块。
重新安装后,设置区块可见,这首页,可以看到2个参数,一个是'Hello' ,另一个是 '1'. 从后台改变这2个参数,会发现首页也会跟着改变
哈!成功了!
Part 5 - Guidlines for Module Development
摘要: 由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。应该是我看的太快了,要学而时习之啊。因此翻译在这里。==============作者:Surance Yin邮箱:Suranceyin@yahoo.com.cn主页:http://www.fltek.com.cn=================第六步- 插入...
阅读全文
2008年4月18日
#
由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。
看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。
应该是我看的太快了,要学而时习之啊。因此翻译在这里。
==============
作者:Surance Yin
邮箱:Suranceyin@yahoo.com.cn
主页:http://www.fltek.com.cn
=================
第三章 ――建立一个具有Ajax功能的模块
Part 1 Building a simple module
Part 2 - using Smarty templates in a module
这一章,我会教会你怎么在模块里面用ajax。范例里面使用的是xajax. 当然,还有很多其他的ajax的工具,比如说主要由php实现的xajax, sajax 等. 还有一些是纯javascript脚本的,比如说scriptaculous. 当你看完这个教程的时候,可以大胆的尝试其他的ajax库。呵呵,我为什么会用Xajax呢?因为我比较熟悉php的编程,所以我觉得xajax更加灵活。
ajax是什么? 就是不用刷新网页的情况下更改网页的部分显示。这个功能很有用;尤其当要降低服务器负载的时候.
什么时候用ajax呢?举例说明:当你有一个数据表的时候,可以用ajax来存数据库.注意:ajax并不是万能的.这只是一个改善用户体验的工具而已。
开始吧!
第一步:下载模块文件
|
下载并安装这个文件。这里用的是第二章做好的文件。不同的是增加了一个class文件夹,文件夹里面有一个xajax子文件夹,里面放置了xajax的文件 (version 0.5 beta 4b)。另外,还要将 index.php重命名为index_from_part2.php 并创建一个新的 index file.
|
Tutorial module from part 2 with xajax files added
tutorial
|
第二步:使用xajax
这里是新版本的 index.php:
|
<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
require_once(XOOPS_ROOT_PATH.'/modules/tutorial/functions.php');
require_once(XOOPS_ROOT_PATH.'/modules/tutorial/class/xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->registerFunction("processFormData");
//$xajax->setFlag("debug", true);
$xajax->processRequest();
$Xjavapath=XOOPS_URL.'/modules/tutorial/class/xajax';
$xajaxjava=$xajax->getJavascript($Xjavapath);
$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');
$xoopsTpl->assign('xajaxjava', $xajaxjava);
require(XOOPS_ROOT_PATH.'/footer.php');
?>
|
让我们一步一步来解释:
|
require_once(XOOPS_ROOT_PATH.'/modules/tutorial/functions.php');
require_once(XOOPS_ROOT_PATH.'/modules/tutorial/class/xajax/xajax_core/xajax.inc.php');
|
第一行引用了functions文件。这个文件里面有所有我的 xajax 和其他方法。在开始使用 xajax之前,可以创建2个文件,一个储存ajax方法,一个储存其他方法。第二行引用了xajax 的必须文件.
|
$xajax = new xajax();
$xajax->registerFunction("processFormData");
//$xajax->setFlag("debug", true);
$xajax->processRequest();
|
第一行创建了一个xajax对象。使用xajax之前,要明确一点:所有要用到的方法都要先注册给xajax。这就是第二行做的事情: $xajax->registerFunction("processFormData");.我会在解释functions.php时候再次说明的.第三行会给出一个输出,可以用来调试 xajax. 第四行启动xajax的功能. 注意:如果要在xoops里面使用xajax,以上代码的顺序是不能变化的。比如,如果将$xajax->processRequest();写在 require(XOOPS_ROOT_PATH.'/header.php');后面就不行了。为什么?因为xajax必须在没有html输出之前调用processRequest()方法, 而 XOOPS_ROOT_PATH.'/header.php具有一些html输出.
|
$Xjavapath=XOOPS_URL.'/modules/tutorial/class/xajax';
$xajaxjava=$xajax->getJavascript($Xjavapath);
|
xajax 的运行需要一个javascript 脚本文件。 因为要使用smarty,所以只能用这种方法来包含javascript文件了。第一行将xajax的根目录地址赋给变量$Xjavapath.然后再通过xajax内置的方法获取他所有的javascript脚本,并赋给变量$xajaxjava.
|
$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');
$xoopsTpl->assign('xajaxjava', $xajaxjava);
|
就像上一章讲到的,第一行指定了模板文件。第二行输出头部信息。第三行将xajax的脚本引用发给smarty模板。
|
require(XOOPS_ROOT_PATH.'/footer.php');
?>
|
这句上一章解释过了。
第三步- Functions.php
打开 tutorial/functions.php 写入以下代码:
|
<?php
function processFormData($arg)
{
// do some stuff based on $arg like query data from a database and
// put it into a variable like $newContent
$newContent = "Value of field: ".$arg['myfield'];
// Instantiate the xajaxResponse object
$objResponse = new xajaxResponse();
// add a command to the response to assign the innerHTML attribute of
// the element with id="SomeElementId" to whatever the new content is
$objResponse->assign("thisID","innerHTML", $newContent);
//return the xajaxResponse object
return $objResponse;
}
?>
|
function processFormData($arg)创建了一个函数processFormData. 这个函数接收参数$arg. 表单的所有内容会存放在 $arg 数组里面。
$newContent = "Value of field: ".$arg['myfield'];创建变量来接收$arg里面的一个信息.
$objResponse = new xajaxResponse(); xajax开始输出。当你的代码都写完后,就可以调用这个方法了。
$objResponse->assign("thisID","innerHTML", $newContent); 这里将变量$newContent赋给一个ID为“thisID”的控件的“innerHTML”属性。这个控件是类似于这样的: <div id="thisID">而不是 <div class="thisID">.
重点::虽然可以赋任何值给这个控件,不过不能做任何html输出。如果输入 print_r($arg), 这样的语句,xajax就会出错了。
重点::想要调试xajax的时候,取消index.php.中这句的注释 $xajax->setFlag("debug", true);
return $objResponse;结束函数,返回变量.
好,现在xajax搞定了,我们来看一下如何写模板文件
第四步- Tut_main.html 模板
打开 /tutorial/templates/tut_main.html.写入以下代码:
|
<{$xajaxjava}>
<form id="myform" name="myform" method="post" action="index.php">
<input type="text" name="myfield" id="myfield" />
<input type="button" value="Calculate" onclick="xajax_processFormData(xajax.getFormValues('myform'));" />
</form>
<div id="thisID"></div>
|
<{$xajaxjava}>这里会引用所有需要的javascript文件,放在模板的第一行。
<form id="myform" name="myform" method="post" action="index.php">这一行建立了名为 myform的表单 。表单的名称非常重要。
<input type="text" name="myfield" id="myfield" />创建了一个叫做myfield.的文本域
<input type="button" value="Calculate" onclick="xajax_processFormData(xajax.getFormValues('myform'));" />现在创建了一个向xajax提交数据的按钮.当用户点这个按钮后, 'myform'会向 xajax 的函数 processFormData发送数据。
注意: 当创建xajax函数的时候,方法名为processFormData但是在html文件中,函数名为: xajax_processFormData.
</form>结束表单.
注意: IE浏览器不允许嵌套表单,而firefox允许。嵌套表单的意思是一个form里面有另外一个form。当使用ajax这一点很重要,但是因为IE不支持,只能另寻它法了。 经常使用 xajax时,这个问题会显露出来。解决方案就是创建一个Form之前,关闭旧的Form。
<div id="thisID"></div>这就是xajax输出的地方。
第五步- 测试
重点: 在测试之前,请保证xoops的debugging 是 OFF的。 不然Xajax 会出错。Why? 因为xajax运行之前,不能有任何html输出。我们可以开始测试了。打开tutorial的主页面, 可以看到一个文本框和一个按钮。在文本框中输入 xajax is cool,按下按钮。可以看到输出: Value of field: xajax is cool.id为thisID的标签<div>负责更新数据。很酷吧!
2008年4月17日
#
摘要: 由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。应该是我看的太快了,要学而时习之啊。因此翻译在这里。==============作者:Surance Yin邮箱:Suranceyin@yahoo.com.cn主页:http://www.fltek.com.cn=================第二章――使用...
阅读全文
由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。
看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。
应该是我看的太快了,要学而时习之啊。因此翻译在这里。
==============
作者:Surance Yin
邮箱:Suranceyin@yahoo.com.cn
主页:http://www.fltek.com.cn
=================
显示数据库记录
现在我们来创建一个按钮,点击后显示表中的所有数据。
|
<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');
if (isset($_POST['listall'])){
echo '<table width="100" border="0">
<tr>
<td bgcolor="#99cc99">Name</td>
<td bgcolor="#66cc99">Address</td>
<td bgcolor="#99cc99">Telephone</td>
<td bgcolor="#66cc99">Email</td>
</tr>';
$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$name = $myrow['name'];
$address = $myrow['address'];
$telephone = $myrow['telephone'];
$email = $myrow['email'];
echo '<tr><td bgcolor="#99cc99">'.$name.'</td><td bgcolor="#66cc99">'.$address.'</td><td bgcolor="#99cc99">'.$telephone.'</td><td bgcolor="#66cc99">'.$email.'</td></tr>';
}
echo '</table>';
}
if (isset($_POST['submit'])){
if (empty($_POST['name'])){
echo 'please fill in a name';
} else {
$name=$_POST['name'];
$address=$_POST['address'];
$tel=$_POST['tel'];
$email=$_POST['email'];
$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";
$res=$xoopsDB->query($query);
if(!$res) {
echo "error: $query";
} else {
echo "Data was correctly inserted into DB!";
}
}
}
?>
<form name="tutorial_form" method="post" action="index.php">
<table width="400" border="0">
<tr>
<td align="right">Name</td>
<td><input type="text" name="name"></td>
</tr><tr>
<td align="right">Address</td>
<td><input type="text" name="address"></td>
</tr><tr>
<td align="right">Telephone</td>
<td><input type="text" name="tel"></td>
</tr><tr>
<td align="right">Email</td>
<td><input type="text" name="email"></td>
</tr><tr>
<td><input type="submit" name="listall" value="List All"></td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
<?php
require(XOOPS_ROOT_PATH.'/footer.php');
?>
|
有个变化:一个是建立了一个新的按钮,叫做listall,当按下后会列出所有数据。第二是创建了一个If语句来检查按钮是否被按下
还有一个要做的事情,就是在语言文件中增加常量。打开/language/english中的main.php,增加以下代码:
|
<?php
define('TT_NAME','Name');
define('TT_EMAIL','Email');
define('TT_ADDRESS','Address');
define('TT_TELEPHONE','Telephone');
?>
|
这里是index.php的最终代码:
|
<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');
if (isset($_POST['listall'])){
echo '<table width="100" border="0">
<tr>
<td bgcolor="#99cc99">'.TT_NAME.'</td>
<td bgcolor="#66cc99">'.TT_ADDRESS.'</td>
<td bgcolor="#99cc99">'.TT_TELEPHONE.'</td>
<td bgcolor="#66cc99">'.TT_EMAIL.'</td>
</tr>';
$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$name = $myrow['name'];
$address = $myrow['address'];
$telephone = $myrow['telephone'];
$email = $myrow['email'];
echo '<tr><td bgcolor="#99cc99">'.$name.'</td><td bgcolor="#66cc99">'.$address.'</td><td bgcolor="#99cc99">'.$telephone.'</td><td bgcolor="#66cc99">'.$email.'</td></tr>';
}
echo '</table>';
}
if (isset($_POST['submit'])){
if (empty($_POST['name'])){
echo 'please fill in a name';
} else {
$name=$_POST['name'];
$address=$_POST['address'];
$tel=$_POST['tel'];
$email=$_POST['email'];
$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";
$res=$xoopsDB->query($query);
if(!$res) {
echo "error: $query";
} else {
echo "Data was correctly inserted into DB!";
}
}
}
?>
<form name="tutorial_form" method="post" action="index.php">
<table width="400" border="0">
<tr>
<td align="right"><?php echo TT_NAME; ?></td>
<td><input type="text" name="name"></td>
</tr><tr>
<td align="right"><?php echo TT_ADDRESS; ?></td>
<td><input type="text" name="address"></td>
</tr><tr>
<td align="right"><?php echo TT_TELEPHONE; ?></td>
<td><input type="text" name="tel"></td>
</tr><tr>
<td align="right"><?php echo TT_EMAIL; ?></td>
<td><input type="text" name="email"></td>
</tr><tr>
<td><input type="submit" name="listall" value="List All"></td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
<?php
require(XOOPS_ROOT_PATH.'/footer.php');
?>
|
现在快速入门完成了 。希望你觉得有用!
Part 2 - Learn how to use smarty templates
由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。
看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。
应该是我看的太快了,要学而时习之啊。因此翻译在这里。
==============
作者:Surance Yin
邮箱:Suranceyin@yahoo.com.cn
主页:http://www.fltek.com.cn
=================
使用表单和数据库
现在我们在ndex.php里面制作一个表单
|
<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');
?>
<form name="tutorial_form" method="post" action="index.php">
<table width="400" border="0">
<tr>
<td align="right">Name</td>
<td><input type="text" name="name"></td>
</tr><tr>
<td align="right">Address</td>
<td><input type="text" name="address"></td>
</tr><tr>
<td align="right">Telephone</td>
<td><input type="text" name="tel"></td>
</tr><tr>
<td align="right">Email</td>
<td><input type="text" name="email"></td>
</tr><tr>
<td> </td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
<?php
require(XOOPS_ROOT_PATH.'/footer.php');
?>
|
现在在主菜单点击tutorial ,可以看到一个含有 name, address, telephone 和email 、submit按钮的表单。点击“Submit”会回到index.php不过不会做什么数据库操作。现在我们来加上以下的代码:
|
<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');
require(XOOPS_ROOT_PATH.'/header.php');
if (isset($_POST['submit'])){
echo 'my name is: '. $_POST['name'];
}
?>
|
我现在只显示了上面一部分代码,这样看起来少一点。.
现在,如果点击submit你会看到"my name is: " 后面是你的名字。
这里讲解了PHP的基本语法,略过。