Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
I'm creating a fundraising site that displays the progress toward a goal as a percentage. I fetch the total donations from a MYSQL table and divide by the project's goal to calculate a percentage. I use an editable region in the clonable template to set the goal.

Code: Select all
<cms:php>   
   $progress = round(($total/<cms:show project_goal />)*100);
        //variable $total is fetched from a database earlier in the code
</cms:php>

This works properly, but when I try to use the variable $progress later on in the template, it's empty.
Code: Select all
<p><cms:php>echo $progress;</cms:php>% of $<cms:show project_goal /> Raised</p>

How can I make a variable created in one <cms:php> tag persist into another?

Thanks,
Tim
Hi Tim,

Please declare the $progress variable as 'global' in both the PHP blocks e.g.
Code: Select all
<cms:php>   
   global $progress;
   $progress = round(($total/<cms:show project_goal />)*100);
   ...

and
Code: Select all
<cms:php>global $progress; echo $progress; ..

Hope this helps.
Im working with something of the same character.

Im having a progress bar showing % based on the start date and end date using the following code:

Code: Select all
<cms:php>
global $CTX;
$today = new DateTime(date($CTX->get('start_date')));
$pday = new DateTime(date($CTX->get('end_date')));
$dayz = $pday->diff($today)->days;
$CTX->set( 'dayz', $dayz, 'global' );
</cms:php>


Is it possible to get some help to INCLUDE the formular so the outcome is %.

Like a project has been running for 235 days and i wanna find the % of this. The lenght of project is 390 days total.
@kimheggen

This can be done without the use of php I think.

I had some similar thing in the past maybe this can point you in the correct direction http://www.couchcms.com/forum/viewtopic.php?f=4&t=7593&p=11742&hilit=percentage#p11742

Edit:

Just playing with custom tags :

Code: Select all
    class Calculations{
   
        function get_percentage_handler( $params, $node ){
            global $FUNCS, $CTX;
            ob_start();
            if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}
           
            extract( $FUNCS->get_named_vars(
                array(
                       'full' =>'', /* full amount */
                       'curnumber' =>'', /* current number */
                      ),
                $params)
            );
           
           
            if ( $full > 0 ) { return round($curnumber / ($full / 100),2);} else {return 0;}
         
            echo get_percentage_handler($full,$curnumber);
         
            $percentof .= ob_get_contents();
            ob_end_clean();
            return $percentof;

        }   
    }
   
    $FUNCS->register_tag( 'percentage', array('Calculations', 'get_percentage_handler') );



Then you could use:
Code: Select all
<cms:percentage full='400' curnumber='3'/>

result is then 0,75
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
I did make it work :) Here is my code

Code: Select all
<cms:php>
     global $CTX;
     $today = new DateTime(date($CTX->get('end_date')));
     $pday = new DateTime(date($CTX->get('start_date')));
     $dayz = $pday->diff($today)->days;
     $CTX->set( 'dayz', $dayz, 'global' );
</cms:php>
<cms:php>
     global $CTX;
     $today = new DateTime(date($CTX->get('cur_date')));
     $pday = new DateTime(date($CTX->get('start_date')));
     $dayzz = $pday->diff($today)->days;
     $CTX->set( 'dayzz', $dayzz, 'global' );
</cms:php> 
                                             
[b]<cms:set dag_pr_prosent = "<cms:div dayz '100'/>" />
<cms:set prosent = "<cms:div dayzz dag_pr_prosent/>" />[/b]
                                           
<td class="project_progress">
     <div class="progress progress_sm">
          <div class="progress-bar bg-green" role="progressbar" data-transitiongoal="            [code]<cms:number_format prosent />[/code]">
          </div>
     </div>
<small>[b]<cms:number_format prosent />[/b]% Complete</small>
</td>
5 posts Page 1 of 1
cron