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.
Related
I have this following code in the Address controller
public function index()
{
if($this->session->userdata('isLogin')) {
$this->load->driver('cache');
$this->load->model('MemberModel');
if(!$this->cache->get('province') == false) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces(); // it will return json object
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
$userdata = $this->MemberModel->getProfile($this->session->userdata('userid'));
$data['user'] = $userdata;
$this->display_member_area('member/address', $data);
}
else {
redirect(base_url());
}
}
When I want to get the data using:
var_dump($this->cache->get('province'));
the result I get always shows
bool(false)
but when I tried to do this instead
var_dump($data['provinces']) // it's show me json object, that I want
Can anyone please show me where I'am doing wrong?
Thanks in advance
Remove false from the if condition and use this code.
if(!$this->cache->get('province')) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces();
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
Make sure you have the correct credential on your config at:
https://github.com/bcit-ci/CodeIgniter/blob/develop/application/config/memcached.php
it will be at you application/config/memcached.php file. Hostname is the ip you setup your memcached instance. You could provide multiple server if you wish.
reference on installing memcached
I am trying to make a CRUD Module in Codeigniter HMVC but I seem to be missing something in the process. Here is what I am facing.
I have a News Module which has a Manage function
function manage(){
$grid = Modules::run('Crud/renderGrid', 'News' , 'News management');
}
Render Grid Function
function renderGrid($module , $page_title){
$data['page_title'] = $page_title; //Dynamic
$data['module'] = $module;
$data['view_module'] = 'Crud';
$data['displayfields'] = Modules::run($module.'/get_displayfields');
$data['key'] = Modules::run($module.'/get_key');
$data['rows'] = Modules::run($module.'/get' , $data['key']);
$data['view_file'] = 'manage';
$this->load->module('dashboard');
$this->dashboard->show_dashboard($data);
}
Here, the show_dashboard function just loads up a template layout with a desired view in it.
function show_dashboard($data = NULL){
if($data == NULL){
$data['view_file'] = "manage";
$data['page_title'] = 'Sigma Web Solutions';
}
$this->load->module('templates');
$this->templates->admin($data);
}
Templates->admin
function admin($data){
$this->load->view('admin' , $data);
}
The View (omitting the header n Footer)
<?php
if (!isset($view_file)) {
$view_file = "";
}
if (!isset($view_module)) {
$module = $this->uri->segment(1);
}
if (($view_module!="") && ($view_file!="")) {
$path = $view_module."/".$view_file;
$this->load->view($path);
}
?>
Now, when I try the url news/manage, it gives me a blank page with no source code in it. But when I try something like
crud/renderGrid/news/sometitle/ it works just fine.
Kindly point out what did I miss here. Thanks.
Working Solution:
Thanks to wolf I added a route
$route['managenews']= 'crud/renderGrid/news/News';
And it works like charm. But why do I need a route here? Shouldn't it just work. And this means for every module I need to have 4 entries in my route file for the CRUD system to work. can anyone suggest a better method?
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
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.?
I'm using Smarty_parser.php and it works well when when I use the parser by itself or if I run the parser then a view call. For example:
public function act() {
#$this->load->library('smarty_parser');
$data = array('Someinfo');
$this->smarty_parser->parse('contentTmpls/act.tpl', $data);
// Load Footer
$this->load->view('Templates/footer');
}
but not if I do:
public function act() {
#$this->load->library('smarty_parser');
$this->load->view('Templates/header');
$data = array('Someinfo');
$this->smarty_parser->parse('contentTmpls/act.tpl', $data);
// Load Footer
$this->load->view('Templates/footer');
}
The header view call seems to disappear and does not output anything. I was wondering if anybody has run into this problem or have seen a fix.
I'm not sure if this is kosher but here's the answer.
I need to switch out a piece of code:
From
if ($return == FALSE)
{
$CI->output->final_output = $template;
}
To this
if ($return == FALSE)
{
$CI->output->append_output($template);
}
Seems to be working fine. Now I just need to see if anybody is maintaining this code to add the patch.