您好,登錄后才能下訂單哦!
從數(shù)據(jù)庫查詢數(shù)據(jù),瀏覽器以表格形式顯示
模板頁面
<table border="1" width="800" align="center"> <caption>用戶信息表</caption> <{foreach $tdname as $val}> <th><{$val}></th> <{/foreach}> <{foreach $users as $user}> <{if $user@first}> <tr bgcolor="red"> <{elseif $user@last}> <tr bgcolor="yellow"> <{elseif $user@index is even}> <tr bgcolor="pink"> <{else}> <tr bgcolor="gray"> <{/if}> <{foreach $user as $val}> <td align="center"> <{$val}> </td> <{/foreach}> </tr> <{foreachelse}> 沒有用戶查詢出來! <{/foreach}> </table>
PHP頁面
<?php //創(chuàng)建smarty對象 require_once './libs/Smarty.class.php'; //定義根目錄 define('ROOT', str_replace("\\", "/",dirname(__FILE__))."/"); //實(shí)例化Smarty類 $smarty=new Smarty(); //設(shè)定定界符 $smarty->left_delimiter="<{"; $smarty->right_delimiter="}>"; //設(shè)置為false 定界符號左右可以有空格 $smarty->auto_literal = false; //添加一個插件的目錄 //$smarty->setPluginsDir(ROOT."/libs/myplugins/"); //注意添加一個插件,要把系統(tǒng)默認(rèn)設(shè)置的路徑加入 否則不能使用默認(rèn)系統(tǒng)的插件 $smarty->setPluginsDir(array( ROOT."/libs/plugins/",//系統(tǒng)默認(rèn)設(shè)置的路徑 ROOT."/libs/myplugins/",//自定義的 )); //連接數(shù)據(jù)庫 const DSN = 'mysql:host=localhost;dbname=test'; const DBUSER = 'root'; const DBPWD = 'root'; try{ $pdo = new PDO(DSN, DBUSER,DBPWD); }catch(PDOException $e){ echo "數(shù)據(jù)庫連接失敗:".$e->getMessage(); exit; } $query = "select id, username, password,email from users"; $stmt = $pdo->prepare($query); $stmt ->execute(); $users = $stmt->fetchAll(PDO::FETCH_ASSOC); //var_dump($users); $smarty->assign('users',$users); $query = "desc users"; $stmt = $pdo->prepare($query); $smarty->assign("users",$users); $stmt ->execute(); $tdname = $stmt->fetchAll(PDO::FETCH_COLUMN); //var_dump($tdname); $smarty->assign('tdname',$tdname); //變量輸出 $smarty->display('hello.tpl'); ?>
瀏覽器顯示
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。