簡體   English   中英

如何從表中查找同一用戶的最新記錄?

[英]How do i find latest record of same user from a table?

我正在研究蛋糕php應用程序。 在此,我使用jqgrid來顯示我的記錄。

這是我的ctp文件:

    <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>

這是我在控制器中定義的功能

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

在展示位置表格中,學生可能有多個條目。

例如

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

我想為每個用戶顯示最新記錄。 例如,對於student_id 16,它應該獲取在2010-12-30 12:48:44創建的記錄。

我試圖通過上面的代碼實現這一點。 但我收到以下錯誤:

警告 (512): SQL錯誤: 1054:“訂單子句”中的未知列“ Placement.latest”

我無法解決問題。

請幫我

謝謝,

潘卡·庫拉納(Pankaj Khurana)

您的“展示位置”表中沒有“最新”列,或者您沒有在$ result變量中傳遞它。 也許您在find語句中指定了列,但未包含“ latest”。

嘗試放入debug($ result); 在循環內。 這將向您顯示已傳遞的數據。

順帶一提,這不是一個好主意

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

...除了語法上的錯誤外,還可能在以后導致無法預測的行為。 最好將結果作為$ results(復數)傳遞並執行

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM