`CI` return wrong value - codeigniter

I have no idea why my CI return wrong value from phpMyAdmin
Model :
<?php
class Post extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getallpost()
{
return $this->db->get('post');
}
}
?>
And
Controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Postc extends CI_Controller {
function index()
{
$this->load->model('post');
$posts=$this->post->getallpost();
echo '<pre>';
print_r($posts);
}
}
?>
enter image description here

You code is not giving you any error, it is doing exactly what you are asking it to do. Perhaps you need to be specific about the information you want to get. You see in your model
function getallpost()
{
return $this->db->get('post');
}
The above function will return you an object. If you want to get an array you need to write
return $this->db->get('post')->result_array();
And make sure you have some data in your post table to print For learning more about query database you should read Codeigniter's Query Builder Class

Change your Model Code
<?php
class Post extends CI_Model
{
function __construct()
{
parent::__construct();
}
function getallpost()
{
$query = $this->db->get('post');
if($query->num_rows() > 0)
{
return $query->result();
}else{
return false;
}
}
}

If you want to get return your result as as array then use
return $this->db->get('post')->result_array();
And if you want to get return your result as object then use
return $this->db->get('post')->result();

Related

How to get all data from Category in Codeigniter

I am new in Codeigniter, Please help me.
My Codeigniter url is - http://localhost/framework/CodeIgniter/index.php/restaurant/patna/
I want to get all data.
I set the route -
$route['restaurant(:any)'] = "Restaurant/getRestaurantByCity/$1";
And my function in Controller is
here i want the category id where pass the cat_id in below function for get all the data of that category
public function getRestaurantByCity()
{
$rst_list = $this->Restaurant_model->get_restaurant_by_city();
}
try this
controller
$this->data['RestaurantByCity'] = $this->Restaurant_model->getRestaurantByCity();
echo "<pre>"; print_R($this->data['RestaurantByCity']);
model
public function getRestaurantByCity($ids){
$sql ="select * from categoriestableName where categories_id in($ids) order by categories_id asc";
$rs = $this->db->query($sql);
foreach($rs->result() as $record ){
$result[] = $record;
}
return $result;
}
This should work
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Restaurant extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function getRestaurantByCity($id)
{
$this->load->model('Restaurant_model');
$rst_list = $this->Restaurant_model->get_restaurant_by_city($id);
}
}

creating a model and controller to get category list from db using codeigniter

1.models/calists.php // My model file
Model file here i get the category list from database to the controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlists extends CI_Model
{
public function __construct()
{
$this->load->database(); //load database
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
?>
2.controllers/catlist.php // controller file
Controller to get the data from model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Catlist extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('catlists');
}
public function catlist()
{
$data['catlist'] = $this->catlists->getCategories();
$this->load->view('home', $data);
}
}
In header am printng the categories list
print_r($data['catlist']);
I am not sure what error you are getting here, but when you are loading model in controller, first letter should be capital.
$this->load->model('Catlists');
$data['catlist'] = $this->Catlists->getCategories();
In header you have to call
print_r($catlist);
The file name must match the class name.
For example
if your class is this:
class User_model extends CI_Model {
function __construct()
{
parent::__construct();
}
}
Your file will be this:
application/models/User_model.php
Here in you case your model name is
models/Calists.php
So your model file be
Model Calists.php
class Calists extends CI_Model {
function __construct()
{
parent::__construct();
}
public function getCategories()
{
$query = $this->db->get_where('category',array('status'=>'Enable'));
if ($query->num_rows() > 0)
{
return $query->result();
}
else
{
return array();
}
}
}
And you call this model file in you controller like this
Controller
public function __construct()
{
parent::__construct();
$this->load->model('calists');
}
CodeIgniter anatomy of model.

Understanding codeigniter base controller structure

The following is a working example of how my Codeigniter website currently functions:
Model:
<?php
class Default_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_link()
{
$query = $this->db->query('SELECT * FROM links LIMIT 5');
return $query->result();
}
Controller:
<?php
class Home extends CI_Controller {
public function index()
{
$this->load->model('segment1/Page_model');
$data['link'] = $this->Page_model->get_link();
$this->load->view('page_view', $data);
}
}
View:
<h2>Link</h2>
<ul>
<?php if (isset($link)):?>
<?php foreach ($link as $row):?>
<li><?=$row->link?></li>
<?php endforeach;?>
<?php endif;?>
</ul>
I want to begin using a base controller for the above example, and while I've followed a few online examples - I can't quite get it right, and I'd appreciate some guidance...
I autoload the Model, no problem
The View file remains
I alter the config.php file
Controller:
<?php
class Home extends Main_Controller {
public function index()
{
$this->load->model('segment1/Page_model');
$this->load->view('page_view', $data);
}
}
MY_Controller
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Now, here's where I get stuck - I can't quite figure out exactly what goes in the Main_Controller, and how it's structured...
Main_Controller:
<?php
class Main_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
//
// WHAT GOES IN HERE?
// SERIOUSLY, HELP!
//
}
}
Clearly, there's one big line of data missing from the original controller...
$data['link'] = $this->Page_model->get_link();
How does it all tie up?
Not exactly sure if I understand your question correctly, but if you want to avoid repeating this line:
$data['link'] = $this->Page_model->get_link();
What you can do is to put that in the constructor and create a public variable where you can store it.
i.e.
Main_Controller:
<?php
class Main_Controller extends MY_Controller
{
public $link;
function __construct()
{
parent::__construct();
$this->load->model('segment1/Page_model');
$this->link = $this->Page_model->get_link();
}
}
Controller:
<?php
class Home extends Main_Controller {
public function index()
{
$this->load->view('page_view', array('link' => $this->link));
}
public function another_page()
{
// you can keep using the value assigned to link in other
// methods without having to call Page_model->get_link() everytime
$this->load->view('page_view', array('link' => $this->link));
}
}

