I did a REST app + codeigniter + database following: https://github.com/chriskacerguis/codeigniter-restserver , works perfect, wonderful.
Right now my json file getting static data, how can i get data from database?
i wrote a file and i can get from database the data, but i have no idea how to get the data from database in json, can you guys help me? thank you.
That file return to me that static json, works perfect, I would like get data from database.
/controllers/hello.php
<?php
include (APPPATH.'/libraries/REST_Controller.php');
class Hello extends REST_Controller {
function world_get(){
$data = new stdClass();
$data->name = 'Mark ';
$this->response($data, 200);
}
}
These files below I can get data from database but in html, how can I get data in json format?
/controllers/site.php
<?php
Class Site extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function index(){
$this->load->model('data_model');
$data['rows'] = $this->data_model->getAll();
$this->load->view('home', $data);
}
}
/models/data_model.php
<?php
class Data_model extends CI_Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data [] = $row;
}
return $data;
}
}
}
views/home.php
<htmL>
<body>
<pre>
<?php foreach ($rows as $r)
{
echo '<h1>' . $r->title . '</h1>';
}
?>
</pre>
<?php foreach ($rows as $r) : ?>
<h1> <?php echo $r->author; ?></h1>
<h1> <?php echo $r->contents; ?></h1>
<?php endforeach; ?>
</body>
</htmL>
You are making API.To return data you don't need to call view.You can do it simply this way
function index()
{
$this->load->model('data_model');
$data = $this->data_model->getAll();
$this->response($data, 200);
}
Your model function is OK but you can make it simple.
function getAll()
{
return $this->db->get('data')->result();
}
just use json_encode in your controller and little change in your model file
Controller
function index(){
$this->load->model('data_model');
$result= $this->data_model->getAll();// change this line
$data['rows']= json_encode($result);// add this line
$this->load->view('home', $data);
}
Model
<?php
class Data_model extends CI_Model {
function getAll(){
$q = $this->db->query("SELECT * from data");
if($q->num_rows() > 0) {
return $q->result();
}else{
return false;
}
}
}
Related
I want to get data from the database and display it on a webpage using CodeIgniter. I coded my controller, model and view as follows.
Controller;
//HomeController
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model;
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
return $query->unbuffered_row('object');
}
}
?>
View;
//HomeView
<?php
echo "Recoeds from database<br>";
while($records)
{
echo $records->name." ".$records->age."</br>";
}
?>
But this code doesn't print anything on the screen.(echo "Records from database<br>"; )
So I tried the following code given in the CodeIgniter documentation and echoed the result in the model itself rather than return it to the controller and then to the views.It worked fine.
//HomeModel
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$query = $this->db->query('SELECT * FROM data');
while ($row = $query->unbuffered_row())
{
echo $row->name;
echo $row->age;
}
}
}
?>
My question is how do we return the result of the unbuffered_row() method into the controller and then to the view as per MVC architecture? We can get the output by echoing result at the model itself but it is against the purpose of the MVC architecture.
You should use return $query->result(); and then get the right object in the view or controller (that's up to you).
Try This One
controller
<?php
class HomeController extends CI_Controller
{
public function index()
{
$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}
?>
Model
<?php
class HomeModel extends CI_Model
{
public function getData()
{
$data = $this->db->query('SELECT * FROM data');
return array('count'=>$data->num_rows(), 'data'=>$data->result(),'first'=>$data->row());
}
}
?>
View
<?php
echo "Recoeds from database<br>";
foreach($record['data'] as $row)
{
echo $row->name." ".$row->age;
}
?>
I simulated your code in local.Your mistake is here.$records is object .And your while loop has not condition for leaving the loop
<pre>
<?php
var_dump($records);
echo "Recoeds from database<br>";
foreach($records as $value) {
echo $value->name."<br>";
echo $value->age."<br>";
}
?>
</pre>
Sorry for the basic question, but I couldn't fidn the answer anywhere online. I started with codeigniter yesterday And all I am trying to do is selecting data from my database. However I don't get any errors nor does it show any data. If i change the database tabel name. It immediately throws an error.
Model
<?php
class Customer_model extends CI_Model {
public function get_customers($id) {
if ($id != FALSE) {
$query = $this->db->get_where('customer', array('id' => $id));
} else {
return FALSE;
}
}
}
Controller
<?php
class Customer extends CI_Controller {
public function show($id) {
$this->load->model('customer_model');
$news = $this->customer_model->get_customers($id);
$data['customer_name'] = $news['customer_name'];
$data['streetname'] = $news['streetname'];
$this->load->view('customers', $data);
}
}
VIEW
<?php echo "hallo"; ?>
<?php echo $customer_name; ?>
** EDIT, in my page is can see the echo of hello. So I am in the correct page, but there are no errors nor notices of any data missing.
If anyone have any idea what I'm doing wrong, thanks a zillion. btw database connectivity is included and also the package is loaded.
thanks
In Model
class Customer_model extends CI_Model {
public function get_customers($id) { # refactored
$this->db->select(*);
$query = $this->db->get_where('customer', array('id' => $id));
$result = $query->result_array();
$count = count($result);
if(empty($count)){
return false;
}
else{
return $result;
}
}
}
In Controller
class Customer extends CI_Controller {
public function show($id) {
if(empty($id)) # Added
{
echo "Token Invalid";
}
else{
$this->load->model('customer_model');
$news = $this->customer_model->get_customers($id);
if($news == false){ # Added
echo "No result Found";
}
else{
$data['customer_name'] = $news[0]['customer_name']; # Changed
$data['streetname'] = $news[0]['streetname']; # Changed
$this->load->view('customers', $data);
}
}
}
}
table is not displaying any value in table. When i did var_dump($results). It printed NULL. Since, i am new to codeigniter, i am not able to solve this problem.
I'm not getting, where i did mistake. Help Me.
View Page
<tbody>
<?php
if(!empty($results) ) {
foreach($results as $row) {
echo '<tr>';
echo '<td>'." ".'</td>';
echo '<td>'.$row->FileTitle.'</td>';
echo '<td>'.$row->UploadedFile.'</td>';
echo '<td>'.$row->CreationDate.'</td>';
echo '<td>'.$row->UpdateDate.'</td>';
echo '<td>'." ".'</td>';
echo '</tr>';
}
}?>
</tbody>
Controller
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function member_CAttachments()
{
$data['results'] = $this->news_model->member_MAttachments();
$this->load->view('member/templates/header');
$this->load->view('member/index',$data);
$this->load->view('member/templates/footer');
}
}
Model
class News_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function member_MAttachments()
{
$results = array();
$query = $this->db->get('MemberFileDetails');
if($query->num_rows() > 0)
{
$results = $query->result();
}
return $results;
}
}
Instead you can do it in fewer lines with inbuilt CI Table library,
supports both 2.2 and 3.x versions.
Use it like,
$this->load->library('table');
$this->table->set_heading('FileTitle', 'UploadedFile', 'CreationDate', 'UpdateDate');
$query = $this->db->query('SELECT * FROM MemberFileDetails');
echo $this->table->generate($query);
do you have records in your table ? if yes then please try to remove the
if($query->num_rows() > 0) condition in the model and try.
remove $results = array(); in your code
I'm working on simple application consisting of simple form. My code really works fine in local environment. But when i uploaded it on a live server gives an error, unable to fetch database fields and i think there is an error in my model.
Here is my model class
class Model_get extends CI_Model
{
function getData($page) {
$query = $this->db->get_where('ci_tbl', array('page' => $page));
print_r($query->result());
return $query->result();
}
}
Here goes my view
<div id="content">
<?php
foreach ($results as $row) {
$title = $row->title;
$para1 = $row->para1;
$para2 = $row->para2;
}
echo heading($title, 1);
?>
<p><?php echo $title;?></p>
<p><?php echo $para1;?></p>
<p><?php echo $para2;?></p>
</div>
My controller goes as
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index()
{
$this->home();
}
public function home() {
$this->load->model('model_get');
$data['results'] = $this->model_get->getData('home');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('main_content',$data);
$this->load->view('footer');
}
public function about() {
$this->load->model('model_get');
$data['results'] = $this->model_get->getData('about');
$this->load->view('header');
$this->load->view('nav');
$this->load->view('about_page',$data);
$this->load->view('footer');
}
Make sure you actually return the results of the query:
//print_r($query->result());
return $query->result();
At the moment you've commented out return $query->result(); and you're just printing the result. The controller calling the model isn't going to get that information and therefore isn't passing through to the view.
Also check that the database connection is correct if moving from local to a public environment.
Actually you had an error
foreach ($results as $row) {
$title=$ row->title;
^^^^
It should be
$title = $row->title;
^^^
You need to update your query within model as
class Model_get extends CI_Model {
function getData($page) {
$query = $this - > db - > get_where('ci_tbl', array('page' => $page));
return $query->result_array();
}
}
I am currently new to database query and other for codeigniter can not seem to quite get model working for what I am after.
I have tried loading db in direct to controller and that works OK. But if I call my model does not want to work nothing shows up.
Here is what I would like to achieve.
Controller
public function website_test_model() {
$this->load->model('admin/website/model_website');
$results = $this->model_website->getWebsites();
$data['websites'] = array();
foreach ($results as $result) {
$data['websites'] = array(
'website_id' => $result['website_id'],
'name' => $result['name'],
'url' => $result['url']
);
}
$this->load->view('website/websites', $data);
}
View
<!-- Not Working From Controller Function Model Test -->
<?php if ($websites) { ?>
<?php foreach ($websites as $website) { ?>
<?php echo $website['website_id'];?>
<br>
<?php echo $website['name'];?>
<br>
<?php echo $website['url'];?>
<?php } ?>
<?php } ?>
Model
<?php
class Model_website extends CI_Model {
function getWebsites() {
$this->db->select('*');
$this->db->from('website');
$this->db->where('website_id');
$this->db->where('name');
$this->db->where('url');
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query->row();
} else {
return false;
}
}
}
Codeigniter Demo It works when I try it this way as said on codeigniter User Guide. Just the code above is way I am after though Trying to make model work no luck
public function website() {
$results = $this->db->get('website'); // Works Direct From Controller OK.
foreach ($results->result() as $row) {
$data = array(
'website_id' => $row->website_id,
'name' => $row->name,
'url' => $row->url
);
}
$this->load->view('website/website', $data);
}
you are doing wrong in you model file
<?php
class Model_website extends CI_Model {
function getWebsites() {
$query =$this->db->get('website');
if($query->num_rows() > 0) {
return $query->result();
}
}
}
1) In your model you are using where condition but $this->db->where('website_id'); but you not passing anything in your condition you have to do like this $this->db->where('website_id',$yourwebsite_id);
You have to check db query in model it's return result or not.
use this query in model
$this->db->last_query();
Use this query in model
$query = $this->db->get('website');
return $query;
Pass this query to view then use this foreach
foreach ($query->result() as $row)
{
<?php echo $row->website_id; ?>
<br>
<?php echo $row->name;?>
<br>
<?php echo $row->url;?>
}