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
Related
I already get the results from query to display them on a table it looks like this.
table that shows all employees
Then I can do a search query to get the specific employee I´m looking for:
search for name result
The question is how can I choose or pick that result to display that information in a form or table or anything I can use to pass those results to other inputs.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Busqueda de empleados</h2>
<form action="" method="post" id="myform1">
ver todos los empleados:
<input type="text" name="buscar">
<input type="submit" name="enviar1" id="enviar1" value="enviar">
<hr>
</form>
<table class="table table-bordered table-responsive">
<thead>
<tr class= "info" bgcolor='#B9EDFD'>
<th >INTERNO</th>
<th>CURP</th>
<th>NOMBRE</th>
<th>A_PATERNO</th>
<th>A_MATERNO</th>
</thead>
<tbody>
<?php
foreach ($empleados-> result() as $row) {
echo "<tr>";
echo "<td bgcolor='#EAECEE'>".$row->Interno;"</td>";
echo "<td bgcolor='#EAECEE'>".$row->Curp;"</td>";
echo "<td bgcolor='#EAECEE'>".$row->Nombre;"</td>";
echo "<td bgcolor='#EAECEE'>".$row->A_Paterno;"</td>";
echo "<td bgcolor='#EAECEE'>".$row->A_Materno;"</td>";
"</tr>";
}
?>
</tbody>
</table>
</body>
</html>`
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Empleados extends CI_Controller {
public function __construct() {
parent::__construct();
// Load the todo model to make it available
// to *all* of the controller's actions
$this->load->model('consultas_M');
}
public function busqueda(){
if ($_POST) {
$buscar = $this->input->post('buscar');
} else {
$buscar = '';
}
$data['empleados'] = $this->consultas_M->muestra($buscar);
// 3. Render the view:
$this->load->view('main/busqueda', $data);
}
}`
Model:
function __construct() {
parent::__construct();
$this->load->database();
}
function muestra($bus)
{
$this->db->like('interno', $bus);
$this->db->or_like('Curp', $bus);
$this->db->or_like('Nombre', $bus);
$this->db->or_like('A_Paterno', $bus);
$this->db->or_like('A_Materno', $bus);
$q= $this->db ->get('empleados');
if($q -> num_rows() >=0){
return $q;
}else{
return false;
}
}
I try to view my record from DB by Code Igniter following the tutorial on this site but it show the error message
what can I try to make it identified?
Belows is view "view_inlist.php" code
<table class="table table-striped table-bordered">
<tr>
<td><strong>ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Number</strong></td>
<td><strong>Type</strong></td>
<td><strong>Unit</strong></td>
<td><strong>Date</strong></td>
</tr>
<?php foreach($LIST as $list)
{?>
<tr>
<td><?=$list->ID;?></td>
<td><?=$list->Name;?></td>
<td><?=$list->Letter_Number;?></td>
<td><?=$list->Letter_Type;?></td>
<td><?=$list->Unit;?></td>
<td><?=$list->Date;?></td>
</tr>
<?php }?>
</table>
Here is controller "Main.php"
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function check_inList() {
$this->load->model('login');
$query = $this->login->check_inlist();
$data['LIST'] = null;
if($query){
$data['LIST'] = $query;
}
$this->load->view('form/view_inlist', $data);
}
}
Here is Model "login.php"
function check_inlist(){
$this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,Date");
$this->db->from('in_list');
$query = $this->db->get();
return $query->result();
}
try this
in controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login');
}
public function check_inList() {
$data['all_list'] = $this->login->check_inlist();
$this->load->view('form/view_inlist', $data);
}
}
in model
<?php
class Login extends CI_Model {
function check_inlist(){
$this->db->select('*');
$this->db->from('in_list');
$query = $this->db->get();
return $query->result();
}
}
in view page
<?php foreach($all_list as $list) {?>
<tr>
<td><?php echo $list->ID;?></td>
<td><?php echo $list->Name;?></td>
<td><?php echo $list->Letter_Number;?></td>
<td><?php echo $list->Letter_Type;?></td>
<td><?php echo $list->Unit;?></td>
<td><?php echo $list->Date;?></td>
</tr>
<?php }?>
first check your database structure because in model you fetch EMAIL column
$this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,EMAIL");
And you print Date column <td><?=$list->Date;?></td> in view
1.Controller
Main.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('login');
$this->load->helper('url_helper');
}
public function check_inList() {
$query = $this->login->check_inlist();
$data['LIST'] = null;
if($query){
$data['LIST'] = $query;
}
$this->load->view('view_inlist', $data);
}
}
?>
2)Views
view_inlist.php
<table class="table table-striped table-bordered">
<tr>
<td><strong>ID</strong></td>
<td><strong>Name</strong></td>
<td><strong>Number</strong></td>
<td><strong>Type</strong></td>
<td><strong>Unit</strong></td>
<td><strong>Date</strong></td>
</tr>
<?php foreach($LIST as $list)
{?>
<tr>
<td><?=$list->ID;?></td>
<td><?=$list->Name;?></td>
<td><?=$list->Letter_Number;?></td>
<td><?=$list->Letter_Type;?></td>
<td><?=$list->Unit;?></td>
<td><?=$list->EMAIL;?></td>
</tr>
<?php }?>
</table>
3.Model Login.php
<?php
class Login extends CI_Model {
function HomeModel(){
parent::Model();
}
function check_inlist(){
$this->db->select("ID,Name,Letter_Number,Letter_Type,Unit,EMAIL");
$this->db->from('in_list');
$query = $this->db->get();
return $query->result();
}
}
?>
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
}
Adding products to my cart is working fine, but after adding product to cart its not able to view it in my view page without refreshing view page,
I tried redirect() method still no use.
How do i view my page after adding products to cart.
Any help??
my controller:
<?php
class Cart extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addtocart($cal_id, $f_id) {
if (empty($cal_id)) {
$this->load->view('Cart_view');
}
$this->load->model('cart_model');
$product = $this->cart_model->cart_contents($cal_id, $f_id);
foreach ($product as $row) {
$name = $row->title;
$fees = $row->fees;
}
$product = array(
'id' => $cal_id,
'qty' => 1,
'price' => $fees,
'name' => $name,
);
print_r($product);
$this->cart->insert($product);
$this->load->view('Cart_view');
}
public function destroy() {
$this->cart->destroy();
}
}
?>
my model:
<?php
class cart_model extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function cart_contents($cal_id,$f_id)
{
$product=$this->db->select('*')
->from('calander')
->join('fees','calander.cal_id=fees.cal_id')
->where('fees.cal_id',$cal_id)
->where('fees.f_id',$f_id)
->get();
return $product->result();
}
}
?>
my view
<html>
<head><title></title></head>
<body>
<?php
if ($cart = $this->cart->contents()) {
?><table>
<tr>
<td>
<h3><u>Cart page</u></h3>
</td>
</tr>
<tr>
<th>course</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php
foreach ($cart as $contents) {
?><tr>
<td><?php echo $contents['name']; ?></td>
<td><input type="text" value="<?php echo $contents['qty']; ?>"></td>
<td><?php echo $contents['price']; ?></td>
</tr>
<?php } ?>
</table> <?php
// redirect(base_url('/index.php/Cart/Cart_view'));
}
?>
</body>
</html>
I dont understand what you are trying to do. When you load your view $this->load->view('Cart_view');, it is required to pass the data array, containing all the variables into the view like this $this->load->view('Cart_view',$data); . Without data array, view cannot display data. When ever you write code atleast, make variables names in such a way that other people will be able to understand what that variable stands for. Without enough data, i am taking the liberty of modifying your code
Controller
public function addtocart($cal_id, $f_id) {
$data=array(
'view'=>''
);
if (empty($cal_id)) {
$this->load->view('Cart_view',$data);
} else {
$this->load->model('cart_model');
$product = $this->cart_model->cart_contents($cal_id, $f_id);
$data['view']='';
foreach($product as $row):
$data['view'].=' <tr>
<td>'.$row->title.'</td>
<td>'.$row->qty.'</td>
<td>'.$row->price.'</td>
</tr>';
endforeach;
$this->load->view('Cart_view',$data);
}
}
MODEL
public function cart_contents($cal_id,$f_id){
$this->db->select('*')
$this->db->from('calander')
$this->db->join('fees','calander.cal_id=fees.cal_id')
$this->db->where('fees.cal_id',$cal_id)
$this->db->where('fees.f_id',$f_id);
$query = $this->db->get();
return $query->result();
}
view
<html>
<head><title></title></head>
<body>
<table>
<tr>
<td>
<h3><u>Cart page</u></h3>
</td>
</tr>
<tr>
<th>course</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php if($count>0){
echo $view;
}?>
</table>
</body>
</html>
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