How do I make a function available to several CodeIgniter methods?

CodeIgniter 2.1.2
I have a class that contains two methods, for the purpose of this questions showone and view. The latter returns all items of a small database and can also perform a search. The other one is for permalinks like domain.com/showone/firstname-lastname
<?php
class Pages extends CI_Controller {
public function view($page)
{
//this includes a mysql search
}
public function showone($slug)
{
//abbreviated version:
$query = "SELECT * FROM mytable WHERE slug = '" . $slug . "'";
$result = $this->db->query($query);
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
?>
So if a user decides to directly enter a URL that doesn't return anything from the database, I would like to direct him to search results instead of showing a 404.
So how do I set up a function searchdatabase($query) to be used by both showone and view?
You can use your controller functions inside your controller:
public function view($page)
{
$this->showone($slug);
}
Define that function in your model, load your model and then call the model->method.
<?php
//Your Model would look something like this.
class Search_Model extends CI_Model {
public function __construct(){
parent::__construct();
}
public function showone($slug)
{
//abbreviated version:
//Its best to use active record for building your queries
$this->db->where->('slug', $slug);
$result = $this->db->get('mytable');
if ($result->num_rows() == 0)
{
//here is where I'd like to use the same search that I used in showall
}
else
{
//show the one item
}
}
} //class
And then in your controller you would do this:
<?php
//Your Controllerwould look something like this.
class Index extends CI_Controller {
public function __construct(){
parent::__construct();
//model will be loaded for each method.
//if you're going to use this model across several controllers
//its best to autoload it, set that in autoload.php under /app/config
$this->load->model('search_model');
}
public function index(){
$searchResults = $this->search_model->showone('slugone');
}
Update
I just realized you are wanting to show all results if no results were returned. In which case you would perform that logic in your model as well..
In your conditional statement you would do the following:
if ($result->num_rows() == 0)
{
return $this->showall();
}
else
{
return $this->view($slug);
}
}
your showall() and view() methods would return $result->result();

Passing query result to 2nd function in Codeigniter

I'm trying to run two queries in two functions in a model on Codeigniter. The 2nd query runs based on the result of the first by passing the variable between the functions.
The first query gets the row from the 'photos' database based on the URL the user goes to. The second query is run against the 'users' database and the row is selected based on the 'userid' field from the row of the first query.
I need to be able to get all data from the rows of both queries. I get a 404 error at the moment, any ideas as how to get it working?
Code is as follows:
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Viewphoto_model extends CI_Model {
public function get_photo($id)
{
$db_photos = $this->load->database('photos', TRUE);
$db_photos->select('*');
$db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
$db_photos->from('photos');
$db_photos->where('approved', '1');
$db_photos->where('id', $id);
$result = $db_photos->get()->row();
$userid = $this->get_user($result->userid);
}
public function get_user($userid)
{
$db_users = $this->load->database('users', TRUE);
$db_users->select('firstname, lastname, email');
$db_users->from('useraccounts');
$db_users->where('id', $userid);
$query = $db_users->get();
return $query->row();
}
}
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Viewphoto extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('viewphoto_model');
}
public function view($id)
{
$data['photo'] = $this->viewphoto_model->get_photo($id);
if (empty($data['photo']))
{
show_404();
}
$data['user'] = $this->viewphoto_model->get_user($userid);
if (empty($data['user']))
{
show_404();
}
$data['title'] = $data['photo']->title.' by '.$data['photo']->username;
$data['meta_description'] = $data['photo']->description;
$data['directory'] = 'sub';
$this->load->view('templates/header', $data);
$this->load->view('viewphoto/viewphoto', $data);
$this->load->view('templates/footer', $data);
}
}
You seem to have a slight logic issue. Your get_photo() function should return the photo object, which your controller can then use in the call to get_user()...
public function get_photo($id)
{
$db_photos = $this->load->database('photos', TRUE);
$db_photos->select('*');
$db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
$db_photos->from('photos');
$db_photos->where('approved', '1');
$db_photos->where('id', $id);
return $db_photos->get()->row();
}
Then in your controller....
public function view($id)
{
$data['photo'] = $this->viewphoto_model->get_photo($id);
if (empty($data['photo']))
{
show_404();
}
// Pass the photo's userid here
$data['user'] = $this->viewphoto_model->get_user($data['photo']->userid);
if (empty($data['user']))
{
show_404();
}
// ...
}

Resources