0

I just need a little help here about displaying my table query result. When i checked the response from my ajax I shows. But when passing to the views it doesn't shows. Here's my code:

Controller/user.php

        <?php

        class User extends CI_Controller{

            public function __construct(){
                parent::__construct();
                $this->load->model('user_model');
            }

            public function index(){
                $this->load->view( 'index' );
            }

            public function read() {
                echo json_encode( $this->user_model->getAll() );
            }


        }

    ?>

Model/user_model.php

        <?php

        class User_Model extends CI_Model{

            public function __construct(){
                parent::__construct();
            }

            public function getAll(){

                $qGetAllData = $this->db->get('user');
                if($qGetAllData->num_rows() > 0){
                    return $qGetAllData->result();
                }else{
                    return array();
                }

            }

        }

    ?>

    View/index.php

        <html>
      <head>
        <title><?php echo $title; ?></title>
        <base href="<?php echo base_url(); ?>" />
        <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
        <link type="text/css" rel="stylesheet" href="css/styles.css" />
      </head>

      <body>

        <table id="records"></table>

        <div id="tabs">

          <ul>
            <li><a href="#read">Read</a></li>
            <li><a href="#create">Create</a></li>
          </ul>

          <div id="read">
            <table id="records"></table>
          </div>

          <div id="create"></div>

        </div>




        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-templ.js"></script>

        <script type="text/template" id="readTemplate">
          <tr>
            <td>${id}</td>                  //doesn't display ID
            <td>${name}</td>                //doesn't display Name
            <td>${email}</td>               //doesn't display EMial
          </tr>
        </script>

        <script type="text/javascript" src="js/myjs.js"></script>

      </body>

    </html>

Javascript/myjs.js

            $(document).ready(function(){

        $( '#tabs' ).tabs({
                fx: { height: 'toggle', opacity: 'toggle' }
            });

            $.ajax({
                url: 'index.php/user/read',
                datatype: 'json',
                success: function(response){
                    $('#readTemplate').render(response).appendTo('#records');
                }
            });

        });

That's all guys. I just don't know where's my error.

3
  • What is the function render() in your js code? Commented Sep 23, 2013 at 7:01
  • part of template plugin which iterates over the response returned from server and insert each value in place of template variables Commented Sep 23, 2013 at 7:11
  • Try this one by calling die() like echo json_encode( $this->user_model->getAll() );die(); Commented Sep 23, 2013 at 7:12

1 Answer 1

1

I think you have issue with just render data.

please review this link for reference.

Please try below code.

Your html : View/index.php
<html>
  <head>
    <title><?php echo $title; ?></title>
    <base href="<?php echo base_url(); ?>" />
    <link type="text/css" rel="stylesheet" href="css/smoothness/jquery-ui-1.8.2.custom.css" />
    <link type="text/css" rel="stylesheet" href="css/styles.css" />
  </head>
  <body>
    <table id="records"></table>
    <div id="tabs">
      <ul>
        <li><a href="#read">Read</a></li>
        <li><a href="#create">Create</a></li>
      </ul>
      <div id="read">
        <table id="records"></table>
      </div>
      <div id="create"></div>
    </div>
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-templ.js"></script>
    <script type="text/javascript" src="js/myjs.js"></script>
  </body>
</html>

Your JS : js/myjs.js
var readtpl = "<tr><td>${id}</td><td>${name}</td><td>${email}</td></tr>";
$.template( "readTemplate", readtpl );
 $(document).ready(function(){

    $( '#tabs' ).tabs({
            fx: { height: 'toggle', opacity: 'toggle' }
        });

        $.ajax({
            url: 'index.php/user/read',
            datatype: 'json',
            success: function(response){
                $.tmpl( "readTemplate", response ).appendTo( "#records" );
            }
        });

    });
Sign up to request clarification or add additional context in comments.

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.