Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

smarty + mysql

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
eniac
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sun Jun 15, 2003 9:45 am    Post subject: smarty + mysql Reply with quote

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
View user's profile Send private message
eniac
Smarty Rookie


Joined: 14 Jun 2003
Posts: 7

PostPosted: Sun Jun 15, 2003 10:02 am    Post subject: Reply with quote

I've did it Smile
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
View user's profile Send private message
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Sun Jun 15, 2003 12:44 pm    Post subject: Reply with quote

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
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun Jun 15, 2003 8:02 pm    Post subject: Reply with quote

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 Smile

Otherwise, good show!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP