Starting probs
| From: | admin at ogj dot dk | Date: | Tue, 07 Aug 2001 15:11:31 +0000 |
| Subject: | Starting probs | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-1315@lists.php.net to get a copy of this message | ||
Howdy
I've just begon using Smarty and I've have a probably simple question that I
couldn't find a solution to:
I want to fetch some data from a MySQL database and build a table showing
the data.
In normal php it would be something like:
$sql = "SELCT uid, uname, email FROM users ORDER BY uname LIMIT 0,25";
$result = mysql_query($sql);
print "<table>";
while (list($uid,$uname,$email) = mysql_fetch_array($result)){
print "<tr><td>$uid<td>$uname<td>$email";
}
print "</table>";
How do you do that with Smarty?
Here's what I've tried:
######## test.php ########
require_once("Smarty.class.php");
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = true;
$sql = "SELCT uid, uname, email FROM users ORDER BY uname LIMIT 0,25";
$result = mysql_query($sql) or die("bla");
$smarty->assign("data", mysql_fetch_array($result));
$smarty->display("test.tpl");
###### test.tpl ##########
<table border='1'>
<tr>
<td>UID
<td>UNAME
<td>EMAIL
{section name=user loop=$data}
<tr {if %user.rownum% is even}style='background-color: silver;'{/if}>
<td>{$user[uid]}
<td>{$user[uname]}
<td>{$user[email]}
{sectionelse}
No data in database
{/section}
</table>
######### This is what it outputs #########
<table border='1'>
<tr>
<td>UID
<td>UNAME
<td>EMAIL
<tr >
<td>
<td>
<td>
<tr style='background-color: silver;'>
<td>
<td>
<td>
<tr >
<td>
<td>
<td>
</table>
<SCRIPT language=javascript>
_smarty_console =
window.open("","console","width=560,height=600,resizable,scrollbars=yes");
_smarty_console.document.write("<HTML><TITLE>Smarty Debug
Console</TITLE><BODY bgcolor=#ffffff>");
_smarty_console.document.write("<table border=0 width=100%>");
_smarty_console.document.write("<tr bgcolor=#cccccc><th colspan=2>Smarty
Debug Console</th></tr>");
_smarty_console.document.write("<tr bgcolor=#cccccc><td
colspan=2><b>included templates:</b></td></tr>");
_smarty_console.document.write("<tr bgcolor=#eeeeee><td
colspan=2><tt><font
color=brown>test3.tpl</font></tt></td></tr>");
_smarty_console.document.write("<tr bgcolor=#cccccc><td
colspan=2><b>assigned template variables:</b></td></tr>");
_smarty_console.document.write("<tr bgcolor=#eeeeee><td><tt><font
color=blue>{$SCRIPT_NAME}</font></td><td><font
color=red>/testing/template/test3.php</font></tt></td></tr>");
_smarty_console.document.write("<tr bgcolor=#fafafa><td><tt><font
color=blue>{$brugere}</font></td><td><font color=green>Array</font>
(6)</tt></td></tr>");
_smarty_console.document.write("<tr bgcolor=#eeeeee><td><tt><font
color=blue>{$data}</font></td><td><font color=green>Array</font>
(3)</tt></td></tr>");
_smarty_console.document.write("<tr bgcolor=#fafafa><td><tt><font
color=blue>{$smarty}</font></td><td><font color=green>Array</font>
(7)</tt></td></tr>");
_smarty_console.document.write("<tr bgcolor=#cccccc><td
colspan=2><b>assigned config file variables (outter template
scope):</b></td></tr>");
_smarty_console.document.write("<tr bgcolor=#eeeeee><td
colspan=2><tt><i>no config vars
assigned</i></tt></td></tr>");
_smarty_console.document.write("</table>");
_smarty_console.document.write("</BODY></HTML>");
_smarty_console.document.close();
</SCRIPT>