|
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 |
toby5482 Smarty n00b
Joined: 24 Feb 2004 Posts: 2 Location: UK, Kent
|
Posted: Tue Feb 24, 2004 1:05 pm Post subject: Refering to a PHP Function with Smarty - Newbie |
|
|
Hi, I'm new to PHP and Smarty so I'm in need of a little help.
I've made a function with PHP that calls values from the MYSQL database. I have tested the PHP function and it works fine. The next step I've tried to do is integrate the function into one of my Smarty templates. To be honest I haven't got a clue what code I'm supposed to be entering and if I need to assign variables.
This is the PHP code I've written:
Quote: | function category_desc($id) {
$result = mysql_query("SELECT categoryid, description FROM xcart_categories WHERE categoryid='$id'");
while($row = mysql_fetch_array($result, MYSQL_NUM)){
printf ("$row[1]");
}
mysql_free_result($result);} |
So how do i get this working in smarty templates???
Any help on this would be great |
|
Back to top |
|
Nick W Smarty Rookie
Joined: 23 May 2003 Posts: 22
|
Posted: Tue Feb 24, 2004 1:39 pm Post subject: |
|
|
How about somthing like this:
Code: |
while($row = mysql_fetch_array($result, MYSQL_NUM)){
$var4smarty[]=$row[1]; // this will populate an array with values
}
$smarty->assign('var4smarty',$var4smarty);
//////////// then in ur template
{if $var4smarty}
<ul>
{foreach from=$var4smarty item=v4s}
<li>{$v4s}</li>
{/foreach}
</ul>
{/if}
|
You can find the functions and constructs i used (there really very simple) in the smarty docs...
hth
Nick |
|
Back to top |
|
toby5482 Smarty n00b
Joined: 24 Feb 2004 Posts: 2 Location: UK, Kent
|
Posted: Tue Feb 24, 2004 2:13 pm Post subject: |
|
|
Thanks Nick, that has helped. But in my code the field that is selected in the MYSQL table is determined by the variable $id, how can I state that in smarty so that it displays the correct result? |
|
Back to top |
|
Nick W Smarty Rookie
Joined: 23 May 2003 Posts: 22
|
Posted: Tue Feb 24, 2004 2:49 pm Post subject: |
|
|
Change the code to this, then do some basic PHP learning over at php.net
Code: |
while($row = mysql_fetch_array($result, MYSQL_NUM)){
$var4smarty[$row[0]]=$row[1]; // Now u have the id and the val
}
$smarty->assign('var4smarty',$var4smarty);
|
You'll have to look at the foreach() function in the smarty docs on how to access the key as i cant remember off hand....
Nick |
|
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
|