Quantcast

How to do this in cakephp

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to do this in cakephp

mona-3

I have code of my controller in which i use normal php codes to fetch
data from a database and update counter how to do this in cakephp


<?php
class EntriesController extends AppController
 {
        var $name = 'Entries';
    var $helpers = array('Html','Form','Javascript','Ajax');
    var $components = array('RequestHandler');
        //var $uses=array('Entry','User');
        function index(){

---------please consider it
-----------------------------------------------------------------
    $query=mysql_query("select max(counter) from entries");
    $row=mysql_fetch_array($query);
        $co=$row[0];
        $this->set('co',$co);
        $name=$this->Session->read('User');
        $query1=mysql_query("select id from users where username='$name'");
    $row1=mysql_fetch_array($query1);
        $user_id=$row1[0];
        $this->set('user_id',$user_id);
------------------------------------------------------------------------------------------------------------------------


        $this->Entry->recursive = 1;
        $this->set('entries', $this->Entry->findAll(null, null, array
('Section.id' => 'ASC','Submenu.submenu' => 'ASC')));
    }

         function view($id = null){
         if (!$id){
         $this->Session->setFlash('Invalid id for Entry.');
         $this->redirect('/entries/index');
         }
         $this->set('entry', $this->Entry->read(null, $id));
         }

    function add(){
    $this->set('sections', $this->Entry->Section->find('list',array
('fields'=>'Section.section','Section.id')));
        if (empty($this->data)){
        $this->render();
        }
    else{
        $this->data['Entry']['name'] = $this->data['Entry']['File']['name'];
    $this->data['Entry']['type'] = $this->data['Entry']['File']
['type'];
    $this->data['Entry']['size'] = $this->data['Entry']['File']
['size'];
        if ($this->Entry->save($this->data)){
-------------------------please check it from
here-----------------------------------------------------------
        $id=mysql_insert_id();
        $query=mysql_query("select max(counter) from entries");
    $row=mysql_fetch_array($query);
    $co=$row[0]+1;
    $q=mysql_query("update entries set counter=$co where id=$id");
------------------------------------------------------------------------------------------------------------------------------
        $this->Session->setFlash('The Entry has been saved');
        }
    else{
        $this->Session->setFlash('Please correct errors below.');
        $this->redirect('/entries/add');
        }
    if (move_uploaded_file($this->data['Entry']['File']['tmp_name'],
WWW_ROOT.'/files/' .$this->data['Entry']['File']['name']))
    {
        echo "File uploaded successfully";
    }
    else{
    echo "There was an error uploading the file, please try again!";
        }
    $this->redirect('/entries/index');
    }
    }

        function edit($id = null){
        $this->set('sections', $this->Entry->Section->find('list',array
('fields'=>'Section.section','Section.id','recursive' => 1,'page' =>
1,)));
        if (empty($this->data)){
        if (!$id){
        $this->Session->setFlash('Invalid id for Entry');
        $this->redirect('/entries/index');
        }
        $this->data = $this->Entry->read(null, $id);
        }
        else{
-----------------------------------------------please
check------------------------------------------------------------------
    $query=mysql_query("select max(counter) from entries");
    $row=mysql_fetch_array($query);
        $co=$row[0]+1;
    $q=mysql_query("update entries set counter=$co where id=$id");
----------------------------------------------------------------------------------------------------------------------------------------
        if ($this->Entry->save($this->data)){
        $this->Session->setFlash('The Entry has been saved');
        $this->redirect('/entries/index');
        }
        else{
        $this->Session->setFlash('Please correct errors below.');
        }
        }
        }

    function delete($id = null){
        if (!$id){
        $this->Session->setFlash('Invalid id for Entry');
        $this->redirect('/entries/index');
        }
        if ($this->Entry->del($id)){
        $this->Session->setFlash('Record deleted successfully');
        $this->redirect('/entries/index');
        }
        }

    function update_select(){
    if(!empty($this->data['Entry']['section_id'])){
    $section_id = (int)$this->data['Entry']['section_id'];
    $options = $this->Entry->Submenu->find('list',array('section_id'=>
$section_id,'recursive' => 1,'conditions'=>array('section_id'=>
$section_id),'page' => 1,'fields'=>'Submenu.submenu'));
    $this->set('options',$options);
    }
    }

}
?>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to do this in cakephp

