Surance Center

XOOPS模块开发快速入门中文翻译(五)

    由于这两天一直研究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');
?>

现在快速入门完成了 。希望你觉得有用!

这里是完成的文件下载


Tutorial module
Completed Tutorial

Part 2 - Learn how to use smarty templates

posted @ 2008-04-17 21:43  xxp  阅读(993)  评论(1编辑  收藏  举报
Surance Center