Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
I am working on a page where I need to resize the divs width based on how many pages are in the folder (for example if I have 2 pages, the divs should each be 50% width).

I have found the useful variable k_page_foldertotalpagecount which returns the correct number of pages in the current folder. The problem I am running into is when I try and divide 100 by that variable to get the % width for each div. For some reason that variable changes to 0 when PHP tries to use as an int. I have tried to manually convert it to an int and float but each time it gets changed to 0. When it is a string it has the right value.

How can I use this number without it changing to 0?

My code: (inside a pages tag with a folder selection)
Code: Select all
<?php
$count="<cms:show k_page_foldertotalpagecount/>";
echo gettype($count);//Currently a string
echo $count;//Returns the correct value but only when it is a string
$width = 100/$count; //Returns error because it is trying to divide by 0
echo "{$width}"; //Doesn't return anything
?>
@blazerunner44,

I think the reason will become clear when you consider that during the execution of a page, first all native PHP code gets executed and then the results are handed over to Couch.

So, in your example, what is getting set in the variable $count is not a number (i.e. the value of 'k_page_foldertotalpagecount'). Rather it is the literal string "<cms:show k_page_foldertotalpagecount/>"
Code: Select all
<?php
    $count="<cms:show k_page_foldertotalpagecount/>";
..
?>

Which would explain the next statement -
Code: Select all
echo gettype($count);//Currently a string

which effectively becomes
Code: Select all
echo gettype("<cms:show k_page_foldertotalpagecount/>");//Currently a string

and also the last statement -
Code: Select all
$width = 100/$count; //Returns error because it is trying to divide by 0

which effectively becomes
Code: Select all
$width = 100/"<cms:show k_page_foldertotalpagecount/>";
and since the string cannot be cast to an integer, it returns 0 and hence the error message.

However, at this point you might be wondering why the intermediate statement correctly outputs a number e.g. 2 and not "<cms:show k_page_foldertotalpagecount/>" -
Code: Select all
echo $count;//Returns the correct value but only when it is a string

Actually PHP does output "<cms:show k_page_foldertotalpagecount/>" and moves on to the next statement that throws 'divide by zero' error and then the PHP block ends.

But now comes Couch's turn to process the data. Since the string above is a valid Couch statement, Couch turns it into the right integer value e.g. 2.

If you are still with me, the TLDR; is that you should avoid mixing native PHP statements with Couch statements.

The correct way is to use <cms:php> instead of <?php -
<cms:php>
$count="<cms:show k_page_foldertotalpagecount/>";
echo gettype($count);//Currently a string
echo $count;//Returns the correct value but only when it is a string
$width = 100/$count; //Returns error because it is trying to divide by 0
echo "{$width}"; //Doesn't return anything
</cms:php>

In the revised code above, since we are not using <?php .. ?>, the PHP interpreter ignores all statements and does nothing with those. Then comes Couch's turn and Couch now recognizes the <cms:php> statement and calls PHP (at which point all Couch statements within the enclosed block would have been correctly resolved).

Hope I was able to make things somewhat clearer for you :)
KK,

Thank you for explaining it to me. It makes complete sense.
3 posts Page 1 of 1