第一步:
比如我要对后台mysql数据库的contents表进行操作。
首先在控制器中建立一个 contents_controller 文件,代码如下:
<?php
/*
* Created on 2008-11-25
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
class ContentsController extends AppController
{
var $scaffold;
function view()
{
$exec="select * from contents"; //sql语句
$result=mysql_query($exec); //执行sql语句,返回结果
while($rs=mysql_fetch_object($result))
{
echo "<table><tr><td>xm:".$rs->name."</td></tr>";
echo "<tr><td>words:".$rs->content."</td></tr></table><br/>";
echo
"..............................................................................";
}
}
function search()
{
$var=$_POST['user_name'];
$trimmed = trim($var);
$query = "SELECT * FROM contents where name like '%".$trimmed."%'" ;
$result = mysql_query($query) ;
echo "<table>\n";
while($line=mysql_fetch_object($result))
{
echo "\t<tr>\n";
foreach ($line as $col_value)
{
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
}
}
?>
之后在model
中建立content.php代码如下:
<?php
/*
* Created on 2008-11-25
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
class Content extends AppModel
{
var $name = 'Content';
}
?>
接下来,就是mvc中的v了,就是视图界面。
需要建立两个界面,一个是默认显示界面,一个是检索后的见面,分别命名view.html.search.html
代码如下:
view.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<!--
* Created on 2008-11-25
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
-->
<head>
<title> </title>
</head>
<body>
</body>
</html>
search.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<!--
* Created on 2008-11-25
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
-->
<head>
<title> </title>
</head>
<body>
<form action="search" method="post" name="name1">
<input type="text" name="user_name">
<input type="submit" value="search">
</body>
</html>
之后调试运行,即可看到结果!!! |
|
|
|
|