0

i am in this situation,

//view

 $.ajax({type: 'POST',
    url: base_url+"home/display_info/"+patient_id,
    async: false,
    success: function(data){
            //alert(data);// alert was working
            }
    });

//controller

function display_info($id)
    {
        $document= $this->document_model->getDocumentOfPatient($id);
        print_r($document);
    }

in this i am getting the data as an array from the controller, and i want to get the data to a php array variable to build a table(html) with that array,but stuck here, is there any way to set a table(html) with this returned data variable, can i access the variable <?php echo $document['document_id'];?> like this in the view.

3
  • You can build the table in PHP, then append it to your HTML with the success function. Commented Aug 20, 2013 at 12:08
  • show your alert(data) Commented Aug 20, 2013 at 12:09
  • @Nathan Array ( [patient_id] => 2122 [document_id] => 6 [document_date] => 1970-01-01 [document_class] => 0 [file_type] => [note_type] => [document_title] => [author_user_id] => 0 [insert_user_id] => 20 [insert_dts] => 2013-08-17 14:42:00 [update_user_id] => 0 [update_dts] => 2013-08-17 14:42:00 [review_user_id] => 0 [review_dts] => 2013-08-17 14:42:00 [signoff_user_id] => 0 [signoff_dts] => 2013-08-17 14:42:00 [file_path] => [comment] => 0 ) Commented Aug 20, 2013 at 12:20

4 Answers 4

2

Try this

First you create table in your view page. Table id name foo and use to create table row and append to the html table

Sample code is given below

    <scrit type="text/javascript">
     $.ajax({type: 'POST',
        url: base_url+"home/display_info/"+patient_id,
        async: false,
        success: function(data){
              var table = '<tr><td>' + data['patient_id'] + '</td><td>' + data['document_id'] + '</td><td>' + data['document_date'] + '</td><td>'+ data['insert_user_id']+  '</td></tr>';

        $('#poo > tbody').append(table);
                }
        });


    </script>
    <table id="poo" style="margin:10px 0px 0px 0px;" width="100%" border="0" cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <td><strong>Product id</strong></td>
                <td><strong>Doc id</strong></td>
                <td><strong>Date</strong></td>
                <td><strong>userid</strong></td>
            </tr>
        </thead>
<tbody>
</tbody>   
    </table>
Sign up to request clarification or add additional context in comments.

Comments

1

PHP is a server-side language. If you want to use PHP data in your view, you need to convert it to a client-side language like Javascript. For example, in your display_info controller, you could return some JSON, using PHP's json_encode to convert a PHP array made of useful data for your view. Output it with the application/json content-type header.

Comments

0

In this kind of situation I used to make table in controller itself and assign it to variable. So you can get that table in View as AJAX Response. Then its very simple to assign response to inner HTML of resource Id where its required to display.

Comments

0

Does the data correctly ?

var obj = jQuery.parseJSON(data);

use JSON in the form of.I hope the right understand

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.