Thomas Allen

How to do what in CakePHP?

Thomas

On Jan 8, 7:46 am, mona <[hidden email]> wrote:

> I have code of my controller in which i use normal php codes to fetch
> data from a database and update counter how to do this in cakephp
>
> <?php
> class EntriesController extends AppController
>  {
>         var $name = 'Entries';
>     var $helpers = array('Html','Form','Javascript','Ajax');
>     var $components = array('RequestHandler');
>         //var $uses=array('Entry','User');
>         function index(){
>
> ---------please consider it
> -----------------------------------------------------------------
>     $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>         $co=$row[0];
>         $this->set('co',$co);
>         $name=$this->Session->read('User');
>         $query1=mysql_query("select id from users where username='$name'");
>     $row1=mysql_fetch_array($query1);
>         $user_id=$row1[0];
>         $this->set('user_id',$user_id);
> ------------------------------------------------------------------------------------------------------------------------
>
>         $this->Entry->recursive = 1;
>         $this->set('entries', $this->Entry->findAll(null, null, array
> ('Section.id' => 'ASC','Submenu.submenu' => 'ASC')));
>     }
>
>          function view($id = null){
>          if (!$id){
>          $this->Session->setFlash('Invalid id for Entry.');
>          $this->redirect('/entries/index');
>          }
>          $this->set('entry', $this->Entry->read(null, $id));
>          }
>
>     function add(){
>     $this->set('sections', $this->Entry->Section->find('list',array
> ('fields'=>'Section.section','Section.id')));
>         if (empty($this->data)){
>         $this->render();
>         }
>     else{
>         $this->data['Entry']['name'] = $this->data['Entry']['File']['name'];
>     $this->data['Entry']['type'] = $this->data['Entry']['File']
> ['type'];
>     $this->data['Entry']['size'] = $this->data['Entry']['File']
> ['size'];
>         if ($this->Entry->save($this->data)){
> -------------------------please check it from
> here-----------------------------------------------------------
>         $id=mysql_insert_id();
>         $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>     $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
> ------------------------------------------------------------------------------------------------------------------------------
>         $this->Session->setFlash('The Entry has been saved');
>         }
>     else{
>         $this->Session->setFlash('Please correct errors below.');
>         $this->redirect('/entries/add');
>         }
>     if (move_uploaded_file($this->data['Entry']['File']['tmp_name'],
> WWW_ROOT.'/files/' .$this->data['Entry']['File']['name']))
>     {
>         echo "File uploaded successfully";
>     }
>     else{
>     echo "There was an error uploading the file, please try again!";
>         }
>     $this->redirect('/entries/index');
>     }
>     }
>
>         function edit($id = null){
>         $this->set('sections', $this->Entry->Section->find('list',array
> ('fields'=>'Section.section','Section.id','recursive' => 1,'page' =>
> 1,)));
>         if (empty($this->data)){
>         if (!$id){
>         $this->Session->setFlash('Invalid id for Entry');
>         $this->redirect('/entries/index');
>         }
>         $this->data = $this->Entry->read(null, $id);
>         }
>         else{
> -----------------------------------------------please
> check------------------------------------------------------------------
>     $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>         $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
> ----------------------------------------------------------------------------------------------------------------------------------------
>         if ($this->Entry->save($this->data)){
>         $this->Session->setFlash('The Entry has been saved');
>         $this->redirect('/entries/index');
>         }
>         else{
>         $this->Session->setFlash('Please correct errors below.');
>         }
>         }
>         }
>
>     function delete($id = null){
>         if (!$id){
>         $this->Session->setFlash('Invalid id for Entry');
>         $this->redirect('/entries/index');
>         }
>         if ($this->Entry->del($id)){
>         $this->Session->setFlash('Record deleted successfully');
>         $this->redirect('/entries/index');
>         }
>         }
>
>     function update_select(){
>     if(!empty($this->data['Entry']['section_id'])){
>     $section_id = (int)$this->data['Entry']['section_id'];
>     $options = $this->Entry->Submenu->find('list',array('section_id'=>
> $section_id,'recursive' => 1,'conditions'=>array('section_id'=>
> $section_id),'page' => 1,'fields'=>'Submenu.submenu'));
>     $this->set('options',$options);
>     }
>     }
>
> }
>
> ?>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to do this in cakephp

