|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
LordNeon Smarty Rookie
Joined: 01 Dec 2005 Posts: 13 Location: Oslo, Norway
|
Posted: Tue Dec 06, 2005 10:25 am Post subject: Displaying all data from a MySQL Query |
|
|
First we need a sql query in whatever php file you want.
F.ex users.php
Code: |
<?php
$sql = "SELECT * FROM users ORDER BY username ASC";
$result = mysql_query($sql) OR die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$data[] = $row; # $data is the array created for use in the Smarty template.
}
$smarty->assign('data', $data);
$smarty->display('users.tpl');
?>
|
Now we need to create the users.tpl in the template folder of Smarty and put the following codes:
Code: |
<html>
<head>
<title>Displaying all data from a MySQL Query</title>
</head>
<body>
<table cellpadding="2" cellspacing="0">
<tr>
<td>Username:</td>
</tr>
{foreach from=$data item=item key=key}
<tr>
<td>{$item.username}</td>
<tr>
{/foreach}
</table>
</body>
</html>
|
This should loop through the $data array and display all data in the sql query.
Hope this helps. _________________ That's how we Troll |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Sun Dec 11, 2005 12:14 am Post subject: |
|
|
I bet no one has ever thought of that solution since Smarty knows to loop over associative arrays -- incl. those originating from a SQL query ...
Sorry for sounding sarcastic -- it's intended, especially since this "trick" is documented in the manual for {foreach}, Example 7-5. foreach key
Have fun, and happy exploring!
CirTap |
|
Back to top |
|
LordNeon Smarty Rookie
Joined: 01 Dec 2005 Posts: 13 Location: Oslo, Norway
|
Posted: Tue Dec 13, 2005 10:19 am Post subject: |
|
|
CirTap wrote: | I bet no one has ever thought of that solution since Smarty knows to loop over associative arrays -- incl. those originating from a SQL query ...
Sorry for sounding sarcastic -- it's intended, especially since this "trick" is documented in the manual for {foreach}, Example 7-5. foreach key
Have fun, and happy exploring!
CirTap |
If you have a better way of doing this, please share.
I'm a noob when it comes to Smarty anyway, but posting for other newbies like myself as things works the way I want.
I'm also open for all types of critisisms (including spelling ) _________________ That's how we Troll |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Tue Dec 13, 2005 10:52 am Post subject: |
|
|
LordNeon wrote: | If you have a better way of doing this, please share. | that's the point of my post: this is the no-brainer approach to handle associative arrays in Smarty (ignoring {section} here on purpose), and anyone who reads the manual , looking for a way to process this common type of data will find your "trick" as the 3rd example -- maybe I shoudn't presume people RTFM?
That's why I think it's funny to see this in the "Tips and Tricks" section, since IMHO it doesn't "qualify" as such, and you're basically citing Example 7-6 from the manual.
LordNeon wrote: | ... including spelling | sorry, no help from my part: I'm not a native english speaker. If you find any typos or spelling errors in my writings, feel free to keep them
Have fun,
CirTap |
|
Back to top |
|
LordNeon Smarty Rookie
Joined: 01 Dec 2005 Posts: 13 Location: Oslo, Norway
|
Posted: Wed Dec 14, 2005 11:11 am Post subject: |
|
|
Well, if I were able to post comments in the manual I would've put it there (like on the PHP's website). I do however understand what you're saying.
English isn't my native language either, so I guess we've something in comon there. _________________ That's how we Troll |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|