0

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; 

?>
1
  • Do you mean how to set variables from controller to use in default.ctp page ? Commented Nov 12, 2016 at 13:42

2 Answers 2

2

Create beforeRender() method in AppController

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

$a variable will be available in templates

Sign up to request clarification or add additional context in comments.

Comments

0

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);
}

11 Comments

No I am getting some error in my default.ctp page.Actually I have created a sql query in the UsersController like this public function orderCount() { //For 24 hrs Order check for all orders $counts = $this->User->query("select * FROM carts LEFT JOIN checks ON carts.orderid = checks.order_id AND carts.exam_name = checks.exam WHERE checks.order_id is null AND checks.exam is null AND carts.order_date < NOW() - INTERVAL 1 DAY"); $this->set('count',$counts); }
In deafult.ctp page I have to pass the variable count <?php$a = 0; for($j=0;$j<count($count);$j++) { $orderDate = $count[$j]['carts']['order_date']; $currentDate = $dateTime; $diff = strtotime($currentDate) - strtotime($orderDate); $hour = $diff/(60*60); if($hour>24) { $a++; } } return $a;
but I am getting fatal error in the default.ctp page on line 120
My doubt is without creating Controller and Model for deafult.ctp page how can i pass "count" variable like this $this->set('count',$counts); into the default.ctp page
If you do not want to create a model or controller you can always use AppController.php and can create your above function ( which contains the sql query ) there, since you are using query function you do not have to worry about the model also, So in my solution in countOccurences function you can call the query and can then access that in default.ctp. Although its not a good practice. I would suggest you to paste the error trace here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.