第一篇在这里
http://topic.csdn.net/u/20100226/12/43bf3dc5-795f-4fa5-861a-2f7bf8fd0cd6.html今天讲第二篇CURD操作我们下载最新的0.9.6.6beta版
新版改了一个函数up()改为edit()表示设置那个为编辑状态这样比较直观
下载后
把data目录下mallbook.sql文件导入数据库mallbook做测试
把数据库 用户名和密码和数据库名称
framework/config/inc.ini.php 里面修改 查看
framework/router/curdRouter.class.php
代码如下
<?php
class curdRouter extends controller{
    public function index()
{
   $booktype=M("booktype");
   //getRecord表示么得
   $this->assign("list",$booktype->orderby("bookid desc")->limit(10)->fetch()->getRecord());
}
public function create()
{
  //自动显示view/curd/目录下create.php文件
}
public function createForm()
{
  $booktype=M("booktype")->createForm()->save();
  //看看mysql没有操作成功
  if($booktype->isEffect())
  {
    $this->assign("msg","添加成功!");
  }
}
public function edit()
{
  //->edit()为原来up函数,现在改为edit表示编辑那个record默认是record[0];
  $form=M("booktype")->get(intval($_GET['id']))->edit()->getData();
  $this->assign("form",$form);
}
public function update()
{
  $booktype=M("booktype")->createForm()->save();
  $this->assign("form",$booktype->getData());
  if($booktype->isEffect())
  {
    $this->assign("msg","修改成功!");
  }
}
public function delete()
{
  $booktype=M("booktype")->delete(intval($_GET['id']));
  //检查有没有操作成功
  if($booktype->isEffect())
  {
    $this->assign("msg","删除成功!");
  }
}
}
?>
视图文件
view/curd/index.php文件<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CURD演示</title>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse;}
.center {text-align: center;}
.center table { margin-left: auto; margin-right: auto; text-align: left;}
.center th { text-align: center !important; }
td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccccff; font-weight: bold; color: #000000;}
.h {background-color: #9999cc; font-weight: bold; color: #000000;}
.v {background-color: #cccccc; color: #000000;}
.vr {background-color: #cccccc; text-align: right; color: #000000;}
img {float: right; border: 0px;}
hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
</style>
</head><body>
<ul>
<li><a href="<?php echo url_for("curd/create")?>">添加新记录</a></li>
<li><a href="<?php echo url_for("curd/index")?>">数据列表</a></li>
</ul>
<table width="400" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>序号</td>
    <td>类名</td>
    <td>typeid</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <?php foreach($list as $key=>$value):?>
  <tr>
    <td><?php echo $value['bookid'];?></td>
    <td><?php echo $value['classname'];?></td>
    <td><?php echo $value['typeid'];?></td>
    <td><a href="<?php echo url_for("curd/edit/id/".$value['bookid'])?>">编辑</a></td>
    <td><a href="<?php echo url_for("curd/delete/id/".$value['bookid'])?>">删除</a></td>
  </tr>
  <?php endforeach;?>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>

解决方案 »

  1.   

      <?php foreach($list as $key=>$value):?>
      <tr>
        <td><?php echo $value['bookid'];?></td>
        <td><?php echo $value['classname'];?></td>
        <td><?php echo $value['typeid'];?></td>
        <td><a href="<?php echo url_for("curd/edit/id/".$value['bookid'])?>">编辑</a></td>
        <td><a href="<?php echo url_for("curd/delete/id/".$value['bookid'])?>">删除</a></td>
      </tr>
      <?php endforeach;?>使用原生php代码标签看手册
    PHP 提供了一些流程控制的替代语法,包括 if,while,for,foreach 和 switch。替代语法的基本形式是把左花括号({)换成冒号(:),把右花括号(})分别换成 endif;,endwhile;,endfor;,endforeach; 以及 endswitch;。还有不少可以做标签呢url_for()为url地址修改函数请使用这个函数设置地址
      

  2.   

    其它视图代码页面
    create.php
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>CURD演示</title>
    <style type="text/css">
    body {background-color: #ffffff; color: #000000;}
    body, td, th, h1, h2 {font-family: sans-serif;}
    pre {margin: 0px; font-family: monospace;}
    a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
    a:hover {text-decoration: underline;}
    table {border-collapse: collapse;}
    .center {text-align: center;}
    .center table { margin-left: auto; margin-right: auto; text-align: left;}
    .center th { text-align: center !important; }
    td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
    h1 {font-size: 150%;}
    h2 {font-size: 125%;}
    .p {text-align: left;}
    .e {background-color: #ccccff; font-weight: bold; color: #000000;}
    .h {background-color: #9999cc; font-weight: bold; color: #000000;}
    .v {background-color: #cccccc; color: #000000;}
    .vr {background-color: #cccccc; text-align: right; color: #000000;}
    img {float: right; border: 0px;}
    hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
    </style>
    </head><body>
    目前编辑#:<?php echo $form['bookid']?>
    <ul>
    <li><a href="<?php echo url_for("curd/create")?>">添加新记录</a></li>
    <li><a href="<?php echo url_for("curd/index")?>">数据列表</a></li>
    </ul>
    <form id="form1" name="form1" method="post" action="<?php echo url_for("curd/createForm");?>">
    <dt>
    <label for="clalssname">类型名</label>
    </dt>
    <dd>
    <input type="text" name="classname" id="classname" value="<?php echo $form['classname'];?>"/>
    </dd>
    <dt>
    <label for="clalssname">类型ID</label>
    </dt>
    <dd>
    <input type="text" name="typeid" id="typeid" value="<?php echo $form['typeid'];?>"/>
    </dd>
    <dt>
    <label for="clalssname"></label>
    </dt>
    <dd>
    <input name="submitform" value="修改" type="submit" />
    </dd>
    <input name="bookid" type="hidden" value="<?php echo $form['bookid']?>" />
    </form>
    </body>
    </html>update.php
    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>CURD演示</title>
    <style type="text/css">
    body {background-color: #ffffff; color: #000000;}
    body, td, th, h1, h2 {font-family: sans-serif;}
    pre {margin: 0px; font-family: monospace;}
    a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
    a:hover {text-decoration: underline;}
    table {border-collapse: collapse;}
    .center {text-align: center;}
    .center table { margin-left: auto; margin-right: auto; text-align: left;}
    .center th { text-align: center !important; }
    td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
    h1 {font-size: 150%;}
    h2 {font-size: 125%;}
    .p {text-align: left;}
    .e {background-color: #ccccff; font-weight: bold; color: #000000;}
    .h {background-color: #9999cc; font-weight: bold; color: #000000;}
    .v {background-color: #cccccc; color: #000000;}
    .vr {background-color: #cccccc; text-align: right; color: #000000;}
    img {float: right; border: 0px;}
    hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
    </style>
    </head><body>
    目前编辑#:<?php echo $form['bookid']?><?php echo $msg;?>
    <ul>
    <li><a href="<?php echo url_for("curd/create")?>">添加新记录</a></li>
    <li><a href="<?php echo url_for("curd/index")?>">数据列表</a></li>
    </ul>
    </body>
    </html>edit.php<!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>CURD演示</title>
    <style type="text/css">
    body {background-color: #ffffff; color: #000000;}
    body, td, th, h1, h2 {font-family: sans-serif;}
    pre {margin: 0px; font-family: monospace;}
    a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
    a:hover {text-decoration: underline;}
    table {border-collapse: collapse;}
    .center {text-align: center;}
    .center table { margin-left: auto; margin-right: auto; text-align: left;}
    .center th { text-align: center !important; }
    td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
    h1 {font-size: 150%;}
    h2 {font-size: 125%;}
    .p {text-align: left;}
    .e {background-color: #ccccff; font-weight: bold; color: #000000;}
    .h {background-color: #9999cc; font-weight: bold; color: #000000;}
    .v {background-color: #cccccc; color: #000000;}
    .vr {background-color: #cccccc; text-align: right; color: #000000;}
    img {float: right; border: 0px;}
    hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
    </style>
    </head><body>
    目前编辑#:<?php echo $form['bookid']?>
    <ul>
    <li><a href="<?php echo url_for("curd/create")?>">添加新记录</a></li>
    <li><a href="<?php echo url_for("curd/index")?>">数据列表</a></li>
    </ul>
    <form id="form1" name="form1" method="post" action="<?php echo url_for("curd/update/id/".$form['bookid']);?>">
    <dt>
    <label for="clalssname">类型名</label>
    </dt>
    <dd>
    <input type="text" name="classname" id="classname" value="<?php echo $form['classname'];?>"/>
    </dd>
    <dt>
    <label for="clalssname">类型ID</label>
    </dt>
    <dd>
    <input type="text" name="typeid" id="typeid" value="<?php echo $form['typeid'];?>"/>
    </dd>
    <dt>
    <label for="clalssname"></label>
    </dt>
    <dd>
    <input name="submitform" value="修改" type="submit" />
    </dd>
    <input name="bookid" type="hidden" value="<?php echo $form['bookid']?>" />
    </form>
    </body>
    </html>delete.php<!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>CURD演示</title>
    <style type="text/css">
    body {background-color: #ffffff; color: #000000;}
    body, td, th, h1, h2 {font-family: sans-serif;}
    pre {margin: 0px; font-family: monospace;}
    a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
    a:hover {text-decoration: underline;}
    table {border-collapse: collapse;}
    .center {text-align: center;}
    .center table { margin-left: auto; margin-right: auto; text-align: left;}
    .center th { text-align: center !important; }
    td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
    h1 {font-size: 150%;}
    h2 {font-size: 125%;}
    .p {text-align: left;}
    .e {background-color: #ccccff; font-weight: bold; color: #000000;}
    .h {background-color: #9999cc; font-weight: bold; color: #000000;}
    .v {background-color: #cccccc; color: #000000;}
    .vr {background-color: #cccccc; text-align: right; color: #000000;}
    img {float: right; border: 0px;}
    hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
    </style>
    </head><body>
    目前编辑#:<?php echo $form['bookid']?><?php echo $msg;?>
    <ul>
    <li><a href="<?php echo url_for("curd/create")?>">添加新记录</a></li>
    <li><a href="<?php echo url_for("curd/index")?>">数据列表</a></li>
    </ul>
    </body>
    </html>
    index.php<!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>CURD演示</title>
    <style type="text/css">
    body {background-color: #ffffff; color: #000000;}
    body, td, th, h1, h2 {font-family: sans-serif;}
    pre {margin: 0px; font-family: monospace;}
    a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
    a:hover {text-decoration: underline;}
    table {border-collapse: collapse;}
    .center {text-align: center;}
    .center table { margin-left: auto; margin-right: auto; text-align: left;}
    .center th { text-align: center !important; }
    td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}
    h1 {font-size: 150%;}
    h2 {font-size: 125%;}
    .p {text-align: left;}
    .e {background-color: #ccccff; font-weight: bold; color: #000000;}
    .h {background-color: #9999cc; font-weight: bold; color: #000000;}
    .v {background-color: #cccccc; color: #000000;}
    .vr {background-color: #cccccc; text-align: right; color: #000000;}
    img {float: right; border: 0px;}
    hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}
    </style>
    </head><body>
    <ul>
    <li><a href="<?php echo url_for("curd/create")?>">添加新记录</a></li>
    <li><a href="<?php echo url_for("curd/index")?>">数据列表</a></li>
    </ul>
    <table width="400" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>序号</td>
        <td>类名</td>
        <td>typeid</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <?php foreach($list as $key=>$value):?>
      <tr>
        <td><?php echo $value['bookid'];?></td>
        <td><?php echo $value['classname'];?></td>
        <td><?php echo $value['typeid'];?></td>
        <td><a href="<?php echo url_for("curd/edit/id/".$value['bookid'])?>">编辑</a></td>
        <td><a href="<?php echo url_for("curd/delete/id/".$value['bookid'])?>">删除</a></td>
      </tr>
      <?php endforeach;?>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    </body>
    </html>
    以上文件都是view/curd/目录下面的视图文件
    下载后
    设置下就可以用了
    http://localhost/queryphp/index.php/curd/index就可以看到内容了
      

  3.   

    今晚再写一个教程 第三篇ORM使用
    这样大家可以使用在现有的项目中