dpauf28808 2019-07-09 07:53
浏览 46
已采纳

想从sql db获取图像src并通过ajax codeigniter显示视图文件

I am new to php... I have developed ajax dynamic dependent dropdown in codeigniter and it works, what i want is to when the last dropdowns's option is select it should show the image for selected option plz help

view and ajax:

<div id="student_img">
<img src="" class="student_img">    
</div>
                <script>$('#student').change(function()
        {
            var s_id = $('#student').val();

            if(s_id != ''){
                $.ajax({
                    url:"<?php echo base_url(); ?>dynamic_controller/fech_student_img",
                    method:"POST",
                    data:{s_id:s_id},
                    success:function(data){
                        $('#student_img').html(data);
                    }
                })
            }


        });</script>

controller:

public function fech_student_img(){
        if($this->input->post('s_id'))
        {
            echo $this->dynamic_model->fech_student_img($this->input->post('s_id'));
        }
    }

Model ftn:

public function fech_student_img($s_id){
        $query = $this->db->select('s_img')->from('student')->where('s_id',$s_id)->get();
        $q = $query->result();
        $output = '<img src="'.$q.'">';
        return $output;
    }

Message: Array to string conversion

Filename: models/dynamic_model.php

  • 写回答

1条回答 默认 最新

  • douyingp82418 2019-07-09 08:05
    关注

    The error

    Array to string conversion

    means exactly what it sounds like: You are trying to use an array as a string. In this code...

    $query = $this->db->select('s_img')->from('student')->where('s_id',$s_id)->get();
    $q = $query->result();
    $output = '<img src="'.$q.'">';
    

    ...$q will result in an array of all rows in your database that match your query. If you only want to return one row, your code should look something like this, depending your data-structure:

    $query = $this->db->select('s_img')->from('student')->where('s_id',$s_id)->get();
    $row = $query->row();
    $output = '<img src="'.$row->s_img.'">';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?