dtlh12053 2013-01-22 13:50
浏览 42
已采纳

从模型返回单个变量值(字符串)并将其填入文本视图字段 - Codeigniter

I have the following Model, Controller and view. Using PHP Point of Sale 11.3. I'm trying to take the max value in a DB column and display it in a text field in view.

Model:

function getmax()
{
    $query = $this->db->query("SELECT max(item_id) FROM phppos_items");
    return $query->row();
}

Controller:

function view()
{   
    $data['max_id']=$this->Item->getmax();      
    $this->load->view("items/form",$data);
}

View:

<?php 

    echo $max_id; //line 16. Error Here.

    echo form_input(array(
                        'name'=>'item_number',
                        'id'=>'item_number',
                        'value'=>set_value('item_number',$max_id)) //is this right??
                    );
?>

Am getting two errors when execution.

A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: items/form.php
Line Number: 16

A PHP Error was encountered
Severity: Warning
Message: htmlspecialchars() expects parameter 1 to be string, object given
Filename: helpers/form_helper.php
Line Number: 625

I'm getting the max_id without any issues when used as dropdown in view.

<?php echo form_dropdown('max_id',$max_id);?>

But I need this value to be filled in default in the form text field. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douzhi2017 2013-01-22 13:54
    关注

    It should have been like this

    Model

    function getmax()
    {
        $query = $this->db->query("SELECT max(item_id) as MaxID FROM phppos_items");
        return $query->row();
    }
    

    Controller

    function view()
    {   
        $data['row']=$this->Item->getmax();      
        $this->load->view("items/form",$data);
    }   
    

    View

    $options['name']    =   'item_number';
    $options['id']      =   'item_number';
    $options['value']   =   set_value('item_number',$row->MaxID);
    
    echo form_input($options);  
    

    You see i am using row

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