|
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 |
eniac Smarty Rookie
Joined: 14 Jun 2003 Posts: 7
|
Posted: Sun Jun 15, 2003 9:45 am Post subject: smarty + mysql |
|
|
I have a bunch of data.
Now I would like to get out 20records out of my mysql db. And display them on my page.
I've been trying for some hours to get it to work with smarty.
Could someone give me a little example code on how I could get some records out of mysql and display them with smarty ? |
|
Back to top |
|
eniac Smarty Rookie
Joined: 14 Jun 2003 Posts: 7
|
Posted: Sun Jun 15, 2003 10:02 am Post subject: |
|
|
I've did it
here's how I've done it
Code: |
$faqs = array();
while($row = mysql_fetch_array($res,MYSQL_ASSOC)){
array_push($faqs,
array("q" => $row["question"]),
array("a" => $row["answer"])
);
}
$smarty->assign("faqs",$faqs); |
Code: | { * Smarty * }
{section name=i loop=$faqs}
<b>{$faqs[i].q}</b><br>
<i>{$faqs[i].a}</i>
<p>
{/section} |
Is this a good way or is this really bad code ? |
|
Back to top |
|
sweatje Smarty Regular
Joined: 17 Apr 2003 Posts: 70 Location: Bettendorf, Iowa, USA
|
Posted: Sun Jun 15, 2003 12:44 pm Post subject: |
|
|
I would probably do:
[php:1:243534ee81] $faqs = array();
while($row = mysql_fetch_array($res,MYSQL_ASSOC)){
$faqs[] = array("q" => $row["question"], "a" => $row["answer"]);
} [/php:1:243534ee81] _________________ Jason
jsweat_php AT yahoo DOT com |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Jun 15, 2003 8:02 pm Post subject: |
|
|
In addition to sweatje's suggestion, you can also optimize some. For example, you don't NEED to use associative keys since you can rewrite your sql to only pull the q, a (ie. SELECT question, answer...) meaning you can take the results from ordinal positions 0 and 1.
$faqs[] = array('q'=>$row[0], 'a'=>$row[1]);
I realize that you didn't show your SQL, so I will only mention as a tip that in general SELECT * is bad -- always explicitly pull only the fields you need and the rows you want from the db. It may be a little bit of overkill to go with the non-associative keys, but I just thought I'd point it out for you
Otherwise, good show! |
|
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
|