Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7914755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:13:17+00:00 2026-06-03T14:13:17+00:00

Iam trying to return json data from codeigniter view. but my code is not

  • 0

Iam trying to return json data from codeigniter view. but my code is not working.i have tried all options given in this site. but i could not able to return json data from view
this is controller code.

<?php
class Json extends CI_Controller
{

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

    }
    public function index()
    {
        $this->load->model('get_json');
        $data['message']=$this->get_json->get_fruits();

         if ($data!== false)
         {
                    $this->load->view('json',json_encode($data['message']));
        }
    }
}

my model

<?php

    class get_json extends CI_Model
    {
        function __construct()
        {
           parent::__construct();
        }


        function get_fruits()
        {
            $sql = "SELECT * FROM fruit where name='Apple'";
            $query = $this->db->query($sql);
            // Fetch the result array from the result object and return it
            return $query->result();

        }

    }

View code:

<?php
$this->output->set_content_type('application/json');
echo $message;

Please help me wwhere my code went wrong.

This is what iam getting in my logs:

ERROR - 2012-05-11 15:28:40 --> Severity: Notice  --> Undefined variable: message C:\xampp\htdocs\ci\system\core\Loader.php(829) : eval()'d code 3
DEBUG - 2012-05-11 15:28:40 --> File loaded: application/views/json.php
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:13:19+00:00Added an answer on June 3, 2026 at 2:13 pm

    Change your controller index method to this:

    public function index()
        {
            $this->load->model('get_json');
            $data['message']= json_encode($this->get_json->get_fruits());
    
             if ($data!== false)
             {
                        $this->load->view('json',$data);
            }
    }
    

    And you should be good to go: you need to pass the array to $this->load->view, not the encoded value.

    EDIT: as a side note, for this sort of thing you don’t even NEED a view. You can return the result directly from the controller:

    public function index()
        {
            $this->load->model('get_json');
            $message = $this->get_json->get_fruits();
    
             if ($message !== false)
             {
                        $this->output->set_content_type('application/json');
                        echo json_encode($message);
            }
    }
    

    Cleaner, puts everything in one place, and slightly more efficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to return a JSON object from an aspx page using Jayrock.JSON.
i am noticing a error when im trying to return json from a fetchAll.
I am trying to return an array Data Member from one smaller 2D Array
I am trying to populate a UITableView with data from json result. I can
I am trying to return an html table from a asp.net web service but
I am trying to return data from the server via a JsonResult in mvc.
I am trying to pass JSON-formatted data from my Android WebView to a HTML
I am trying to pass JSON data from the client browser to an ASP.NET
I am trying to send JSON data from a form using the XMLHttpRequest object.
I am trying to make an ajax sending data in JSON from a partial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.