Ragith Thomas
Ragith Thomas

Reputation: 105

php code not working in default.ctp page in Cakephp2.8.5

Thanks in advance, I am new to cakephp, i am using cakephp2.8.5 version. Actually i want to write a php code for counting number of records from the mysql database table comparing the ordered date column date values with the current date. I have written the code but my menus are in default.ctp page. In Order Check menu i have to show the count in numbers. default.ctp page lying in app/view/Layout/default.ctp so how to create a count value in php code without using controller.

My code will compare the current date with the table column date and calculates the count.How can i pass the variable $ordCounts into default.ctp page without creating controller page Which is as below:

<?php                     

$a = 0; 

for($j=0; $j<count($ordCounts) ;$j++)   
{
    $orderDate = $ordCounts[$j]['carts']['order_date'];          
    $currentDate = $dateTime;        
    $diff = strtotime($currentDate) - strtotime($orderDate);         
    $hour = $diff/(60*60);                             
    if($hour>24)  
    {
        $a++;           
    }
} 

echo $a; 

?>

Upvotes: 0

Views: 170

Answers (2)

Ankush Tanwar
Ankush Tanwar

Reputation: 239

you can create a function of the above code which counts the occurences in AppController like this

function countOccurences(){
    $a = 0; 
    for($j=0; $j<count($ordCounts) ;$j++)   
    {
        $orderDate = $ordCounts[$j]['carts']['order_date'];          
        $currentDate = $dateTime;        
        $diff = strtotime($currentDate) - strtotime($orderDate);         
        $hour = $diff/(60*60);                             
        if($hour>24)  
        {
            $a++;           
        }
    } 
    return $a;
}

and then call this function in your beforeFilterMethod in AppController

function beforeFilter(){
    parent::beforeFilter();
    $count = $this->countOccurences();
    $this->set('count',$count);
}

Upvotes: 0

Bart
Bart

Reputation: 1268

Create beforeRender() method in AppController

public function beforeRender(){
    parent::beforeRender();
    //here your code
    $this->set('a',$a);
}

$a variable will be available in templates

Upvotes: 2

Related Questions