CodeIgniter: Application Doesn't work and wont log errors - php

My small project with CodeIgniter (wich is only to get familiar with it. It's actually like their tutorial's example) it stoped working, giving me no output or errors even that in the index.php is set to developement...
So I tried to debug it myself with some echo's
And I found that here stops logging
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
echo 'This is echoed';
$this->load->model('news_model');
echo 'This wont be echoed';
}
/*(class continues)*/
}
And my news_model.php looks like this:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news'{});
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
}
Any idea what I'm doing wrong?
-EDIT-
public function __construct()
{
echo '__construct()';
$this->load->database();
echo 'after__construct()';
}
None of them are echoed...

find 1
$this->db->get('news'{}); // try to del {}
first parameters is table name
The second and third parameters enable you to set a limit and offset clause

Original codeignitor tutorial has this:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news'); // <----------------------------
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}

Related

URI routing in CodeIgniter causes 404 error

Second EDIT - Found the Problem, and answered it as well.
First Edit - added my Post_model.php file as well for clearer explanation of my code.
I am trying to redirect a Blogs slug to a separate page where I can show the entire blogs content.
here's an example slug
http://localhost/aag/posts/test-one
Here's the Posts controller
<?php
class Posts extends CI_Controller {
public function index(){
// Shows a blog listing
}
public function view($slug = NULL){
$data['post'] = $this->post_model->get_posts($slug);
if(empty($data['post'])){
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('templates/header');
$this->load->view('posts/view', $data);
$this->load->view('templates/footer');
}
}
The posts/view.php file
<h2><?php echo $post['title']; ?></h2>
<small class="post-date">Created on <?php echo $post['created_at']?></small><br>
<div class="post-body">
<?php echo $post['body']; ?>
</div>
the Post_model.php
class Post_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = FALSE)
{
if ($slug === FALSE) {
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => '$slug'));
return $query->row_array();
}
}
routes.php
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['(:any)'] = 'pages/view/$1';
Okay, so years of writing mysqli queries made me do this mistake. In my Post_model.php I am getting the data where (DB column field) slug should match the $slug that gets passed in, but I surrounded the slugs inside single quotes that was causing the error. Here's the working code now.
class Post_model extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = NULL)
{
if ($slug === NULL) {
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts', array('slug' => $slug));
return $query->row_array();
}
}
TLDR: Don't encapsulate your passed in arguments inside quotes.

Code igniter - Echo a variable from my array on my view

