從數(shù)據(jù)庫查詢數(shù)據(jù),瀏覽器以表格形式顯示
專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!成都創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計制作,服務(wù)好的網(wǎng)站設(shè)計公司,網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計負(fù)責(zé)任的成都網(wǎng)站制作公司!
模板頁面
<{$val}> | <{/foreach}> <{foreach $users as $user}> <{if $user@first}>
---|
<{$val}> | <{/foreach}>
PHP頁面
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'); ?>
瀏覽器顯示