Pankaj Khurana
Pankaj Khurana

Reputation: 3271

How to find the latest record for a specific user in a database table?

I am working on a cake php application. In this i am using jqgrid for displaying my records.

This is my ctp file:

    <script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#list").jqGrid(
        {
            url:'<?php if(!empty($this->params["named"]["batch"]))echo $this->Html->url(array("controller" => "placements", "action" => "report17", "ajax" => true,"batch"=>$this->params["named"]["batch"])); else echo $this->Html->url(array("controller" => "placements", "action" => "report17", "ajax" => true)); ?>',
            datatype: "json",
            mtype: "GET",
            colNames:['Student','Phone No.','Batch','Created'],
            colModel:[
                {name:'studentname',index:'studentname', width:30, search:true},
                {name:'contactnumber1',index:'contactnumber1', width:20, search:true},
                {name:'batchname',index:'batchname', width:30, search:true},
                {name:'latest',index:'latest', width:30, search:true},
                
            ],
            rowNum:10,
            rowList:[10,20,30],
            pager: jQuery('#pager'),
            sortname: 'latest',
            viewrecords: true,
            sortorder: "desc",
            caption:"Placements",
            height:"auto",
            autowidth: true,
            navigator:true
        });
        jQuery("#list").navGrid("#pager",{edit:false,add:false,del:false,search:false});
        jQuery("#list").jqGrid('filterToolbar');
    })
</script>

And this is my function defined in controller

 $conditions = array();
            if(!empty($this->params['named']['batch']))
            array_push(&$conditions, array('Batch.id' => $this->params['named']['batch'] ));
            array_push(&$conditions, array('Student.type' => 'student' ));
            array_push(&$conditions, array('Student.trainingcompleted' => '1' ));
            $result = $this->Placement->find('all', array(
                'fields' => array('id','student_id','company_id','branch_id','max(Placement.created) as latest'),
                'link' => array(
                    'Student' => array(
                        'Batch'
                    ),
                    'Company'
                ),
                'group'=>'Placement.student_id',
                'conditions'=>$conditions,
                'order' => $sort_range,
                'limit' => $limit_range
            ));
        }

        $i = 0;
        $response->page = $page;
        $response->total = $total_pages;
        $response->records = $count;
        
        //pr($result);

        foreach($result as $result)
        {
            $response->rows[$i]['id'] = $result['Placement']['id'];

            $student = "<a href='" . APP_URL . "students/view/" . $result['Student']['id'] . "/by:student'>" . $result['Student']['fullname'] . "</a>";
            $batch = "<a href='" . APP_URL . "batches/view/" . $result['Batch']['id'] . "'>" . $result['Batch']['name'] . "</a>";
            $contactnumber1 =$result['Student']['contactnumber1'];

            $response->rows[$i]['cell'] = array($student, $contactnumber1, $batch,$result['Placement']['latest']);
            $i++;
        }

        echo json_encode($response);

In placements table there may be more than one entry for student.

For e.g.

id student_id istillworking created
 1   16        no           2010-09-07 10:02:16
 2   16        yes          2010-12-30 12:48:44

I wanted to display the latest record for each user. For e.g.- for student_id 16 it should fetch the record created on 2010-12-30 12:48:44.

I tried to implement this through my above code. But i am getting the following error:

Warning (512): SQL Error: 1054: Unknown column 'Placement.latest' in 'order clause'

I am unable to sort out the issue.

Upvotes: 0

Views: 402

Answers (1)

Leo
Leo

Reputation: 6571

Either your 'placements' table does not have a column 'latest' or you are not passing it through in the $result variable. Perhaps you have specified columns in your find statement and not included 'latest'.

Try putting debug($result); inside the loop. This will show you the data that has been passed.

Incidentally, it's not a good idea to do

foreach($result as $result)
{
    ...
}

...apart from being grammatically incorrect, it may lead to unpredictable behaviour later. Better to pass the result as $results (plural) and do

foreach($results as $result)
{
    ...
}

Upvotes: -1

Related Questions