How to show entry from “dynamic” category
Posted: 17 May 2008 05:50 AM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

I have this following weblog structure:

My First Weblog
1. Category 1
2. Category 2
3. Category 3

The Category is created dynamically, means that the administrator will create the new category when the several new posts/entries are created.

I have been searching this forum and the document ( but I maybe missed something ) most of the tag to display entry from specific category are always shown in template like this :

{exp:weblog:entries weblog="My First Weblog" category="3" limit="1" dynamic="off"}
{body}
{
/exp:weblog:entries}

Now here’s my question:
How can I show an entry randomly only from the latest category that’s been created dynamically without having to go to the template and change the category tag (category_id)?

Thanks a lot for reply.

Profile
 
 
Posted: 17 May 2008 06:28 AM   [ Ignore ]   [ # 1 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

One method would be PHP enabled on the template, on Input parsing stage:

<?
global $DB;
$query = $DB->query("SELECT cat_id FROM exp_categories ORDER BY cat_id DESC LIMIT 1");
$cat_id = ($query->num_rows == 1) ? $query->row['cat_id'] : '';
?>

{exp
:weblog:entries weblog="My First Weblog" category="<?php echo $cat_id; ?>" limit="1" dynamic="off"}
{body}
{
/exp:weblog:entries}

 Signature 
Profile
MSG
 
 
Posted: 17 May 2008 06:08 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

Hi Derek,

I appreciate your reply, but is there any other solutions that using native EE tag. I kinda like the idea of having free PHP code in the template.

Cheers

Profile
 
 
Posted: 17 May 2008 10:13 PM   [ Ignore ]   [ # 3 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

Using embeds, yes.

{exp:query sql="SELECT cat_id FROM exp_categories ORDER BY cat_id DESC LIMIT 1"}{embed="other/template" category="{cat_id}"}{/exp:query}

Then in the “other/template” template:

{exp:weblog:entries weblog="My First Weblog" category="{embed:category}" limit="1" dynamic="off"}
{body}
{
/exp:weblog:entries}

 Signature 
Profile
MSG
 
 
Posted: 09 June 2008 11:34 PM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

Hi Derek,

I am using your solution but why does not display the query.

Here’s the code that I use :

<?
            
global $DB;
            
$query = $DB->query("SELECT cat_id FROM exp_categories ORDER BY cat_id DESC LIMIT 1");
            
$cat_id = ($query->num_rows == 1) ? $query->row['cat_id'] : '';
            
?>

            {exp
:weblog:entries weblog="MyWeblog1" orderby="random" category="<?php echo $cat_id; ?>" limit="1" dynamic="off"  }
            
                {exp
:trunchtml chars="500"}
                {body}
                {
/exp:trunchtml}
             {
/exp:weblog:entries}

I set the preference of the template to Allow PHP , and PHP Parsing Stage to Input.
Can you please have a look at the code?

Thanks a lot, I really appreciate it

Profile
 
 
Posted: 10 June 2008 08:08 AM   [ Ignore ]   [ # 5 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

Where is your opening PHP tag?  Have you dumped $cat_id to the browser to see if it holds the value you expect?  Is “MyWeblog1” a valid weblog short name with entries in that category?  If you remove the PHP and write the tag manually with that category does it display the entry you expect?

 Signature 
Profile
MSG
 
 
Posted: 11 June 2008 07:09 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

Hi Derek,

Thanks a lot for your reply.

Derek Jones - 10 June 2008 08:08 AM

Where is your opening PHP tag?

Do you mean this :

<?php
            
global $DB;
            
$query = $DB->query("SELECT cat_id FROM exp_categories ORDER BY cat_id DESC LIMIT 1");
            
$cat_id = ($query->num_rows == 1) ? $query->row['cat_id'] : '';
            
?>

and this :

{exp:weblog:entries weblog="edition" orderby="random" category="<?php echo $cat_id; ?>" limit="1" dynamic="off"  }

Derek Jones - 10 June 2008 08:08 AM

Have you dumped $cat_id to the browser to see if it holds the value you expect?

I create test_cat.php with this following code and it gives me an error message.

The code :

<?php
            
global $DB;
            
$query = $DB->query("SELECT cat_id FROM exp_categories ORDER BY cat_id DESC LIMIT 1");
            
$cat_id = ($query->num_rows == 1) ? $query->row['cat_id'] : '';
            
?>

The error message :

Fatal error: Call to a member function query() on a non-object in /home/witheld/public_html/test_cat.php on line 3

Derek Jones - 10 June 2008 08:08 AM

Is “MyWeblog1” a valid weblog short name with entries in that category? 

Yes It does. I have double checked them. I put the name of “MyWeblog1” above is for example.

Derek Jones - 10 June 2008 08:08 AM

If you remove the PHP and write the tag manually with that category does it display the entry you expect?

Yes It does. Here’s the tag

{exp:weblog:entries weblog="MyWeblog1" orderby="random" category="16" limit="1" dynamic="off"  }
{body}
{
/exp:weblog:entries}

Note: category 16 is the newst category created in the MyWeblog1.

Thanks a lot for having a look

Profile
 
 
Posted: 11 June 2008 09:00 AM   [ Ignore ]   [ # 7 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

Beebs, this goes in a PHP enabled template, not a separate PHP file; ExpressionEngine won’t be invoked from your own PHP scripts.

 Signature 
Profile
MSG
 
 
Posted: 11 June 2008 03:26 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

OK, I understand Derek. I thought when you asked me : “Have you dumped $cat_id to the browser to see if it holds the value you expect?” is to dump the query directly in the page.

You know what, attached is the template with PHP enabled. It begins from line# 100. Appreciate for you to have a look.

Profile
 
 
Posted: 11 June 2008 11:16 PM   [ Ignore ]   [ # 9 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

I mean using echo $cat_id, or print_r($cat_id), etc.  Is PHP on this template set to parse in Input stage or Output stage?  It would need to be Input stage to be able to feed information to a tag.

 Signature 
Profile
MSG
 
 
Posted: 11 June 2008 11:21 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

It was set to Input stage (my post #4 above) and with this :

<?php echo $cat_id; ?>

Print the result correctly.

But when those code is put inside EE tag iit doesn’t print the result.

Appreciate a lot

Profile
 
 
Posted: 12 June 2008 07:07 AM   [ Ignore ]   [ # 11 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

Is the category displayed one that is assigned to the ‘edition’ weblog?  If so, place this, and only this, no markup, nothing else, in a new test template with PHP enabled on Input:

<?php
global $DB;
$query = $DB->query("SELECT cat_id FROM exp_categories ORDER BY cat_id DESC LIMIT 1");
$cat_id = ($query->num_rows == 1) ? $query->row['cat_id'] : '';
?>

{exp
:weblog:entries weblog="edition" orderby="random" category="<?php echo $cat_id; ?>" limit="1" dynamic="off"}
{entry_id}
- {categories}{category_id},{/categories}
{
/exp:weblog:entries}

Then visit the template in your browser.

 Signature 
Profile
MSG
 
 
Posted: 12 June 2008 02:13 PM   [ Ignore ]   [ # 12 ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

Hmhhhh.. the test template does not print anything.

I put the

<?php echo $cat_id; ?>

out of the tag and it prints the result though.

Thanks again

Profile
 
 
Posted: 12 June 2008 02:18 PM   [ Ignore ]   [ # 13 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

It sounds like that category id doesn’t exist as a valid category from the ‘edition’ weblog.  Removing the PHP and hardcoding the category would verify, or by visiting the category management section and checking id’s.  If you have multiple category groups in your installation, you would need to alter the query to only select from valid categories for the desired weblog.

 Signature 
Profile
MSG
 
 
Posted: 12 June 2008 02:41 PM   [ Ignore ]   [ # 14 ]  
Lab Assistant
RankRank
Total Posts:  183
Joined  11-27-2003

Hi Derek,

I change the query and It does work. I think the original query will print the entire cat_id regardless where the category coming from.

As what you advised, I limit the query only into the respective weblog.

Thanks a lot for your patient and help,

You may close the thread.

Here’s the code :

<?php
            
global $DB;
            
$query = $DB->query("SELECT cat_id FROM exp_categories WHERE group_id=4 ORDER BY cat_id DESC LIMIT 1");
            
$cat_id = ($query->num_rows == 1) ? $query->row['cat_id'] : '';
            
?>
            {exp
:weblog:entries weblog="edition" orderby="random" category="<?php echo $cat_id; ?>" limit="1" dynamic="off"  }{/exp:weblog:entries}

Profile
 
 
Posted: 12 June 2008 02:46 PM   [ Ignore ]   [ # 15 ]  
Administrator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15727
Joined  06-03-2002

Glad you got it working, Beebs.

 Signature 
Profile
MSG
 
 
   
 
 
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: 64491 Total Logged-in Users: 59
Total Topics: 81032 Total Anonymous Users: 23
Total Replies: 436058 Total Guests: 276
Total Posts: 517090    
Members ( View Memberlist )