specific data is not coming in codeigniter - php

i am doing some practice on the codeigniter to retrieve the data from database and i am success to do this.but the problem arise when i want to fetch data of a specific field.
to retrieve the specific value i am using the following URL on my local host:
localhost/codeigniter/index.php/news/view/city-news
where news is controller,view is method of controller and city-news is argument.
Here is my code of controller:
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
this method calls the method get_news($slug) of model news_model.here is the code of this method:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
echo $slug;//here is i m also getting the slug value.
$query = $this->db->get_where('news', array('slug' => $slug));//i think this is not working properly
print_r($query->row_array());die;//now i am getting values here.
return $query->row_array();
}
but still my view shows "404 page not found". my view code is:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
?>
Now please tell me where i am going wrong.

your error is you are setting $data['news'] and testing $data['news_item'] :
$data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.
if (empty($data['news_item'])) // <=== HERE IS THE ERROR
{
show_404();
}

have you tried with simple where query as follows
$data = $this->db->where('slug', $slug)->from('news');
you must get something in $data. Means at least mysql object. Its been another part that whether rows are available in database or not.
secondly i am worried about the true and false. Means as per i know Boolean store as INT(1) means either 0 or 1. You needs to check with that as well.
you can debug your query with
$this->db->last_query();
Hope this helps.

Here is the cleaned code of the controller:
public function view($slug)
{
$news = $this->news_model->get_news($slug);
if (empty($news))
{
show_404();
}
$data['title'] =$news['title'];
$data['text'] = $news['text'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
this method calls the method get_news($slug) of model news_model:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
echo $slug;
$query = $this->db->get_where('news', array('slug' => $slug));
print_r($query->row_array());die;//now i am getting values here.
return $query->row_array();
}
<?php echo '<h2>'.$title.'</h2>'; echo
$text; ?>

A small error:
Here
if (empty($data['news_item'])) // <=== put "news" here
{
show_404();
}
and at your view try to get data as
echo $news;

die() is a function use exit instead.
set this at the beginning of script
ini_set('display_errors', 1);
error_reporting(E_ALL);
also use var_dump($query); to check if it return any result if not exception or fatal error was raised when calling query
if array is empty it means that no news with this slug is in db. You should set some conditions when particular news is not found
also what is the value of this slug, does it contain any special chars etc.?

Related

passing variables in form_open() in codeigniter

Good day all. I'm new to codeigniter and I'm trying to pass a variable from one view page to another. But I get error and after a lot of tries still couldn't figure it out.
This is the Model code:
function get_post($postID){
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->result_array();
}
function update_post($postID, $data)
{
$this->where('post_id', $postID);
$this->db->update('khanposts', $data);
}
This is the Controller code:
function editpost($postID)
{
$data['success']=0;
if($_POST){
$data_post=array(
'fullname'=>$_POST['fullname'],
'dob'=>$_POST['dob'],
'blood'=>$_POST['blood'],
'village'=>$_POST['village'],
'occupation'=>$_POST['occupation'],
'company'=>$_POST['company'],
'email'=>$_POST['email'],
'contact'=>$_POST['contact'],
'password'=>$_POST['pass'],
'marry'=>$_POST['marry']);
$this->khanpost->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->khanpost->get_post($postID);
$this->load->view('edit_post', $data);
}
This is the code of View page which passes the value to edit_post view page:
foreach ($posts as $row){
<tr><td><i>Edit</i></td></tr>';
}
This is code of edit_post view page where it must get the value of $row['post_id']:
echo form_open(base_url().'khanposts/editpost/'.$row['post_id']);
echo '<b>Full Name: </b>';
$data_form=array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value'=>$row['fullname'] );
echo form_input($data_form);
How do I assign the passsed variable($row['post_id']) in form_open()? Any solution will be really helpful. Tnx.
As far as I understand the problem;
Pass PostID to view in editpost function:
function editpost($postID)
{
...
$data['postID'] = $postID;
}
Get it in view page like:
echo form_open(base_url('khanposts/editpost/'.$postID));
You should load form helper:
$this->load->helper('form');
And your data_form input:
$data_form = array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value' => $post['fullname']);
You should use row_array instead of result_array, because of you get single post.
function get_post($postID)
{
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->row_array();
}

Redirecting not working in codeigniter

So here's what I want to do. I want to check if the userid in segment(3) exist or else it will redirect somewhere instead of still loading the view with an error.
Here's the example url
http://localhost/ems/edit_user/edit_user_main/1001
Now if I try to edit the userid in segment(3) and intentionally put an invalid userid, it still loads the view and i don't know why
Here's my function
public function edit_user_main(){
$id = $this->uri->segment(3);
$check = $this->get_data->check_if_exist($id);
if($check) {
$data['title'] = 'Edit User';
$data['id'] = $this->session->userdata('usertoedit');
$this->load->model('accounts/get_data');
$item = $this->get_data->get_user($id);
$data['user'] = $item[0];
$data['main_content'] = 'edit_user/edit_user_main';
$this->load->view('includes/template', $data);
} else {
redirect('admin/adminuser');
}
}
Here's the model
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query) {
return TRUE;
} else {
return FALSE;
}
}
There is no problem with the fetching of data.
The problem is even if the userid doesn't exist, the view is still loading but with an error coz there's no data for that userID. It's not redirecting,
I tried using print_r and it working fine, the value of the $check is 1 when there's a valid userID.
Hope someone can help me with this. Thank you
With your function it will always return true because the statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always execute,So you need to check query is returning any result row or not with the statement
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
Try this..
With the function it will always return true because the following statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always be execute, So need to check query is returning any result row or not
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
And load heper as:-
$this->load->helper('url');
before the redirection

