|
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 |
bmadigan Smarty Rookie
Joined: 03 May 2003 Posts: 6
|
Posted: Sat May 03, 2003 6:01 pm Post subject: Nested Sections |
|
|
I have been following the link below (Loop in Loop) and I keep coming back to where I was before. I will try to be somewhat explicit in my code:
Here is my Current Output:
Federal Links
Local Links
Here is the output I DESIRE
Federal Links
Local Links
Links.html
Code: |
// Get Public Links Categories
$pubCatSQL = "SELECT * FROM LinksCat ORDER BY Name";
$pubCatResult = mysql_query($pubCatSQL,$conn) or die('Error In Query: Category');
$rsPubCat = array(); // Set Result Set Variable
$i = 0; // Initialize Array Index
while ($pubRowCat = mysql_fetch_array($pubCatResult)) {
$tmpPubCat = array (
'id' => $pubRowCat['id'],
'Name' => $pubRowCat['Name']
);
// Get the Link(s) under this Category
// $pubSQL = "SELECT * FROM Links WHERE CatID = $tmpPubCat[id] ORDER BY Title";
$pubSQL = "SELECT * FROM Links WHERE CatID = $pubRowCat[id] ORDER BY Title";
$pubResult = mysql_query($pubSQL,$conn) or die('Error In Query: Links');
$rsPub = array();
$li = 0;
while ($pubRow = mysql_fetch_array($pubResult)) {
$tmpPub = array (
'id' => $pubRow['id'],
'Title' => $pubRow['Title'],
'URL' => $pubRow['URL']
);
$rsSetPub[$li++] = $tmpPub;
}
$rsSetPubCat[$i++] = $tmpPubCat;
}
...
// Assign/Pass The Public Category Results To the Template
$smarty->assign_by_ref('rowPubCat', $rsSetPubCat);
// Assign/Pass The Public Links Results To the Template
$smarty->assign_by_ref('rowPublic', $rsSetPub);
|
Here is my Smarty Template Code:
Code: |
{section name=PubCat loop=$rowPubCat}
<p class="orangeHeader">{$rowPubCat[PubCat].Name}</p>
{section name=PubLink loop=$rowPublic}
<li><a href="{$rowPublic[PubLink].URL}" class="textlink">{$rowPublic[PubLink].Title}</a></li>
{/section}
{/section}
|
I assume my problem is in the Code: | {section name=PubLink loop=$rowPublic} | Part of my template. I cannot get this to work at all.
Any suggestions would be great |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat May 03, 2003 10:52 pm Post subject: |
|
|
Actually, it looks like your inner PHP loop is overwriting the results of the category contents.
I haven't tested, but you should be okay if you use something along the lines of:
$rsSetPub[$i][$li++] = $tmpPub;
and in your template:
{$rowPublic[PubCat][PubLink].URL}
A few comments:
Get your db to join the data. It is faster and leads to better program design. It also means you don't need an inner-loop
Something like:
$pubCatSQL = "SELECT LinksCat.*, Link.* FROM LinksCat, Links WHERE Links.CatID = LinksCat.ID"
Try to condense your data structure. There is probably no need for separate arrays for categories and links. Either make them both children of a single array or make the links children of each category.
Hope that helps!
Last edited by boots on Sun May 04, 2003 12:17 am; edited 1 time in total |
|
Back to top |
|
bmadigan Smarty Rookie
Joined: 03 May 2003 Posts: 6
|
Posted: Sat May 03, 2003 11:12 pm Post subject: |
|
|
Hi, I will try your suggestion for both your comments.
I did have this query already, I agree with you for defining my data structure within the DB Server itself. (ie. having 1 array)
I would normally have run it like this
Code: |
$publicSQL = "SELECT *, LinksCat.Name, LinksCat.ID
FROM Links
INNER JOIN LinksCat
ON Links.CatID = LinksCat.id
WHERE LinksCat.Members = '0'
ORDER BY LinksCat.Name";
|
Thanks for the reply, I'll try both out. |
|
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
|