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

Refering to a PHP Function with Smarty - Newbie

 
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
toby5482
Smarty n00b


Joined: 24 Feb 2004
Posts: 2
Location: UK, Kent

PostPosted: Tue Feb 24, 2004 1:05 pm    Post subject: Refering to a PHP Function with Smarty - Newbie Reply with quote

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
View user's profile Send private message Visit poster's website
Nick W
Smarty Rookie


Joined: 23 May 2003
Posts: 22

PostPosted: Tue Feb 24, 2004 1:39 pm    Post subject: Reply with quote

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


Joined: 24 Feb 2004
Posts: 2
Location: UK, Kent

PostPosted: Tue Feb 24, 2004 2:13 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Nick W
Smarty Rookie


Joined: 23 May 2003
Posts: 22

PostPosted: Tue Feb 24, 2004 2:49 pm    Post subject: Reply with quote

Change the code to this, then do some basic PHP learning over at php.net Wink

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
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