Bernardo Vieira
In reply to this post by mona-3

You can use Model->query('any sql statement') to execute arbitrary sql
code. Bear in mind, however, that what you're trying to do can be done
with cake's built in methods in a much cleaner way. Model->query()  
should really be used as a last resort when cake can't handle what
you're trying to do natively. Please do take some time to read the docs
(http://book.cakephp.org/) you'll find answers to 90% of your problemas
in the first 4 chapters.


mona wrote:

> I have code of my controller in which i use normal php codes to fetch
> data from a database and update counter how to do this in cakephp
>
>
> <?php
> class EntriesController extends AppController
>  {
> var $name = 'Entries';
>     var $helpers = array('Html','Form','Javascript','Ajax');
>     var $components = array('RequestHandler');
> //var $uses=array('Entry','User');
> function index(){
>
> ---------please consider it
> -----------------------------------------------------------------
>     $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
> $co=$row[0];
> $this->set('co',$co);
> $name=$this->Session->read('User');
> $query1=mysql_query("select id from users where username='$name'");
>     $row1=mysql_fetch_array($query1);
> $user_id=$row1[0];
> $this->set('user_id',$user_id);
> ------------------------------------------------------------------------------------------------------------------------
>
>
> $this->Entry->recursive = 1;
> $this->set('entries', $this->Entry->findAll(null, null, array
> ('Section.id' => 'ASC','Submenu.submenu' => 'ASC')));
>     }
>
> function view($id = null){
> if (!$id){
> $this->Session->setFlash('Invalid id for Entry.');
> $this->redirect('/entries/index');
> }
> $this->set('entry', $this->Entry->read(null, $id));
> }
>
>     function add(){
>     $this->set('sections', $this->Entry->Section->find('list',array
> ('fields'=>'Section.section','Section.id')));
> if (empty($this->data)){
> $this->render();
> }
>     else{
> $this->data['Entry']['name'] = $this->data['Entry']['File']['name'];
>     $this->data['Entry']['type'] = $this->data['Entry']['File']
> ['type'];
>     $this->data['Entry']['size'] = $this->data['Entry']['File']
> ['size'];
> if ($this->Entry->save($this->data)){
> -------------------------please check it from
> here-----------------------------------------------------------
> $id=mysql_insert_id();
> $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>     $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
> ------------------------------------------------------------------------------------------------------------------------------
> $this->Session->setFlash('The Entry has been saved');
> }
>     else{
> $this->Session->setFlash('Please correct errors below.');
> $this->redirect('/entries/add');
> }
>     if (move_uploaded_file($this->data['Entry']['File']['tmp_name'],
> WWW_ROOT.'/files/' .$this->data['Entry']['File']['name']))
>     {
> echo "File uploaded successfully";
>     }
>     else{
>     echo "There was an error uploading the file, please try again!";
> }
>     $this->redirect('/entries/index');
>     }
>     }
>
> function edit($id = null){
> $this->set('sections', $this->Entry->Section->find('list',array
> ('fields'=>'Section.section','Section.id','recursive' => 1,'page' =>
> 1,)));
> if (empty($this->data)){
> if (!$id){
> $this->Session->setFlash('Invalid id for Entry');
> $this->redirect('/entries/index');
> }
> $this->data = $this->Entry->read(null, $id);
> }
> else{
> -----------------------------------------------please
> check------------------------------------------------------------------
>     $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
> $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
> ----------------------------------------------------------------------------------------------------------------------------------------
> if ($this->Entry->save($this->data)){
> $this->Session->setFlash('The Entry has been saved');
> $this->redirect('/entries/index');
> }
> else{
> $this->Session->setFlash('Please correct errors below.');
> }
> }
> }
>
>     function delete($id = null){
> if (!$id){
> $this->Session->setFlash('Invalid id for Entry');
> $this->redirect('/entries/index');
> }
> if ($this->Entry->del($id)){
> $this->Session->setFlash('Record deleted successfully');
> $this->redirect('/entries/index');
> }
> }
>
>     function update_select(){
>     if(!empty($this->data['Entry']['section_id'])){
>     $section_id = (int)$this->data['Entry']['section_id'];
>     $options = $this->Entry->Submenu->find('list',array('section_id'=>
> $section_id,'recursive' => 1,'conditions'=>array('section_id'=>
> $section_id),'page' => 1,'fields'=>'Submenu.submenu'));
>     $this->set('options',$options);
>     }
>     }
>
> }
> ?>
>
> >
>
>  


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to do this in cakephp

Webweave
In reply to this post by mona-3

I'm not sure I understand your question, but if you have a field you
want to save a counter in, you can do this in the model using
countCache: http://book.cakephp.org/view/445/cacheQueries

If on the other hand you need to find the count from some set of
values, you can simply use find('count'): http://book.cakephp.org/view/73/Retrieving-Your-Data

On Jan 8, 4:46 am, mona <[hidden email]> wrote:

> I have code of my controller in which i use normal php codes to fetch
> data from a database and update counter how to do this in cakephp
>
> <?php
> class EntriesController extends AppController
>  {
>         var $name = 'Entries';
>     var $helpers = array('Html','Form','Javascript','Ajax');
>     var $components = array('RequestHandler');
>         //var $uses=array('Entry','User');
>         function index(){
>
> ---------please consider it
> -----------------------------------------------------------------
>     $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>         $co=$row[0];
>         $this->set('co',$co);
>         $name=$this->Session->read('User');
>         $query1=mysql_query("select id from users where username='$name'");
>     $row1=mysql_fetch_array($query1);
>         $user_id=$row1[0];
>         $this->set('user_id',$user_id);
> ------------------------------------------------------------------------------------------------------------------------
>
>         $this->Entry->recursive = 1;
>         $this->set('entries', $this->Entry->findAll(null, null, array
> ('Section.id' => 'ASC','Submenu.submenu' => 'ASC')));
>     }
>
>          function view($id = null){
>          if (!$id){
>          $this->Session->setFlash('Invalid id for Entry.');
>          $this->redirect('/entries/index');
>          }
>          $this->set('entry', $this->Entry->read(null, $id));
>          }
>
>     function add(){
>     $this->set('sections', $this->Entry->Section->find('list',array
> ('fields'=>'Section.section','Section.id')));
>         if (empty($this->data)){
>         $this->render();
>         }
>     else{
>         $this->data['Entry']['name'] = $this->data['Entry']['File']['name'];
>     $this->data['Entry']['type'] = $this->data['Entry']['File']
> ['type'];
>     $this->data['Entry']['size'] = $this->data['Entry']['File']
> ['size'];
>         if ($this->Entry->save($this->data)){
> -------------------------please check it from
> here-----------------------------------------------------------
>         $id=mysql_insert_id();
>         $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>     $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
> ------------------------------------------------------------------------------------------------------------------------------
>         $this->Session->setFlash('The Entry has been saved');
>         }
>     else{
>         $this->Session->setFlash('Please correct errors below.');
>         $this->redirect('/entries/add');
>         }
>     if (move_uploaded_file($this->data['Entry']['File']['tmp_name'],
> WWW_ROOT.'/files/' .$this->data['Entry']['File']['name']))
>     {
>         echo "File uploaded successfully";
>     }
>     else{
>     echo "There was an error uploading the file, please try again!";
>         }
>     $this->redirect('/entries/index');
>     }
>     }
>
>         function edit($id = null){
>         $this->set('sections', $this->Entry->Section->find('list',array
> ('fields'=>'Section.section','Section.id','recursive' => 1,'page' =>
> 1,)));
>         if (empty($this->data)){
>         if (!$id){
>         $this->Session->setFlash('Invalid id for Entry');
>         $this->redirect('/entries/index');
>         }
>         $this->data = $this->Entry->read(null, $id);
>         }
>         else{
> -----------------------------------------------please
> check------------------------------------------------------------------
>     $query=mysql_query("select max(counter) from entries");
>     $row=mysql_fetch_array($query);
>         $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
> ----------------------------------------------------------------------------------------------------------------------------------------
>         if ($this->Entry->save($this->data)){
>         $this->Session->setFlash('The Entry has been saved');
>         $this->redirect('/entries/index');
>         }
>         else{
>         $this->Session->setFlash('Please correct errors below.');
>         }
>         }
>         }
>
>     function delete($id = null){
>         if (!$id){
>         $this->Session->setFlash('Invalid id for Entry');
>         $this->redirect('/entries/index');
>         }
>         if ($this->Entry->del($id)){
>         $this->Session->setFlash('Record deleted successfully');
>         $this->redirect('/entries/index');
>         }
>         }
>
>     function update_select(){
>     if(!empty($this->data['Entry']['section_id'])){
>     $section_id = (int)$this->data['Entry']['section_id'];
>     $options = $this->Entry->Submenu->find('list',array('section_id'=>
> $section_id,'recursive' => 1,'conditions'=>array('section_id'=>
> $section_id),'page' => 1,'fields'=>'Submenu.submenu'));
>     $this->set('options',$options);
>     }
>     }
>
> }
>
> ?>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to do this in cakephp

mona-3


I know how to handle queries but i don't know how to fetch values of
variables for example
i wan't to fetch max(counter) so i know how to write query but how to
store value of this in some variable and print them in my index view
for eg
    $query=mysql_query("select max(counter) from entries");
how to fetch max value from database of some field in cakephp how to
write this query in cakephp
$row=mysql_fetch_array($query);
        $co=$row[0]+1; '
what to use instead of mysql_fetch_array in cakephp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to do this in cakephp

Webweave

$count = $this->Entry->find('all', array('fields' => array('(max
(Entries.counter)) as max_counter'));

Will return an array with the max_counter value in it.

On Jan 8, 8:29 am, mona <[hidden email]> wrote:

> I know how to handle queries but i don't know how to fetch values of
> variables for example
> i wan't to fetch max(counter) so i know how to write query but how to
> store value of this in some variable and print them in my index view
> for eg
>     $query=mysql_query("select max(counter) from entries");
> how to fetch max value from database of some field in cakephp how to
> write this query in cakephp
> $row=mysql_fetch_array($query);
>         $co=$row[0]+1; '
> what to use instead of mysql_fetch_array in cakephp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to do this in cakephp

mona-3

this will help me but it is not returning value it is returning only
Array as value please help me how to solve this porblem
-----------see this ----------------------------------
Counter : Array

function index(){
        $count = $this->Entry->find('all', array('fields' => array('(max
(Entry.counter)) as max_counter')));


        $this->set('count',$count);
        $name=$this->Session->read('User');
        $query1=mysql_query("select id from users where username='$name'");
    $row1=mysql_fetch_array($query1);
        $user_id=$row1[0];
        $this->set('user_id',$user_id);
        $this->Entry->recursive = 1;
        $this->set('entries', $this->Entry->findAll(null, null, array
('Section.id' => 'ASC','Submenu.submenu' => 'ASC')));
    }

It is returning value as counter
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...