文章出處

 

先來看怎么把數據庫的列表全都顯示出來

還是要先建一個php文件,還有html文件,都存到相應的目錄下

php文件中

<?php
include("../init.inc.php");
include("../DBDA.php");
$db = new DBDA();

$sql = "select * from nation";
$arr = $db->Query($sql);

$smarty->assign("shuju",$arr);
$smarty->display("main.html");

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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<h1>數據列表</h1>

<table width="100%" border="1" cellpadding="0" cellspacing="0">

<tr>
<td>代號</td>
<td>名稱</td>
<td>操作</td>

</tr>
<{foreach $shuju as $v}>
<tr>
<td><{$v[0]}></td>
<td><{$v[1]}></td>
<td>操作</td>

</tr>
<{/foreach}>

</table>

</body>
</html>

運行后

列表顯示成功

再來做別的操作

可以在操作那里加一個刪除和修改,刪除和之前做的php的一樣,就不在這里寫了,做個修改的操作

還是在那個main.html文件中

不寫刪除頁面了,做一個修改頁面

修改頁面是需要打到前端顯示的,所以還是要建兩個頁面

一個php一個對應的html頁面

xiugai.php文件中

<?php
include("../init.inc.php");
include("../DBDA.php");
$db = new DBDA();

$code= $_GET["code"];

$sql = "select * from nation where code='{$code}'";
$arr= $db->Query($sql);

$smarty->assign("nation",$arr[0]);
$smarty->display("xiugai.html");

根據傳過來的code來找它的數據

然后把它的數據先放到一個數組

打到xiugai.html顯示

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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<h1>修改頁面</h1>
<form action="update.php" method="post">
<div>代號:<input type="text" name="code" value="<{$nation[0]}>" /></div>
<div>名稱:<input type="text" name="name" value="<{$nation[1]}>" /></div>
<input type="submit" value="修改" />

</form>

</body>
</html>

從main.php開始運行

點擊修改,點n001的吧

它原來的數據默認顯示在這個頁面

再點n005試一試

它對應的數據也在默認顯示

 

 運行成功,接下來的頁面就和php的一樣了

只要能讓用戶看到的頁面,都得分成兩塊,一塊php的,一塊前端的

實現前后分離

做一個分頁查詢

main.php文件中

<?php
include("../init.inc.php");
include("../DBDA.php");
$db = new DBDA();
$sall="select count(*) from nation";
$zts=$db->StrQuery($sall);

include("../page.class.php");
$page = new Page($zts,5);


$sql = "select * from nation ".$page->limit;
$arr = $db->Query($sql);

$smarty->assign("fenye",$page->fpage());
$smarty->assign("shuju",$arr);
$smarty->display("main.html");

要注意這里

 

 如果不打空格的話就會報錯

然后html文件中,輸出一下分頁

這樣就可以了,運行一下

分頁運行成功

再加個查詢功能

html文件中,比較簡單點,加一個文本框和按鈕

php文件中

<?php
include("../init.inc.php");
include("../DBDA.php");
$db = new DBDA();

$tj = " 1=1 ";
if(!empty($_GET["name"]))
{  
    $n = $_GET["name"];
   $tj = " name like '%{$n}%' ";
}

$ztj= "where {$tj}";
$sall="select count(*) from nation ".$ztj;
$zts=$db->StrQuery($sall);



include("../page.class.php");
$page = new Page($zts,5);


$sql = "select * from nation ".$ztj.$page->limit;
$arr = $db->Query($sql);

$smarty->assign("fenye",$page->fpage());
$smarty->assign("shuju",$arr);
$smarty->display("main.html");

這里要注意不要忘了把總條件拼上

運行后

然后輸入條件查詢

點擊查詢

然后可以再試一個

點擊查詢

運行成功

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()