Oops - Not SOLVED: variable doesn’t update inline but does as embedded variable
Posted: 07 February 2008 11:09 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005

This is a curious thing.

Deron Sizemore was trying to show parent categories without links yet have their children with links. I came up with a method that didn’t work, though it seemed logical, of combining a weblog:categories tag pair set to parent_only=“yes” to filter out parents (which then just displayed category names between heading tags), and then used a query module SQL-call found in the Wiki with parent_id={category_id} to extract the children.

It didn’t work. The query simply returned the parent id as many times as there were children.

I put the query part into an embedded template, however, and passed the {category_id} to it via an embed variable, and it worked just fine! Was the {category_id} variable in the first case above not parsing to update itself between passes in the loop when used in-line, but was forced to parse when being passed to the embedded template?

Here’s the two versions of the code over at this forum thread:
Version 1 in-line that doesn’t work:

<h2 class="sidetitle">Categories</h2>

{!-- Sift out parent categories for category_id's --}
{exp:weblog:categories weblog="{master_weblog_name}" parent_only="yes"}

{!-- Print Parent Name --}
<h4>{category_name}</h4>

{!-- get child categories and link them --}

<ul>
{exp:query sql="SELECT cat_id as category_id, cat_name AS category_name FROM exp_categories WHERE parent_id = {embed:my_parent}"}
<li><a href="{path=science/C{category_id}}">{category_name}</a></li>
{/exp:query}
</ul>

{/exp:weblog:categories}

Version 2 embedded template that does work:

<h2 class="sidetitle">Categories</h2>

{!-- Sift out parent categories for category_id's --}
{exp:weblog:categories weblog="{master_weblog_name}" parent_only="yes"}

{!-- Print Parent Name --}
<h4>{category_name}</h4>

{!-- Send category_id of parent to sql query in embedded template --}
{embed="base1/_embed_child_query" my_parent="{category_id}"}

{/exp:weblog:categories}

Embedded query:

{!-- get child categories and link them --}

<ul>
{exp:query sql="SELECT cat_id as category_id, cat_name AS category_name FROM exp_categories WHERE parent_id = {embed:my_parent}"}
<li><a href="{path=science/C{category_id}}">{category_name}</a></li>
{/exp:query}
</ul>

Terry

Profile
 
 
Posted: 07 February 2008 11:27 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23537
Joined  05-20-2002

Off the top of my head, I’d guess a variable name conflict and try using aliases in the query.  Would be leaner than embedding and can’t see why it wouldn’t work.  Still not lean- but leaner.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 07 February 2008 11:31 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005
Robin Sowell - 07 February 2008 11:27 AM

Off the top of my head, I’d guess a variable name conflict and try using aliases in the query.  Would be leaner than embedding and can’t see why it wouldn’t work.  Still not lean- but leaner.

Gotcha - “SELECT cat_id as category_id” uses my {category_id} variable (just pasted that in from the wiki and didn’t even notice!) I’ll try it set differently.

You’re fast, alright!

Terry

Profile
 
 
Posted: 07 February 2008 11:37 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005

That was it! Changing

SELECT cat_id as category_id, cat_name AS category_name

to

SELECT cat_id as child_category_id, cat_name AS child_category_name

did it.

Thanks - I’ll know to look for such things in the future. I’m updating the Wiki entry so it does not use already-standard variable names as aliases.

Terry

Profile
 
 
Posted: 07 February 2008 12:44 PM   [ Ignore ]   [ # 4 ]  
Moderator
Avatar
RankRankRankRankRankRankRankRank
Total Posts:  32899
Joined  05-14-2004

Glad to hear you got it working =)

 Signature 
Profile
MSG
 
 
Posted: 07 February 2008 01:35 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005

Oops - that didn’t fix it entirely. Now the query runs and displays all the right names, and the alias {child_category_id} shows the right ID, but the path=group/template part is displaying the parent category id (with the child id sitting after it in its own segment).

See example of brokenness here.

Here again is the embedded version that works correctly.

Here is the broken code:

<h2 class="sidetitle">Categories</h2>

{exp:weblog:categories weblog="{master_weblog_name}" parent_only="yes"}

<h4>{category_name}</h4>
<
ul>
{exp:query sql="SELECT cat_id as child_category_id, cat_name AS child_category_name FROM exp_categories WHERE parent_id = {category_id}"}
<li><a href="{path=science}C{child_category_id}}">{child_category_name}</a></li>
{/exp:query}
</ul>
{/exp:weblog:categories}

Terry

Profile
 
 
Posted: 09 February 2008 01:52 PM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005

I figured out that this breaks because of the href=”{path= variable in the query module. It has to be evaluated separately in order to update its links to the externally referenced values. That is, used in-line between the weblog:categories tags, it does not get its values from the query, but rather from the calling surrounding tags and their latest category link assignations… Calling the query from outside the loop allows the path= variable to update with the child’s path info, derived from the query itself.

Terry

Profile
 
 
Posted: 10 February 2008 09:36 AM   [ Ignore ]   [ # 7 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23537
Joined  05-20-2002

Nice job figuring the workaround, tbitton.  Glad you got it squared away.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 10 February 2008 11:47 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  609
Joined  06-29-2005
Robin Sowell - 10 February 2008 09:36 AM

Nice job figuring the workaround, tbitton.  Glad you got it squared away.

Thanks, Robin. I updated the Wiki entry on this SQL query to explain the issue and link back to this discussion.

Terry

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 65011 Total Logged-in Users: 52
Total Topics: 82079 Total Anonymous Users: 37
Total Replies: 441121 Total Guests: 302
Total Posts: 523200    
Members ( View Memberlist )