|
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 |
notsmart Smarty n00b
Joined: 26 Oct 2013 Posts: 1
|
Posted: Sat Oct 26, 2013 3:46 pm Post subject: How to get the category ID of another table? (INC. TPL PHP) |
|
|
Hi,
I am new with smarty and new on this forum.
I have a problem. I need to get the category id of the category table, then I have the name of the category. The category id is the 'cid' from the table 'article'.
But how can I get this in .PHP and how do I print this in the .TPL file? Do I need two foreach codes in the .TPL file?
-----------------
.php file
-----------------
Code: | $results = $db->Query("SELECT `id`,`cid`, `name`, `message` FROM `article`");
$getcat = mysql_query("SELECT * FROM `category` WHERE `id` = '".$results['cid']."'");
$getthiscat = mysql_fetch_assoc($getcat);
$smarty->assign("results", $results);
$smarty->assign("getthiscat", $getthiscat); |
-----------------
.tpl file:
-----------------
Code: | {foreach from=$results item=i}
<div class='post-header'>
<h2 class='entry-title'><a href="{$getthiscat.namecategory}/{$i.name}">{$i.name}</a></h2>
</div>
{/foreach} |
-----------------
Thanks! |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Oct 26, 2013 4:47 pm Post subject: |
|
|
The easiest thing would be to do one query with both tables together to return both the item and category name in one go. Then you only have one db query and one result to loop over in Smarty. Also I don't your 2nd query above will work if the first query returns multiple rows.
Code: | $results = $db->Query("SELECT * FROM article,category where article.cid=category.id"); |
Otherwise you can use {section} to loop over and display values from multiple arrays, much like a php for(). |
|
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
|