Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-07-21 04:57:57

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

need to count articles to do some math

I need to know how many total articles are going to be in the output of an article list, so then I can do some PHP maths with that quantity.
I think the idea is very similar to what pgonly attribute (on txp:article) does.
I will take a look at jmd_count and rvm_counter to see if they are helpful with the count part.

In the meanwhile, who can help me with the PHP maths? :)
I need to multiply the quantity of articles using a fixed value (that, of course, I already know) which represents the width of the box containing the article.
Like

25 (qty of articles) x 300 = 7500

Then, I need to use that result just to do this: <div style="width:7500px">.

Last edited by maniqui (2008-07-21 05:02:42)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#2 2008-07-21 05:24:03

rsilletti
Moderator
From: Spokane WA
Registered: 2004-04-28
Posts: 707

Re: need to count articles to do some math

This will give you the total pages in a page list:

function _numPages()
{
	global $thispage;
	return $thispage['numPages'];
}

You can use it as a plugin and call it with txp:_numPages, or just use <txp:php> code… </txp:php> to grab a number from global $thispage; $thispage[‘numPages’]; inline.

Offline

#3 2008-07-21 05:27:38

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: need to count articles to do some math

I’ve already the qty using rvm_counter (neat plug-in, thanks ruud!).
I did this.
On page template:

<!-- articles count reset <txp:rvm_counter name="articles" reset="0" /> -->
      <txp:article status="sticky" form="product" />

On article form:

<!-- <txp:rvm_counter name="articles" /> -->
<div class="product<txp:if_last_article> last-product</txp:if_last_article>">
    <h3 class="product-title"><txp:permlink title=""><txp:title /></txp:permlink></h3>
    <txp:body />
<strong><txp:rvm_counter name="articles" step="0" /></strong>
</div><!-- .product -->

Now, I think the hard task is this: I need to do the math before resetting it again.
And that means I’ve to use that number (or better, the result of the multiplication) somewhere on the page template.
Bring some light on me, please.

Last edited by maniqui (2008-07-21 05:27:54)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#4 2008-07-21 05:29:29

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: need to count articles to do some math

Hi Rick, thanks for your reply, but I’m not sure if I will need the number of pages.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#5 2008-07-21 06:40:22

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: need to count articles to do some math

I’m becoming a genius :P

<!-- articles count reset <txp:rvm_counter name="articles" reset="0" /> -->
<!-- <txp:article pgonly="1" status="sticky" form="product" /> -->
  <div id="content" style="width:<txp:php>$article_qty = rvm_counter(array('name'=>articles,'step'=>0));$width = 590;
$content_width = ($article_qty * $width) + (178 * 2);echo $content_width;</txp:php>px">
    <txp:output_form form="nav" />
    <div id="main" style="width:<txp:php>$article_qty = rvm_counter(array('name'=>articles,'step'=>0));$width = 590;
$main_width = ($article_qty * $width) - 60;echo $main_width;</txp:php>px">
      <txp:article status="sticky" form="product" />
    </div><!-- #main -->
  </div><!-- #content -->

So, in that code, you can see:

<txp:php>
    $article_qty = rvm_counter(array('name'=>articles,'step'=>0));
    $width = 590;
    $content_width = ($article_qty * $width) + (178 * 2);
    echo $content_width;
</txp:php>

and

<txp:php>
    $article_qty = rvm_counter(array('name'=>articles,'step'=>0));
    $width = 590;
    $main_width = ($article_qty * $width) - 60;
    echo $main_width;
</txp:php>

As you can see, I’m repeting a lot of code that could be probably re-used if only I find out how to re-use a $var of one chunk on the other one.

Last edited by maniqui (2008-07-21 14:18:10)


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#6 2008-07-21 14:13:51

thebombsite
Archived Plugin Author
From: Exmouth, England
Registered: 2004-08-24
Posts: 3,251
Website

Re: need to count articles to do some math

I don’t suppose you are using an SVN build Julián?


Stuart

In a Time of Universal Deceit
Telling the Truth is Revolutionary.

Offline

#7 2008-07-21 14:42:43

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: need to count articles to do some math

hi Stuart,
no, I’m not using SVN. I hope to start using it soon. We use SVN and something called UQDS at work.
Did you visualized some easy way to do it using upcoming 4.0.7?

In the meanwhile, with some help of Jon-Michael, I finally got what I need.

The page template:

<!-- Step 1: rvm_counter reset: 
<txp:rvm_counter name="articles" reset="0" /> 
-->
<!-- Step 2: this tag just calls a form with just rvm_counter 
<txp:article pgonly="1" status="sticky" form="product_counter" /> 
-->
<!-- Step 3: do the maths -->
<txp:php>
global $article_qty, $width, $content_width, $main_width;
$article_qty = rvm_counter(array(
    'name' => articles,
    'step' => 0,
));
$width = 590;
$content_width = ($article_qty * $width) + (178 * 2);
$main_width = ($article_qty * $width) - 60;
</txp:php>
<div id="content" style="width:<txp:php>echo $GLOBALS['content_width'];</txp:php>px">
    <txp:output_form form="nav" />
    <div id="main" style="width:<txp:php>echo $GLOBALS['main_width'];</txp:php>px">
        <txp:article status="sticky" form="product" />
    </div><!-- #main -->
</div><!-- #content -->
[...]

I’ve to put the txp:article pgonly="1" tag between HTML comments because there seems to be a problem with it: it outputs everything as if the attribute pgonly="1" isn’t there. :(
As said on Step 2, the product_counter article form:

<txp:rvm_counter name="articles" />

I’m still thinking about how to do it more “efficient” or “clean”. But I’ve a few questions:
*Are things (PHP, and forms calling forms) in TxP processed from top to bottom, from outside to inside?-
I don’t know how to explain what I’m trying to mean.

Basically, in the above code, I think it could be possible to eliminate Step 2 (the “product_counter” made with txp:article pgonly="1" form="product_counter") and do the count directly in the second txp:article form="product" (the real one).
The problem is that the page template needs the count result ($articvle_qty) before that second txp:article tag appears on the page template.

That’s where I think the pgonly="1" trick comes in handy, but would be better (well, just less code) to remove it.
I hope I explained myself. :s


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#8 2008-07-21 15:33:02

thebombsite
Archived Plugin Author
From: Exmouth, England
Registered: 2004-08-24
Posts: 3,251
Website

Re: need to count articles to do some math

Well the reason I mention it is because you have the new <txp:variable /> tag plus the ability to use “tags-within-tags” with the new parser including <php></php> so whilst I’m still trying to come to terms with the new variable tag and all it’s possibilities I was thinking that something like <txp:variable name="count" value='<php>some code here</php>' /> might be a way of storing a value then calling it later with <txp:variable name="count" />.


Stuart

In a Time of Universal Deceit
Telling the Truth is Revolutionary.

Offline

Board footer

Powered by FluxBB