纵有疾风起
人生不言弃

技能测试2:使用smarty模板引擎完成后台分类列表展示

案例效果:

1.首先做一个简单的添加表单,如下:

   <!DOCTYPE html>
<html>
<head>
    <meta charset=”utf-8″>
    <title>分类添加</title>
</head>
<body>
    <h3>添加分类</h3>
    <table>
        <form method=’post’ action=”add.php”>
            <tr>
                <td>分类名称:</td>
                <td><input type=”text” name=”cate_name”></td>
            </tr>    
            <tr>
                <td>分类排序:</td>
                <td><input type=”text” name=”cate_id”></td>
            </tr>
            <tr>
                <td colspan=”2″><input type=”submit” value=”提交”></td>
            </tr>
        </form>    

    </table>
</body>
</html>

2.既然采用smarty,那就得使用add.php脚本来接收表单的值,连接数据库完成入库;数据库连接和增加采用之前所做的数据库操作类

<?php
    include ‘./libs/Smarty.class.php’;
    include ‘./include/Mysql.class.php’;

    $smarty=new Smarty;
    $smarty->display(‘add.html’);
    //如果表单提交执行数据库入库
    if(!empty($_POST)){
        $catename=$_POST[‘cate_name’];
        $cateid=$_POST[‘cate_id’];
        $db=new Mysql;
        $sql=”insert into category(cate_id,cate_name)values(‘$cateid’,’$catename’)”;
        $result=$db->add($sql);
        if($result){
            echo “数据提交成功,跳转中…”;
            //添加成功跳转至show页面展示数据库内容
            header(“refresh:3;url=show.php”);
        }else{
            echo “数据插入不成功!”;
        }
}
?>

3.show.php显示所有的数据库内容,按照分类ID进行降序排列

<?php
    include ‘./libs/Smarty.class.php’;
    include ‘./include/Mysql.class.php’;
    $smarty=new Smarty;
    $db=new Mysql;

    //降序排列显示
    $sql=”select * from category order by cate_id desc”;

    $lists=$db->getAll($sql);

    $smarty->assign(‘lists’,$lists);

    $smarty->display(‘show.html’);
?>

4.show.html模板显示内容,建立delete、insert和update的url链接,通过get方式传值

<!DOCTYPE html>
<html>
<head>
    <meta charset=”utf-8″>
    <title>分类列表</title>
</head>
<body>
    <table>
        <h3>分类列表</h3>
        <tr><td><a href=’add.php’>新增分类</a></td></tr>
        <tr>
            <th>新闻编号</th>
            <th>新闻名称</th>
            <th>操作</th>
        </tr>    
        {foreach $lists as $list}
        <tr>
            <td>{$list.cate_id}</td>
            <td>{$list.cate_name}</td>
            <td><a href=”editor.php?cate_id={$list.cate_id}&cate_name={$list.cate_name}”>修改</a>|<a href=”delete.php?cate_id={$list.cate_id}”>删除</a></td>    
        </tr>
        {/foreach}            
    </table>
</body>
</html>

5.剩下的就很简单了,建立上述跳转文件接收get值进行操作,如此就完成了一个基本的后台类别操作



原文链接:https://blog.csdn.net/living_ren/article/details/78781615

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

未经允许不得转载:起风网 » 技能测试2:使用smarty模板引擎完成后台分类列表展示
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录