I try to show (*localhost/ujian/index.php/user_controller*)
but not found, why?
Code
class User_controller extends CI_Controller{
function __construct()
{
parent ::__construct();
$this->load->model('user_model');
$this->load->helper(array('form'));
}
function user()
{
$data['judul'] = 'Daftar User';
$this->load->view('user', $data);
}
function simpan_user()
{
$this->user_model->simpan_user();
$data['notifikasi'] = 'Data berhasil disimpan';
$data['judul']='Insert Data Berhasil';
$this->load->view('notifikasi', $data);
redirect('user_controller');
}
function view()
{
$data['daftar_user'] = $this->user_model->get_user_all();
$this->load->view('daftar_user', $data);
}
function delete_user($id)
{
$data['daftar_user'] = $this->user_model->get_user_all();
$this->load->view('daftar_user', $data);
$username = $this->user_model->delete_user($id);
redirect('user_controller');
}
I try to show (*localhost/ujian/index.php/user_controller*)
but not found, why?
As per your question, you are calling the URL:
localhost/ujian/index.php/user_controller
But your controler doesn't have default function index(). So add default function and do default action for this url.
function index()
{
// add your action
// or call any default function like $this->user();
}
When You don't Specify any function name after controller name, It call the index() function by default.
In your Requested URL there is no function name specified, So, It by default call the index() function . But There is no Index() function defined in your requested controller .
Please , Create an index() function and try.
Cheers..!!
Related
I am creating eCommerce in Codeigniter. Here is the controller file for registration:
class Register extends CI_Controller {
public function index()
{
$this->load->view('common/header');
$this->load->view('register');
$this->load->view('common/footer');
}
public function home(){
$this->load->view('common/header');
$this->load->view('common/slider');
$this->load->view('home');
$this->load->view('common/footer');
}
public function signup(){
$username = $this->input->post("username");
$password = $this->input->post("password");
$email = $this->input->post("email");
$this->form_validation->set_rules('username','User Name','required|is_unique[user.name]|min_length[4]');
$this->form_validation->set_rules('password','Password','required|min_length[5]');
$this->form_validation->set_rules('email','Email address','required|valid_email');
$this->form_validation->set_error_delimiters('<div class="text-danger">', '</div>');
if($this->form_validation->run() == true){
$this->load->model('register_model');
if($this->register_model->register_user($username,$password,$email)){
$this->home();
}else{
$this->index();
}
}else{
$this->index();
}
}
}
On form submit the page is redirecting to the home page but the URL on the browser is (http://localhost:8080/codignitor_projects/shophere/register/signup) But I want to change the URL to this (http://localhost:8080/codignitor_projects/shophere/)
I got your problem. I don't know your CI version. I hope you are using the latest one and I am also giving my answer according to the latest version CI-4.
Please Write
return redirect()->to('index');
or
return $this->response->redirect(base_url('YourControllerName/index'));
instead of
$this->index();
To clearify this problem I would like to give you an example. Let, I have a Test controller and there are two functions such as abc() and def()
class Test extends Controller
{
function abc()
{
echo "Hi ! I am ABC";
}
function def()
{
$this->abc();//URL will be: http://localhost/your_project_name/public/test/def
//return redirect()->to('abc'); //URL will be: http://localhost/your_project_name/public/test/abc
//return $this->response->redirect(base_url('Test/abc')); //URL will be:http://localhost/your_project_name/public/Test/abc
}
}
There are three lines inside the def() function that give you the same result but the URL from first one to others will be different which is commented.
I want to pass data from Controller to model.But I am unable to fetch it at the model side in CI.Kindly help me out.
Here is my controller function:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
$data['h'] = $this->Chronicles_model->show_seminar();
//return the data in view
$this->load->view('chronicles', $chronicle_num);
}
And here is my model:
public function show_seminar($chronicle_num) {
echo $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
Its because your not passing any value to your model.
CONTROLLER
function show_chronicles($chronicle_num)
{
$this->load->database();
$this->load->model('Chronicles_model');
$data['h']=$this->Chronicles_model->show_seminar($chronicle_num);
$this->load->view('chronicles', $chronicle_num);
}
and you need to return the result() of your query
MODEL
public function show_seminar($chronicle_num = NULL)
{
return $this->db
->get_where('chronicles', array('chronicles_no' => $chronicle_num))
->result();
}
Controller:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
// pass it to model
$data['chronicle_num'] = $this->Chronicles_model->show_seminar($chronicle_num);
//return the data in view
$this->load->view('chronicles', $data);
}
And here is my model:
public function show_seminar($chronicle_num) {
return $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
You just forgot to pass $chronicle_num as a parameter when you called $this->Chronicles_model->show_seminar();.
So the right way is $this->Chronicles_model->show_seminar($chronicle_num);
You can add as many parameters as you wish to the functions. Example:
public function show_seminar($chronicle_num, $param2, $param3) {
//Login here
}
Just remember to pass the parameters every time you call the function.
Can anybody tell me what's wrong with my code? It said that the problem is in the controller, on line 13. Please help
Controller
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('model_databis'); //line13
}
function index(){
$data=array('data' => $this->model_databis->bus());
$this->load->view('admin/databis',$data);
}
Model
class Model_databis extends CI_Model{
function __construct(){
parent::__construct();
}
function dbis(){
$query = $this->db->get('bus');
return $query->result_array();
}
function tambah($data){
$this->db->insert('bus', $data);
return TRUE;
}
}
You can try this solution for your problem :
Please change your function name in controller.
function index(){
$data=array('data' => $this->model_databis->dbis()); // bus into dbis
$this->load->view('admin/databis',$data);
}
model
This is my model.using last query I get value in the model,but I don't get the value in the controller
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function email()
{
$this->db->select('email');
$this->db->from('change_password');
$result=$this->db->get();
return $result;
}
}
controller
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();
echo $dbemail;
}
}
CI is a MVC Framework. So you need to Command from Controller and get data from the models and finely you need to pass them to view. This is a best Practice
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
View
Extra Knowledge
View will be create under View folder/ Ex im using view name
as home.php
you can use your own stylings(its also created as normal html page.)
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}
I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}