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);
}
}
}
}
Related
so I believe I have it right (im new to active records).
the problem I am having I keep getting error
Message: Call to a member function getbank() on a non-object
My code is simple
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
model
class Profile_model extends CI_Model {
function getbank()
{
$query = $this->db->get('bank');
return $query->results();
}
Its so simple, I cant see a error, thank you.
Try this
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('profile_model'); # Added
$query = $this->profile_model->getbank(); # Changed
if(!empty($query)) # Changed
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
Model
class Profile_model extends CI_Model {
public function getbank() # Changed
{
$query = $this->db->get('bank');
return $query->result(); # Changed
}
}
Update
Simple answer, thanks to all those who tried helping. I had the model file name with a small letter not a capital.
Eg profile_model.php
should be Profile_model.php
However, this solution is elegant and simple, so it improved my code so il accept this one.
It may be because you didn't load your model?
Try changing your controller to:
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('Profile_model');
if($query = $this->Profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $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 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;
}
}
}
I have the following.
controllers/customers.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class customers extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function view($id) {
$this->load->model('customers');
$news = $this->customers->view_customer($id);
$data['title'] = $news['title'];
$data['body'] = $news['body'];
$this->load->view('customers_customer_view', $data);
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('customers_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('dashboard', 'refresh');
}
}
?>
models/customer.php
<?php
class customers_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function view_customer($id) {
if($id != FALSE) {
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
else {
return FALSE;
}
}
}
?>
views/customers_customer_view.php
<?php print $title; ?>
<?php print $body; ?>
I am very new to code igniter, I have followed this tutorial from the web, No matter what i do i cannot get the database info to display when loading root/customers/view/1
All i get is a blank page. Even if i change the view to include a some static text it wont display, From this i believe it to be something wrong with loading the view, But all looks ok to me.
Please can somebody assist.
You wrote:
$this->load->model('customers');
But model file is named: customer.php.
And class name is: customers_model.
Please check it again.
I will give you an example:
$this->load->model('customers');
Your model file have to be: customers.php.
And your class name have to be: class Customers {}
here i done code for contact us page with two fileds like email,body it stores values in db
but it s not working i post the code here
Model
//model/pages.php
<?php
class pages extends AppModel{
var $useTable = 'contact';
}
?>
Controller
class PagesController extends AppController {
public $uses = array();
public function display() {
$path = func_get_args();
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count - 1]);
}
$this->set(compact('page', 'subpage', 'title_for_layout'));
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
public function index(){
$this->set('post', $this->contact->find('all'));
}
public function create() {
if ($this->request->is('post'))
//this function for methods like get, post, set,delelte
{
// print_r('post');
$this->contact->create();
if ($this->contact->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
}
view
here code for display
view/contact.ctp
<?php
echo 'welcome to Contact us';
echo $this->Form->create('pages');
echo $this->Form->input('email');
echo $this->Form->input('body', array('rows' => '6'));
echo $this->Form->end('Save pages');
?>
added in db table and values contact
Did you read something to start in CakePHP like Getting Started or CakePHP Conventions?
Reading some documentation, you will find that your tables should be named in plural and models in singular. Your model in app/Model/Contact.php looks like this:
class Contact extends AppModel {
public $useTable = 'contacts'; // It is not necessary, because CakePHP implicitly understands that your table will be in the plural
}
Now, your view located in app/View/Contacts/index.ctp:
echo $this->Form->create('Contact');
echo $this->Form->input('email');
echo $this->Form->input('body', array('rows' => '6'));
echo $this->Form->end('Save');
And your controller in app/Controller/ContactsController.php:
class ContactsController extends AppController {
public function index() {
if ($this->request->is('post')) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
$this->Session->setFlash(__('Your contact has been saved.'));
} else {
$this->Session->setFlash(__('Unable to add your contact.'));
}
}
}
}
This is a basic usage. I recommend you read the documentation to start with CakePHP because it has lots of interesting things that you may need.