Im a designer that i´m taking my first steps on the programing área, mostly on PHP / MySQL. Past week i need to add a blog section on a page made with code igniter.
I make the secction that show all post in order (title and published date). I made a view that takes the id to show the content. But i can`t echo any of the variables.
When i use var_dump or print and it takes all the variables without any problem.
Did i miss somehting? :(
Thank you.
Model:
class blog extends CI_Model {
public function get($id){
$this->db->select('id, title, description, icon, uri, published_at');
$this->db->from('blogs');
$this->db->where('id', $id);
$query = $this->db->get();
$data = $query->result();
return $query->result();
if(!empty($data)){
return $this->make_groups($data);
}
}
}
Controller:
Class Pages extends CI_Controller {
public function blog_detail($id) {
$this->load->model('blog');
if($data = $this->blog->get($id)) {
$this->load->model('blog');
$this->data['blog_menu'] = TRUE;
$this->data['blog'] = $data;
$this->data['og'] = array(
'description' => 'INFO',
'image' => site_url('assets/images/og/blog.png')
);
# layout config
$this->layout->add_css(site_url('assets/css/blog.css'));
$this->layout->title_for_layout = "TITLE";
$this->layout->meta = array(
array('name' => 'description', 'content' => 'DETAIL'),
array('name' => 'keywords', 'content' => 'KEYWORDS'),
);
$this->layout->render('pages/blog_detail/index', $this->data);
}
}
}
View:
<?php var_dump($this->data);
echo $blog->published_at; ?>
On your model you are not returning the result
Check that the filenames and class names only have the first letter upper case.
Blog_model.php
class Blog_model extends CI_Model {
public function get($id) {
$this->db->select('id, title, description, icon, uri, published_at');
$this->db->from('blogs');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->row_array();
}
}
Controller
controllers > Pages.php
class Pages extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('blog_model');
}
public function blog_detail($id) {
$blog_data = $this->blog_model->get($id);
if($blog_data)) {
$this->data['blog_menu'] = TRUE;
$this->data['published_at'] = $blog_data['published_at'];
var_dump($blog_data);
$this->data['og'] = array(
'description' => 'INFO',
'image' => site_url('assets/images/og/blog.png')
);
# layout config
$this->layout->add_css(site_url('assets/css/blog.css'));
$this->layout->title_for_layout = "TITLE";
$this->layout->meta = array(
array('name' => 'description', 'content' => 'DETAIL'),
array('name' => 'keywords', 'content' => 'KEYWORDS'),
);
$this->layout->render('pages/blog_detail/index', $this->data);
}
}
}
View
<?php echo $published_at;?>

Can't update my database record in codeigniter, no error is appearing

I'm quite new to php and there's no error appearing but appereantly, can't update my data in the database.
The controller
public function update_user_view() {
$this->load->helper('form');
$user_id = $this->uri->segment('3');
$query = $this->db->get_where("users",array("user_id"=>$user_id));
$data['records'] = $query->result();
$data['old_user_id'] = $user_id;
$this->load->view('user_edit',$data);
}
public function update_user(){
$this->load->model('user_model');
$data = array(
'user_id' => $this->input->post('user_id'),
'name' => $this->input->post('name'),
'nickname' => $this->input->post('nickname'),
'email' => $this->input->post('email'),
'hadd' => $this->input->post('hadd'),
'gender' => $this->input->post('gender'),
'cpnum' => $this->input->post('cpnum'),
'comment' => $this->input->post('comment')
);
$old_user_id = $this->input->post('old_user_id');
$this->user_model->update($data,$old_user_id);
$query = $this->db->get("users");
$data['records'] = $query->result();
$this->load->view('user_view',$data);
}
the model
<?php
class User_model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert($data) {
if ($this->db->insert("users", $data)) {
return true;
}
}
public function delete($user_id) {
if ($this->db->delete("users", "user_id = ".$user_id)) {
return true;
}
}
public function update($data,$old_user_id) {
$this->db->set($data);
$this->db->where("user_id", $old_user_id);
$this->db->update("users", $data);
}
}
?>
Just do
$this->db->where("user_id", $old_user_id);
$this->db->update("users",$data);
Remove $this->db->set($data); from update method of model. This is not required as update query data section is used to set table records.

How to use insert_batch to insert data in db

how to use insert_batch to add multiple data in database by using for loop please help me as i am very new to codeigniter.
My controller
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
$data = array(
array(
'studentname' => 'Reddy' ,
'gender' => 'Male' ,
'phone' => '456879'
),
array(
'studentname' => 'Yalla' ,
'gender' => 'Female' ,
'phone' => '12345678'
)
);
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$id=$this->input->post('id');
$data=array(
'studentname' => $this->input->post('studentname'),
'gender' => $this->input->post('gender'),
'phone' => $this->input->post('phone')
);
$this->db->where('id',$id);
$this->db->update('tblstudent',$data);
redirect("Student/index");
}
function delete($id)
{
$id=$this->db->where('id',$id);
$this->db->delete('tblstudent');
redirect("Student/index");
}
}
Model
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
}
First of all you don't have to add direct access to database in controller. For all database access code you have to create model.
in your above code you added database access code in direct controller,which is logically wrong as per MVC architecture.
Below is code as per MVC and all database access code resides in Model.
Your main question for batch insert is in function insert()
of model.Please go through it.
<?php
class Student extends CI_Controller {
public function _construct()
{
parent::_construct();
//call model
$this->load->model("StudentModel","m");
}
function index()
{
$this->load->view("index");
}
function savedata()
{
$stdarray = array();
$stdarray[] = array('studentname' => 'Reddy' ,'gender' => 'Male' ,'phone' => '456879');
$stdarray[] = array('studentname' => 'Yalla' ,'gender' => 'Female' ,'phone' => '12345678');
//mean that insert into database table name tblstudent
$this->db->insert_batch('tblstudent',$data);
$this->m->insert($stdarray);
//mean that when insert already it will go to page index
redirect("Student/index");
}
function edit($id)
{
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view('edit',$data);
}
function update($id)
{
$updateArray = array();
$updateArray['studentname'] = $this->input->post('studentname');
$updateArray['gender'] = $this->input->post('gender');
$updateArray['phone'] = $this->input->post('phone');
$whereArray = array();
$whereArray['id'] = $id;
$this->m->updateRow($updateArray,$whereArray);
redirect("Student/index");
}
function delete($id)
{
$whereArray = array();
$whereArray['id'] = $id;
$this->m->deleteRow($whereArray);
redirect("Student/index");
}
}
?>
Model:
class StudentModel extends CI_Model{
function _construct()
{
parent::_construct();
}
function gettable()
{
$query=$this->db->get('tblstudent');
return $query->result();
}
function getonerow($id)
{
$this->db->where('id',$id);
$query = $this->db->get('tblstudent');
return $query->row();
}
function insert($insrtArray)
{
$success = $this->db->insert_batch('tblstudent', $insrtArray);
}
function updateRow($updateArray,$whereArray)
{
if(!empty($whereArray))
{
foreach ($whereArray as $key => $value) {
$this->db->where($key,$value);
}
}
$this->db->update('tblstudent',$updateArray);
}
function deleteRow($whereArray)
{
if(!empty($whereArray))
{
foreach ($whereArray as $key => $value) {
$this->db->where($key,$value);
}
}
$this->db->delete('tblstudent');
}
}
?>
Important: morever you have to use escape function for all inputa which are direct send to database for maintain security from SQL injection.

How to use global variable in PHP Codeigniter

I have implemented the login logic in an MVC application; I want to see if the user has filled the username and passowrd incorrectly and if so I want to show a notifictaion in the view; so I'm passing this information via $data['er']; but for some reason it is not catching this data:
Please let me know if my question is clear or not; and if any clarification is needed, please let me know which part is ambiguous
My code:
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$GLOBALS['er'] = False;
}
public function index() {
$data['er']=$GLOBALS['er'];
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
public function validate_credentials() {
$this->load->model('user_model');
$query = $this->user_model->validate();
if ($query) {
$data = array(
'username' => $this->input->post('username'),
);
$this->session->set_userdata($data);
redirect('project/members_area');
} else {
$GLOBALS['er'] = TRUE;
$this->index();
}
}
}
Don't use GLOBALS you can just use a private variable in your class.
Create the variable above your __construct function like private $er
In your __contruct function set the default value
Set and get in your public function using $this->er
Implemented in your code:
class Login extends CI_Controller {
private $er;
public function __construct() {
parent::__construct();
$this->er = FALSE;
}
public function index() {
$data['er']= $this->er;
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
public function validate_credentials() {
$this->load->model('user_model');
$query = $this->user_model->validate();
if ($query) {
$data = array(
'username' => $this->input->post('username'),
);
$this->session->set_userdata($data);
redirect('pmpBulletin/members_area');
//die(here);
} else {
$this->er = TRUE;
$this->index();
}
}
}

Categories