I have good command over core PHP, and I have just started learning CodeIgniter. I have created some pages according to the CodeIgniter's Tutorial. But I am stuck in this tutorial: http://www.codeigniter.com/user_guide/tutorial/news_section.html
Following is the code:
/application/config/routes.php :
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['users/(:any)'] = 'users/view/$1';
$route['users'] = 'users';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
/application/models/Users_model.php :
class Users_model extends CI_Model
{ public function __construct()
{ $this->load->database();
}
public function get_users($username = FALSE)
{ if ($username === FALSE)
{ $query = $this->db->get('users');
return $query->result_array();
}
$query = $this->db->get_where('users', array('username' => $username));
return $query->row_array();
}
}
/application/controllers/Users.php :
class Users extends CI_Controller
{ public function __construct()
{ parent::__construct();
$this->load->model('users_model');
$this->load->helper('url_helper');
}
public function index()
{ $data['users'] = $this->users_model->get_users();
$data['title'] = 'List of Users';
$this->load->view('templates/header', $data);
$this->load->view('users/index', $data);
$this->load->view('templates/footer');
}
public function view($username = NULL)
{ $data['user'] = $this->users_model->get_users($username);
if (empty($data['user']))
{ show_404();
}
$data['title'] = $data['user']['display_name'];
$this->load->view('templates/header', $data);
$this->load->view('users/view', $data);
$this->load->view('templates/footer');
}
}
/application/views/users/index.php :
<div class='main'>
<table border='1'>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Username</th>
</tr>
<?php foreach ($users as $user) { ?>
<tr>
<td><?php echo $user['display_name'] ?></td>
<td><?php echo $user['email'] ?></td>
<td>#<?php echo $user['username'] ?></td>
</tr>
<?php } ?>
</table>
</div>
/application/views/users/view.php :
<h2><?php $user['display_name'] ?></h2>
<p>#<?php $user['username'] ?></p>
MySQL 'users' table's structure :
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(255),
username VARCHAR(255),
display_name VARCHAR(50)
);
(You can replace .... (4 dots) with your desired domain name OR localhost in the below code.)
Output of ..../index.php/users page :
<html>
<head>
<title>List of Users</title>
</head>
<body>
<h1>List of Users</h1>
<div class='main'>
<table border='1'>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Username</th>
</tr>
<tr>
<td>Nikunj Bhatt</td>
<td>nikunj#example.com</td>
<td>#NikunjBhatt</td>
</tr>
</table>
</div>
</body>
</html>
This output is proper, but when I click on the username, it is redirecting to ..../index.php/users/NikunjBhatt page and this page is showing the error "This webpage is not available" "ERR_CONNECTION_REFUSED" in Google Chrome.
So, where is the problem? What I am missing?
Try to write code as below:-
public function view($username = ""){
// do your stuff here
}
Related
I have made an admin page and user page, and i want to show the list of users who have registered in database when the admin logs in.
For that i've created a model as follows,
public function regi_users(){
$q = $this->db->query("SELECT username FROM public");
return $q;
}
and this i am accessing through a view i have created, to which, when admin logs in, he is redirected, as follows,
account.php
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
$this->load->model('loginmodel');
$qresult = $this->loginmodel->regi_user();
foreach ($qresult as $row) {
echo $row->username;
}
?>
</body>
but when my admin logs in, he's shown the following error,
Fatal error: Call to undefined method LoginModel::regi_user() in
E:\wamp64\www\ci\application\controllers\account.php on line 11
what am i doing wrong here? I apologize if it is a silly question but i am kind of new to php
Thank you for your suggestions
Controller
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('loginmodel');
}
public function FunctionName($value='')
{
$this->data["users"] = $this->loginmodel->regi_user();
$this->load->view('account',$this->data);
}
}
Model
class Loginmodel extends CI_Model{
function __construct() {
parent::__construct();
}
public function regi_user() {
$query = $this->db->get('table_name')->result();
return $query;
}
}
View
<table class="table">
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $row) { ?>
<tr>
<td><?php echo $row->username; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Your query should be like this
public function regi_users(){
return $this->db->select('username')->from('table_name')->get()->result_array();
}
controller
class User extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('YourModelName');
}
public function fetchUser()
{
$data['user']=$this->YourModelName->fetchUsers();
$this->load->view('yourViewClass',$data);
}
}
Model
class YourModelName extends CI_Model
{
function __construct()
{
$this->load->database();
}
public function fetchUsers()
{
$query = $this->db->select('*')->from('table_name');
$query=$this->db->get();
if($query->num_rows()>0)
{
$result=$query->result();
return $result;
}else{
return 0;
}
}
}
view
<table>
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?php foreach ($user as $row) { ?>
<tr>
<td><?php echo $row->username; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Model name should be LoginModel.php
I am using Codeigniter 3 and trying CRUD operation. I have created the basic crud operation and am showing the data in a table however I have linked a paragraph tag in the controller below the table to the form controller, If I want to enter another data
The issue is when I click on the link to enter another data it redirect me the original form in controller but when I enter the data and submit it, The data is shown below the table in the paragraph tag.
I am not able understand why this is happening as the controller is the same
I had faced a similar issue before when redirecting in controller.I had redirected the page after submission to show_form() controller which was basically redirecting the page to $this->load->view('learn/view_form');
in which I have kept a condition that if No data is present click to enter. Now when it redirects to show_form() controller it goes into else condition even if the data is present
CONTROLLER
<?php
defined('BASEPATH') OR exit("No direct script access allowed");
class Learning extends CI_Controller{
public function __construct(){
parent::__construct();
$this ->load->helper("url");
$this->load->model("tatti_test");
$this->load->database();
$this->load->helper();
}
//functions should be passed here
//creating a function
function start_learn() {
//this varible
$this->load->view('learn/start_learn');
}
function start_crud(){
$this->load->view('learn/form');
}
function show_form(){
$this->load->view("learn/view_form");
}
function insert_form(){
$name = $this->input->post("u_name");
$email = $this->input->post("u_email");
$mobile = $this->input->post("u_mobile");
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("u_file")){
$file='noimage.png';
}
else {
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->insert_tatti($data);
redirect("learning/view_form");
}
function view_form(){
$data['returned_data']=$this->tatti_test->show_form();
$this->load->view("learn/view_form",$data);
}
function delete_entry(){
$id=$this->uri->segment(3);
$data=$this->tatti_test->for_unlink($id);
$filepath="./assets/images/".$data['file_name'];
unlink($filepath);
$this->tatti_test->delete_entry($id);
redirect('learning/view_form');
}
function time_to_update(){
$id=$this->uri->segment(3);
$data['fetched_update_entry']=$this->tatti_test->update_entry($id);
$this->load->view("learn/update.php",$data); //bus associative array hi leta hai
}
function up_db(){
$name =$this->input->post('up_name');
$email = $this->input->post('up_email');
$mobile = $this->input->post('up_mobile');
$file = $this->input->post('up_file');
$id = $this->input->post('up_id');
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("up_file")){
$data= $this->tatti_test->remove_prev($id);
$file=$data['file_name'];
}
else {
$data= $this->tatti_test->remove_prev($id);
$path="./assets/images/".$data['file_name'];
unlink($path);
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->up_nw($data,$id);
redirect('learning/view_form');
}
} /*this accesses command from main ci controller */
?>
VIEW
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } ?>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
MODEL
<?php
class Tatti_test extends CI_Model{
function insert_tatti($insert_data){
$this->db->insert("f_form",$insert_data);
}
function show_form(){
$query = $this->db->get("f_form");
$response=[];
if ($query->num_rows() > 0){
$response = $query->result_array();
}
else {
$response = 0;
}
return $response;
}
function for_unlink($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response=[];
foreach ($query->result_array() as $rows){
return $response = $rows;
}
}
function delete_entry($id){
$this->db->where("id",$id);
$this->db->delete("f_form");
}
function update_entry($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response = [];
if($query->num_rows() > 0 ){
foreach($query->result_array() as $rows);
$response = $rows;
}
return $response;
}
function up_nw($introduced_data,$id){
$this->db->set($introduced_data);
$this->db->where('id',$id);
$this->db->update('f_form');
}
function remove_prev($id){
$this->db->where('id',$id);
$query = $this->db->get('f_form');
$response = [];
foreach($query->result_array() as $rows){
$response=$rows;
}
return $response;
}
}
?>
This is how the data is showing when clicked on the link below table
enter image description here
You're html formatting is messed up. You should have the closing </table> outside your foreach loop or premature <table> closure.
Also moved the Add another entry link outside the foreach loop. So it only appears once, and your document format not messed up.
You can use this fixed view instead:
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
I realize that you are trying to do CURD,
first of all, try this to improve your code and fill the missing library of codeigniter: https://github.com/avenirer/CodeIgniter-MY_Model
For your code:
No data passed to the view in show_form(),
You should check the form submission and the conditions to load the view,
simple thing to do is follow the best practice using ready scripts,
Hope this will be useful,
The thing is when i use this code
in my model :
public function get_all_subject_tasks(){
$this->db->select('*');
$this->db->from('task');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("task_id", "desc");
$query_result=$this->db->get();
$result=$query_result->result();
return $result;
}
in my controller:
public function subjects($t,$action=""){
$data=array();
$data['subject_tasks']=$this->Schoolmodel->get_all_subject_tasks($t);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
in my php page:
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } ?>
</table>
</div>
I get all of the tasks that are in the database, without any subject filter.
So my question is
How do i make the page echo the tasks based on which subject they are in?
Also here is how i have setup my database structure
subject_id
task_id
task_name
task_desc
user_id
Where subject_id is the id of the subject where the task is inserted in.
<?php
// your model
public function getTask($subject_id)
{
$result = $this->db
->where("subject_id", $subject_id) // !!!
->where("user_id", $this->session->userdata('user_id')) // !!!
->order_by("task_id", "desc")
->get('tasks');
return $result->num_rows() > 0 ?
$result->result() : false;
}
// your class
class School extends CI_Controller {
public function __construct() {
parent::__construct();
if($this->session->userdata('logged_in')==FALSE){
redirect('User');
}
$this->load->database();
$this->load->model('Schoolmodel');
$this->load->library('form_validation');
}
public function subjects($subject_id,$action=""){
$data=array();
$data['subject_tasks'] = $this->Schoolmodel->getTask($subject_id);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
}
?>
// your view
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php if(false !== $subject_tasks) { foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } } ?>
</table>
</div>
This is not going to work:
$this->db->where("subject_id",$subject_id AND "user_id",$this->session->userdata('user_id'));
From Active Record documentations: Multiple calls to where will result in AND:
$this->db->where("subject_id", $subject_id);
$this->db->where("user_id", $this->session->userdata('user_id'));
I'm developing this pagination page with ajax on code igniter hmvc but I get this javascript:void(0) on the uri of my link to the next pages. how can I fix this?
here is my codes.
controllers
<?php
class Job_Titles extends MY_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Job_Titles_Model');
$this->load->library('Ajax_pagination');
$this->perPage = 10;
}
// VIEW REDIRECTING /////////////////////////////////////////////////////////
public function index(){
/// view ajax config/////
$data = array();
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//configuration
$config['first_link'] = 'First';
$config['div'] = 'postList'; //parent div tag id
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
$data['content_view'] = 'Job_Titles/jobtitles_read';
$data['content'] = $this->Job_Titles_Model->getRows(array('limit'=>$this->perPage));
$data['false'] = FALSE;
$this->templates->admin_template($data);
}
//// pagination
public function ajaxPaginationData(){
$data = array();
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//pagination config
$config['first_link'] = 'First';
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get post data
$data['content_view'] = 'Job_Titles/ajax-pagination-data';
$data['content'] = $this->Job_Titles_Model->getRows(array('start'=>$offset,'limit'=>$this->perPage));
$this->templates->admin_template($data);
}
}
models
public function getRows($params = array()){
$this->db->select('*');
$this->db->from($this->table);
if(array_key_exists("start", $params) && array_key_exists("limit", $params)){
$this->db->limit($params['limit'],$params['start']);
}elseif (!array_key_exists("start", $params) && array_key_exists("limit", $params)) {
$this->db->limit($params['limit']);
}
$query = $this->db->get();
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
views
<?php
echo anchor('Job_Titles/add_view','ADD ');
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div id = "container" class = "page-header">
<table class= "table table-bordered">
<thead>
<tr>
<th>JOB CODE</th>
<th>JOB NAME</th>
<th></th>
<th></th>
<tr>
</thead>
<tbody>
<?php if(!empty($content)){foreach ($content as $job_title) {?>
<tr>
<td><?php echo $job_title['JOB_CODE']; ?></td>
<td><?php echo $job_title['JOB_NAME']; ?></td>
<td></i></td>
<td><i class="fa fa-trash-o"></i></td>
</tr>
<?php }}else{ ?>
<li class="err_msg">Post(s) not available.</li>
<?php } ?>
</tbody>
</table>
<?php echo $this->ajax_pagination->create_links(); ?>
</div>
I'm developing this pagination page with ajax on code igniter hmvc but I get this javascript:void(0) on the uri of my link to the next pages. how can I fix this?
can someone please help me with the my search function on my code? Im new into php and im using codeigniter framework for the development.
search view
<div id="admin-col">
<div>
<?php echo form_open('search_admin/search_admins'); ?>
<?php
$data = array('name'=>'search', 'id'=>'search');
echo form_input($data);
?>
<?php
$data = array('name'=>'submit', 'id'=>'submit', 'value'=>'Search Admin');
echo form_submit($data);
?>
</div>
controller
class Search_admin extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security');
$this->load->library('tank_auth');
$this->load->model('users/usermodel');
}
function index()
{
if(!$this->tank_auth->is_logged_in())
{
redirect('/auth/login');
}
else
{
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
}
}
function search_admins()
{
$data['query']=$this->usermodel->search_admins($this->input->post('search'));
$this->load->view('admin/users/search_result',$data);
}
}
model
class UserModel extends CI_Model {
function _construct() {
parent::_construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security');
$this->load->library('tank_auth');
$this->load->model('users/usermodel');
$this->load->database();
}
function search_admins($search)
{
return $query = $this->db->get_where('users', array('username ='=> '$search'))->result();
}
}
view for the result
<div id="admin-col">
<table>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email Address</th>
<th>Actions</th>
</tr>
<?php foreach($query as $row){?>
<tr>
<td><?php print $row->id; ?></td>
<td><?php print $row->username; ?></td>
<td><?php print $row->email; ?></td>
<td><?=anchor('userslist/get_Admin/'.$row->id, 'Edit');?> | <?=anchor('userslist/deleteAdmin/'.$row->id, 'Delete');?></td>
</tr>
<?php }?>
</table>
</div>
you had
array('username ='=> '$search')
in your search_admins() function
try
array('username'=> $search)
instead