smarty编写新闻管理系统(一)

本文详细介绍了新闻管理系统中登录界面、显示页面和添加新闻页面的实现过程,包括PHP会话管理、数据库操作及使用Smarty模板引擎。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天老师总结了smarty知识点,开始带我们做新闻管理系统,课上加上课下做的如下:

登陆界面:login.php

<?php
include("./conn.php");
include("./init.inc.php");

//开启session
session_start();
//M+C
if(isset($_POST['submit'])){
 $user=$_POST['username'];
 $pass=$_POST['password'];

 $_SESSION['username']=$user;
 
 $sql="select * from user where username='{$user}' and passwd='{$pass}'";
 echo $sql;
 $result=$mysqli->query($sql);
 $nums=$result->num_rows;

 if($nums==0){
  echo "username is not exit";
 }else{
  header("Location:index.php");
 }
}

$tpl->display("login.html");

?>

模板:login.html

<html>
<head>
 <title>这个模板文件</title>
 <style type="text/css">
  body{
   margin-top:40px;
  }  
  .style{
   font-size:38px;
   font-weight:bold;
  }

</style>
</head>
<body> 
 <p class="style">这个模板文件</p>

 <form name="myform"  method="post">
  <table border="2" width="290">
   <tr>
    <td>用户名:</td>
    <td><input type="text" name="username" /></td>
   </tr>
   <tr>
    <td>密码:</td>
    <td><input type="password" name="password" /></td>
   </tr>
  </table>
  <input type="submit" name="submit" value="提交" />
 </form>
</body>
</html>

新闻显示页面:

index.php

<?php
include("./conn.php");
include("./init.inc.php");
 session_start();

 $name=$_SESSION['username'];
 $mysqli->query("set names utf8");
 $sql="select * from news";

 $result=$mysqli->query($sql);
 while($row=$result->fetch_assoc()){
  $data[]=$row;
 }

$tpl->assign("name",$name);
$tpl->assign("data",$data);

$tpl->display("index.html");
?>

模板文件:index.html

<html>
<head>
 <title>这是模板文件</title>
 <style>
  body{
   margin-top:50px;
  }
  #name{
   font-size:18px;
   color:red;
  }
  #table{
   margin-left:30px;
  }
 </style>
</head>
<body>
 欢迎<span id="name"> <{ $name }> </span>登陆 !! &nbsp; &nbsp; &nbsp;<a href="login.php">安全退出</a>
 <p>这是模板文件</p>
 <hr />
 <br><br>
 <table width="800" height="200" border="0" id="table">
  <tr>
   <td>系统管理</td>
    <td>
    <form>
     <table border="0" width="500">
      <tr>
       <td width="120">标题</td>
       <td width="120" align="center">日期</td>
       <td width="120" align="center">选择</td>
      </tr>
      <{*循环显示数据库数据*}>
      <{section name=data loop=$data}> 
      <tr>
       <td><{$data[data].title}></td>
       <td><{$data[data].date}></td>
       <td align="center"><input type="checkbox" name="checkbox[]"></td>
      </tr>
      <{/section}>
       </table>
     </td>
   </tr>
   <tr>
    <td><a href="edit.php">添加新闻</a></td>
    <td>
     <input type="submit" value="删除" />
    </form>

    </td>
   </tr>
 </table>

</body>
</html>

添加新闻页面:

edit.php

<?php
session_start();

include("./init.inc.php");
include("./conn.php");
include('./FCKeditor/fckeditor.php');
$name=$_SESSION['username'];

$mysqli->query("set names utf8");

if(empty($_POST['sub'])){
 $title=$_POST['title'];
 $content=$_POST['content'];
 $sql="insert into news(author,title,content,datetime) values('{$name}','{$title}','{$content}',now())";
 echo $sql;
 $mysqli->query($sql);
}
$tpl->display("edit.html");
?>

模板文件:

edit.html

<html>
<head>
 <title>新建新闻</title>
</head>
<body>
<br><br>
<p>新闻管理登陆</p>
<hr>
<table width="771" height="501" border="0">
  <tr>
    <td height="62"><div align="center">系统管理</div></td>
    <td width="666" rowspan="2">
 <form name="form1" method="post" action="edit.php">
      <p>标题
          <input name="title" type="text" id="title" value="">
   </p>
      <p>内容:</p>
      <p>
    <div>
   <input type="hidden" id="content" name="content" value="" style="display:none" />
   <input type="hidden" id="content___Config" value="AutoDetectLanguage=true&amp;DefaultLanguage=en" style="display:none" />
   
   <iframe id="content___Frame" src="./FCKeditor/editor/fckeditor.html?InstanceName=content&amp;Toolbar=Default"
   width="100%" height="320" frameborder="0" scrolling="no"></iframe>
  </div>
   </p>
      <p>
   <input type="submit" name="sub" value="提交">
   <!--<input type="hidden" name='action' value=addnews>
   <input name="id" type="hidden" value="">-->
   </p>
    </form>
 </td>
  </tr>
  <tr>
    <td width="95" height="433"><div align="center">添加新闻</div></td>
  </tr>
</table>

</body>
</html>

明天继续!!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值