Error page not found manipulation URL with slug in CodeIgniter

I am getting error page not found when I implementation URL using slug.
This is code in controller
function view($slug)
{
$this->data['halaman_item'] = $this->mhalaman->get_profil($slug);
if (empty($this->data['halaman_item'])) {
show_404();
}
$this->data['title'] = $this->data['halaman_item']['judul']; $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('mahasiswa/profil/sejarah', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_content',$this->data);
}
Model
function get_profil($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get($this->tbl_halaman);
return $query->result_array();
}
$query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
return $query->row_array();
}
Route
$route['profil/view'] = "profil/view";
$route['profil/(:any)'] = "profil/view/$1";
$route['profil'] = "profil";
When I run this link tkd/index.php/mahasiswa/profil/sejarah is 404 page is not found. may be you know where is the probelm.
Help me what to do. Thank you.
Well, without knowing how much of the rest of your app works...the first obvious thing is making sure $this->data['halaman_item'] actually has data in it...because if not, it's calling show_404 before anything happens.

Codeigniter display info from 2 database tables

I am new to codeigniter and could be better at php. I have a news item being retrieved from the database table news, it has a foreign key of partner_id that is tied to the partners table. I want to get that value and use it to get the associated partner and display it's info. This is the latest of my attempts. I think I might be making it too hard. All relevant files are below. Thanks in advance.
In the model if the $id_partner is being assigned in the get_news function I don't think it is passing to the get_partner function.
news_model.php
public function get_news($slug_news = FALSE)
{
$this->load->helper('array');
if ($slug_news === FALSE)
{
$news_query = $this->db->get('news');
return $news_query->result_array();
}
$news_query = $this->db->get_where('news', array('slug' => $slug_news));
return $news_query->row_array();
global $id_partner;
$id_partner = element('partner_id', $news_query);
}
public function get_partner($id_partner = FALSE){
$partners_query = $this->db->get('partners');
$partners_query = $this->db->get_where('partners', array('id' => $id_partner));
return $partners_query->row_array();
}
The $slug_news in the view function doesn't apply to the partner part (obviously). but can I have another function?
controller, news.php
public function view($slug_news)
{
$data['news_item'] = $this->news_model->get_news($slug_news);
$partner_data['partner_listing'] = $this->news_model->get_partner($id_partner);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('news/news_partner', $partner_data);
$this->load->view('templates/sidebar');
$this->load->view('templates/footer');
}
view1, news/view.php
<?php
echo '<img src="/images/'.$news_item['thumb'].'" />';
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
echo $news_item['bus_name'];
?><br>
and view2, news/news_partner.php
<?php
echo '<h2>'.$partner_listing['bus_name'].'</h2>';
echo $partner_listing['address1'];
echo $partner_listing['address2'];
echo $partner_listing['city'];
echo $partner_listing['state'];
echo $partner_listing['phone'];
echo $partner_listing['email'];
?>
Your model is a little all over the place. To start, the two lines after the return are never called in get_news. I would just simplify the model and do a simple join:
public function get_news()
{
$this->db->select('*');
$this->db->from('news');
$q = $this->db->join('partners', 'news.partner_id = partners.id');
return $q->result();
}
Here is what I put with your code:
public function get_news($slug_news = FALSE)
{
if ($slug_news === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$this->db->select('*');
$this->db->from('news');
$query_news = $this->db->join('partners', 'news.partner_id = partners.id');
return $query_news ->result();
}
I only used the "view.php" view as well. Does $slug_news need to be in there somewhere? How does it know what line to get? Thanks for your help, I am having so much trouble.

CodeIgniter $data not passed from Controller to View

my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)

Categories