PHP count based upon EE conditional
Posted: 10 November 2009 09:54 PM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

I want to use the PHP counter described in this wiki article, but limit its count to only include articles passing an EE conditional test. Here is what I have that doesn’t work:

<?php $entry_count=0; ?> <!-- initialize a variable -->


{exp:weblog:entries weblog="advertisements" orderby="entry_id" sort="asc" dynamic="off" status="open"}
{if ad_active_inactive
=="Active"}
<?php $entry_count
++; ?> <!-- increment the variable -->
{/if}
{
/exp:weblog:entries}

<?PHP
$minuteR
= date("i"); // this gets the minute numeral for the following operation
$offset =  ($minuteR % $entry_count); // this uses the modulo operator gives the remainder of zero through whatever changing by the minute
$totalR = $entry_count;
?>

{embed
="embed_templates/_ads_rotator" theOffset="<?PHP echo ($offset); ?>" theTotal="<?PHP echo $totalR; ?>" }

I use the entry_count to parse an offset based upon the minute to rotate some scrolling sidebar ads, as described in my wiki entry called Rotate Sidebar Ads Sequentially. Before you tell me I can use status for this issue, the designer has included an “Active/Inactive” radio button field in the weblog entry form to make it easier for the author to designate the status, so that’s why I’m trying to test for the contents of that field.

Problem is, it parses the PHP counter before the if/then statement, so I get the same number every time (all the entries) instead of a count of the smaller number of entries that have that particular field set to “Active”.

Any ideas? I need the PHP to parse on output, I believe, for this to work, if that makes any difference. But I could be all wrong about that.

Terry Leigh Britton

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 10 November 2009 11:47 PM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1191
Joined  12-18-2008

move the conditional into PHP as well.

<?php
if("{ad_active_inactive}" == "Active") {
    $entry_count
++;
}
?>
 Signature 

EE Pro Network
eMarketSouth - web design & development, SEO, video production, and more
ExpressionEngine consulting services - complex SQL queries, .htaccess url rewriting, custom plugins/modules/extensions, template optimization, javascript/jQuery enhancements, and more!
Skype: tywangsness

Profile
 
 
Posted: 11 November 2009 12:10 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005
ender - 10 November 2009 11:47 PM

move the conditional into PHP as well.

<?php
if("{ad_active_inactive}" == "Active") {
    $entry_count
++;
}
?>

Ahhhh - I suspected that but didn’t know what the syntax would be.

Thanks, I’ll give this a try!

Terry Leigh Britton

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 11 November 2009 12:21 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005

hmmm, it isn’t incrementing. The variable is staying at zero for some reason now. Could there be something wrong with the syntax for pulling the field’s content in?

<?php $entry_count=0; ?> <!-- initialize a variable -->


{exp:weblog:entries weblog="advertisements" orderby="entry_id" sort="asc" dynamic="off" status="open"}

<?php
if("{ad_active_inactive}" == "Active") {
    $entry_count
++;
}
?>
{
/exp:weblog:entries}

<?PHP
$minuteR
= date("i"); // this gets the minute numeral for the following operation
$offset =  ($minuteR % $entry_count); // this uses the modulo operator gives the remainder of zero through whatever changing by the minute
$totalR = $entry_count;
?>

Terry Leigh Britton

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 11 November 2009 09:29 AM   [ Ignore ]   [ # 4 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1191
Joined  12-18-2008

you can debug this sort of thing best by turning off PHP to see what code gets generated when you visit the page.

 Signature 

EE Pro Network
eMarketSouth - web design & development, SEO, video production, and more
ExpressionEngine consulting services - complex SQL queries, .htaccess url rewriting, custom plugins/modules/extensions, template optimization, javascript/jQuery enhancements, and more!
Skype: tywangsness

Profile
 
 
Posted: 11 November 2009 09:55 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  671
Joined  06-29-2005
ender - 11 November 2009 09:29 AM

you can debug this sort of thing best by turning off PHP to see what code gets generated when you visit the page.

I hope this isn’t another parsing-order issue!

Terry Leigh Britton

 Signature 

Terry Leigh Britton
http://twitter.com/TerryBritton
http://twitter.com/Terr_ee

Search the web using Terry Leigh Britton to find all kinds of stuff I’m into!
Or use Yasni for Terry Leigh Britton

Profile
 
 
Posted: 11 November 2009 09:58 AM   [ Ignore ]   [ # 6 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1191
Joined  12-18-2008

you only need to set PHP parsing to input when the PHP output contains template tags or values that need to be sent to template tags. In this case you’re using template tags to print PHP to be run afterward, so you’re fine with output processing.

 Signature 

EE Pro Network
eMarketSouth - web design & development, SEO, video production, and more
ExpressionEngine consulting services - complex SQL queries, .htaccess url rewriting, custom plugins/modules/extensions, template optimization, javascript/jQuery enhancements, and more!
Skype: tywangsness

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 1743, on December 02, 2009 03:47 PM
Total Registered Members: 119961 Total Logged-in Users: 76
Total Topics: 126089 Total Anonymous Users: 43
Total Replies: 663252 Total Guests: 437
Total Posts: 789341    
Members ( View Memberlist )