0

I just want to fetch data from database dynamically so I've used onclick ajax method to fetch data from database without reloading.. Also I want to display it in a Modal CSS..

<td><a style="cursor:pointer;" data-toggle="modal" data-target="#myModal" onclick="get_report(<?php echo $det->user_id; ?>);">Today Report</a>
                            <div class="modal fade" id="myModal" role="dialog">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                            <h4 class="modal-title">Today Report</h4>
                                        </div>
                                        <div class="modal-body">
                                            <div id="rep_id"></div>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>

At the Bottom,

function get_report(sales_id)
{ 
    $.ajax({
            url: "<?php echo base_url()."Sales_exec/get_report/"; ?>",
            method: "post",
            data : { sales_id : sales_id }
        }).done(function(response){ 
               $('.modal-body').html(response);
        }); 
}
</script>

In my controller,

public function get_report()
{
    $sales_id = $this->input->post('sales_id'); 
    $response = $this->Sales_exec_model->today_sales_report($sales_id);
}

And my model is,

public function today_sales_report($id)
    {
        $query = $this->db->query('SELECT * FROM tbl_reports WHERE sales_id="'.$id.'" AND DATE(created) = CURDATE()');
        if ($query->num_rows() == 1)
        {
            $report['rep']= $query->row();
            print_r($report);die;
        }
    }

And I'm getting output in the body of the modal as:

Array ( [rep] => stdClass Object ( [rep_id] => 3 [client_name] => joyner [address] => client_address [area] => client_area [sales_id] => 1 [sales_head_id] => 85 [comment] => 0 [pay_by] => cash [amt_value] => 1.00 [created] => 2016-12-22 ) )

Can anyone help me to get these data separated from array to display it?

  • Use `$report['rep'] = $query->row_array();` to return an array. If you want to return HTML call a view. [Otherwise return json](http://stackoverflow.com/a/18821655/3585500). – ourmandave Dec 22 '16 at 21:28
  • if I call a view then the page will get reloaded to display data from database. I want to display data from database into CSS Modal by clicking a link. – Ganesh Karthikeyan Dec 23 '16 at 05:12
  • The view would only echo back the html to your ajax call, so it wouldn't do a page reload. If you wanted to [you can call a view so it returns the output to a variable](http://www.codeigniter.com/user_guide/general/views.html#returning-views-as-data) and then return that. `$report['rep'] = $query->row_array(); $result = $this->load->view('myView', $report, TRUE); echo $result;` – ourmandave Dec 23 '16 at 12:21

1 Answers1

0

Change in your modal $report['rep'] to $report.Because u are saving values at $report['rep'] and try to get the values from $report. SO change in your modal and change your $report['rep'] to $report this will solve your problem.