This is my pagination function under which pagination code is written and i have called this pagination function(abc(offset=0)) in another function login_process in else if part, and passed abc function value to view admin_view inside the function login_process and in my login_view i have used pagination code and in that fetched data is shown with pagination but when i click the link 1 2 or any then comes error The requested URL /terse/welcome/abc/1 was not found on this server.and pagination is not implimented
my pagination function code:
public function abc($offset = 0){
$this->load->model('insert_model');
//$data ['query'] = $this->insert_model->view_data();
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/welcome/abc';
$items= $config['total_rows'] = 20;
$perpage=$config['per_page'] = 1;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
// Here we add the limit and the offset
// The second parameter is the limit, since we are showing
// 10 per page, the limit should be 10
$data['books'] = $this->db->get('register',5, $offset);
// Create custom headers
$header = array('id','username','lastname','email' , 'password', 'image','status');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
return $data['books'];
}
my login_process function in which i have called abc and passed its value to admin_view(only see its elseif part)
public function login_process()
{
$this->load->model('insert_model');
$data['users'] = array(
'fname' => $this->input->post('fname'),
'pass'=> $this->input->post('pass'),
);
$status= $this->insert_model->login_test($data);
$st['ss']=$this->insert_model->stat($data);
//print "<pre>"; print_r($status); die();
if($status == true && $st['ss'] [0] ['status'] == 0)
{
$ses = array(
'username' => $this->input->post('fname'),
'password' => $this->input->post('pass'),
);
$this->session->set_userdata($ses);
$im['pic']=$this->insert_model->image_fetc();
$this->load->view('header_view',$im);
$this->load->view('navside_view');
$this->load->view('user_view');
}
elseif($status == true && $st['ss']['0'] ['status'] == 1)
{
$ses = array(
'username' => $this->input->post('fname'),
'password' => $this->input->post('pass'),
);
$this->session->set_userdata($ses);
$im['pic']=$this->insert_model->image_fetc();
// print_r($aaa); die();
// $data['aa'] = "sd";
$data['books'] = $this->abc();
$this->load->view('header_view',$im);
$this->load->view('navside_view');
$this->load->view('admin_view', $data);
}
else
{
$this->load->view('header_view');
$this->load->view('navside_view');
$this->load->view('center_view_login');
}
}
my admin_view code
echo $this->table->generate($books); ?>
<?php echo $this->pagination->create_links(); ?>
please check what i have done wrong..
I had a similar problem and it was solved when I added the path to the controller function in my appplication/config/routes.php file
something similar to:
$route['terse/welcome/abc/(:num)'] = 'terse/welcome/abc';
Customize it. Hope it helps you.
Related
I want to create multiple user logins but with a different redirect page from each user level, there are 2 levels fyi, in my code can only redirect on 1 page, no matter that level 1 or 2 login is still directed to one page, what I want is that each level has its own page.. this is my code, please help me thank you
public function process()
{
$post = $this->input->post(null, TRUE);
if(isset($post['login'])) {
$this->load->model('m_user');
$query = $this->m_user->login($post);
if($query->num_rows() > 0){
$row = $query->row();
$params = array(
'userid' => $row->id_user,
'level' => $row->level
);
$this->session->set_userdata($params);
echo "<script>
alert('Login success');
window.location='".site_url('klien')."';
</script>";
} else {
echo "<script>
alert('Login failed, wrong username/password');
window.location='".site_url('auth/login')."';
</script>";
}
}
}
When logging in, specify in the redirect URL which page it will go to according to the level condition:
public function process() {
$post = $this->input->post(null, TRUE);
if (isset($post['login'])) {
$this->load->model('m_user');
$query = $this->m_user->login($post);
if ($query->num_rows() > 0) {
$row = $query->row();
$params = array(
'userid' => $row->id_user,
'level' => $row->level
);
$this->session->set_userdata($params);
echo "<script>alert('Login success');</script>";
if($row->level == 1) // Level condition to redirect specific page with example level 1
redirect('/pageX');
else if($row->level == 2) // Level condition to redirect specific page with example level 2
redirect('/pageY');
//... more levels = more conditions
} else {
echo "<script>alert('Login failed, wrong username/password');</script>";
redirect(site_url('auth/login'));
}
}
}
I have this PHP code where I am loading a quiz into the view. When I select and submit an answer the view is changed and displays only the correct message or the wrong one.
What I am trying to achieve is when clicking the submit button for the answer, to keep the question and just display under if correct or not.
This is the code that I have:
function guess()
{
$guess = $this->input->get('name',false);
$personid = $this->input->get('id',false);
if ($guess === null) {
$newguess['data'] = $this->Starmodel->getQuestion();
$this->load->view('starview',$newguess);
}
else {
$res= $this->Starmodel->isCorrectAnswer($personid,$guess);
$person = $this->load->view('starview', array('iscorrect' => $res,
'name' => $guess));
}
}
My main view is starview.php. Can I use divs and target a specific load a view inside the specific div? How can I do this?
Thank you
Let say this your startview view...
//Your question goes here
//Answer part
<?php if($this->input->post()):
echo $answer_view_data
endif; ?>
In your function guess()
function guess()
{
$guess = $this->input->get('name',false);
$personid = $this->input->get('id',false);
if ($guess === null) {
$newguess['data'] = $this->Starmodel->getQuestion();
$this->load->view('starview',$newguess);
}
else {
$res= $this->Starmodel->isCorrectAnswer($personid,$guess);
$answer_data = 'Sample answer data';
$person = $this->load->view('starview', array('iscorrect' => $res,
'name' => $guess, 'answer_data' => $answer_data));
}
}
im new in codeigniter and get some trouble with my function
here is my model
public function kode_unik(){
$q = $this->db->query("select MAX(RIGHT(id_obat,5)) as code_max from obat");
$code = "";
if($q->num_rows()>0){
foreach($q->result() as $cd){
$tmp = ((int)$cd->code_max)+1;
$hitung = strlen($tmp);
if ($hitung == 1 ){
$a = "0000".$tmp;
} elseif ($hitung == 2) {
$a = "000".$tmp;
}elseif ($hitung == 3) {
$a = "00".$tmp;
}elseif ($hitung == 4) {
$a = "0".$tmp;
}else{
$a = $tmp;
}
$code = sprintf("%s", $a);
}
}else{
$code = "0001";
}
$kodenyami = "E".$code;
return $kodenyami;
}
and then i wanna get the result of my models to show in my view.
here is my controller
public function add_data()
{
$this->load->helper( array('fungsidate', 'rupiah', 'url') );
$this->load->model('obat');
$this->load->database();
$data['a'] = $this->obat->tampil_data();
$data['b'] = $this->obat->kode_unik();
$componen = array(
"header" => $this->load->view("admin/header", array(), true),
"sidebar" => $this->load->view("admin/sidebar", array(), true),
"content" => $this->load->view("admin/add_obat", array("data" => $data), true)
);
$this->load->view('admin/index', $componen);
}
and my view goes here.
<i class="fa fa-medkit fa-5x"></i></div>
<div class="col-xs-9 text-right">
line20-> <div class="huge"><?php echo $b; ?></div>
<div>ID Obat</div>
the code give me an errors Message: Undefined variable: b
just don't know how to put the value of my models $kode_unik into my view ..
thanks
$data is already an array. Maybe like this :
"content" => $this->load->view("admin/add_obat", $data, TRUE)
CodeIgniter will extract the keys in $data so you will be able to use them in your view as $b
BTW instead of passing array() as a parameter of the load->view you can use NULL
P.S If you set the last parameter to TRUE, you will return the populated content of that view as a String (HTML in that case)
Here is something to start with :
public function __construct() {
parent::construct();
$this->load->helper(‘array(‘fungsidate’, ‘rupiah’, ‘url’);
$this->load->model(‘obat’);
$this->load->database(); // Should be already loaded depending on your config
}
public function add_data() {
// Set the data
$data[‘a’] = $this->obat->tampil_data();
$data[‘b’] = $this->obat->kodeunik();
// Load the views
$componen = array(
‘header’ => $this->load->view(‘admin/header’, NULL, TRUE);
‘sidebar’ => $this->load->view(‘admin/sidebar’, NULL, TRUE);
‘content’ => $this->load->view(‘admin/add_obat', $data, TRUE);
);
$this->load->view(‘admin/index’ ,$componen);
}
From what I understand about your code, in your templates you will need to echo the array keys
in admin/header you will echo $header;
in admin/sidebar you will echo $sidebar;
in admin/add_obat you will need to echo $content
and then you admin/index should be populated. I won’t do that if I were you to be honest I would probably load each template as is instead of outputting them as HTML string inside the final index. I don’t know exactly how you did your structure so it’s kinda hard for me to guess well. But in my case I would have use something like this instead of the componen array
public function add_data() {
// Set the data
$data[‘a’] = $this->obat->tampil_data();
$data[‘b’] = $this->obat->kodeunik();
// Load the views
$this->load->view(‘admin/header’);
$this->load->view(‘admin/sidebar’);
$this->load->view(‘admin/add_obat, $data); // This should be the body template of your page
$this->load->view(‘admin/footer’); // I guess you have a footer template
}
I hope this will help you!
I'm trying to prevent the user to create the same username. well my real problem is how to loop a list of data from model in controller. Maybe we know how to loop it in view by using this -> data['user'] and in view we can call $user. but how can we do that in controller layer.
here's my code
Controller
$username = strtolower($this->input->post('name'));
$fixUsername = str_replace(" ",".",$username);
$counter = 1;
$list[] = $this->addusermodel->getAllUsername();
for($i=0;$i<sizeof($list);$i++) {
if($list[$i] == $fixUsername) {
$counter = 0;
}
}
if($counter == 0) {
$data['result'] = "The username has already been taken";
$this->load->view('adduserview',$data);
} else {
$data = array(
'Nama' => $this->input->post('name'),
'Username' => $fixUsername."#praba",
'Password' => md5($this->input->post('password')),
'created' => date("Y-m-d h:i:sa"),
'createdBy' => $createdBy,
'lastModified' => date("Y-m-d h:i:sa"),
'lastModifiedBy' => $lastModifiedBy
);
$this->addusermodel->saveUser($data);
//$data['Username'] = $listName;
$data['message'] = "New user successfully added.";
$data['messageContent'] = "The username: ".$fixUsername."#praba". $counter;
$this->load->view('successpageview',$data);
//redirect('successpageview','refresh');
}
my model (is like usual)
function getAllUsername() {
$this->db->select('Username');
$this->db->from('tbluser');
$query = $this->db->get();
return $query->result_array();
}
I think a better approach would be to create another function in your model, which searches your database by ID, or by email, or by another unique field. If the function returns a row - then the user exists. If it returns nothing - then add a new user.
I have this pagination function abc and i want to pass this function to another functions elseif part and in that else if part i want to pass $data['books'] through the view... this is my controller and other function is in which i want to pass data of abc function is
public function abc($offset = 0){
$this->load->model('insert_model');
//$data ['query'] = $this->insert_model->view_data();
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/welcome/';
$items= $config['total_rows'] = 20;
$perpage=$config['per_page'] = 1;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
// Here we add the limit and the offset
// The second parameter is the limit, since we are showing
// 10 per page, the limit should be 10
$data['books'] = $this->db->get('register',5, $offset);
// Create custom headers
$header = array('id','username','lastname','email' , 'password', 'image','status');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
}
in the following function's elseif part i want to pass abc functions $data part inside view admin.. how can i pass it so that i can do pagination in that view
public function login_process()
{
$this->load->model('insert_model');
$data['users'] = array(
'fname' => $this->input->post('fname'),
'pass'=> $this->input->post('pass'),
);
$status= $this->insert_model->login_test($data);
$st['ss']=$this->insert_model->stat($data);
//print "<pre>"; print_r($status); die();
if($status == true && $st['ss'] [0] ['status'] == 0)
{
$ses = array(
'username' => $this->input->post('fname'),
'password' => $this->input->post('pass'),
);
$this->session->set_userdata($ses);
$im['pic']=$this->insert_model->image_fetc();
$this->load->view('header_view',$im);
$this->load->view('navside_view');
$this->load->view('user_view');
}
elseif($status == true && $st['ss']['0'] ['status'] == 1)
{
$ses = array(
'username' => $this->input->post('fname'),
'password' => $this->input->post('pass'),
);
$this->session->set_userdata($ses);
$im['pic']=$this->insert_model->image_fetc();
$this->abc();
$this->load->view('header_view',$im);
$this->load->view('navside_view');
$this->load->view('admin_view', $data);
}
else
{
$this->load->view('header_view');
$this->load->view('navside_view');
$this->load->view('center_view_login');
}
}
in my admin_view i am writing the following code and try to use the data which i have passed to admin view of function abc i.e $aaa code is:-
<?php echo $this->table->generate($books); ?>
<?php echo $this->pagination->create_links(); ?>
but there coming error undefined variable books and this books variable is defined in abc function whose data is passed through loginprocess elseif part in view admin view through $aaa variable please help me to sort it out
you can try this
public function abc($offset = 0, $status){
$this->load->model('insert_model');
//$data ['query'] = $this->insert_model->view_data();
$this->load->library('table');
// Load Pagination`enter code here`
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/welcome/';
$items= $config['total_rows'] = 20;
$perpage=$config['per_page'] = 1;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
// Here we add the limit and the offset
// The second parameter is the limit, since we are showing
// 10 per page, the limit should be 10
$data['books'] = $this->db->get('register',5, $offset);
if($status){
return $data['books'];
}
// Create custom headers
$header = array('id','username','lastname','email' , 'password', 'image','status');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
}
second function elseif part
elseif($status == true && $st['ss']['0'] ['status'] == 1)
{
$ses = array(
'username' => $this->input->post('fname'),
'password' => $this->input->post('pass'),
);
$this->session->set_userdata($ses);
$im['pic']=$this->insert_model->image_fetc();
$aaa = $this->abc(0,true);
$this->load->view('header_view',$im);
$this->load->view('navside_view');
$this->load->view('admin_view', $aaa);
}