如何从表中查找同一用户的最新记录?

发布于 2024-10-09 22:55:33 字数 3800 浏览 4 评论 0原文

我正在开发一个 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:“order 子句”中的未知列“Placement.latest”

我无法整理的问题。

请帮我解决这个问题

,谢谢

Pankaj Khurana

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.

Please help me on this

Thanks,

Pankaj Khurana

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖风昔人 2024-10-16 22:55:33

您的“展示位置”表没有“最新”列,或者您没有在 $result 变量中传递它。也许您在查找语句中指定了列,但未包含“最新”。

尝试放置 debug($result);循环内。这将显示已传递的数据。

顺便说一句,这不是一个好主意

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

……除了语法上不正确之外,它还可能导致以后出现不可预测的行为。最好将结果作为 $results (复数)传递并执行

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

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)
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文