search a database record using codeigniter - php

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

Related

undefined method in model in codeigniter

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

Unable to call fetch data from controller to view in codeigniter

I was unable to show data on view from the database using controller and model in CodeIgniter.
Controller Code:
class Leads extends CI_Controller {
public function show($id) {
$this->load->model('leads_model');
$leads = $this->leads_model->get_leads($id);
$data['name'] = $leads['name'];
$data['email'] = $leads['email'];
$data['contact'] = $leads['contact'];
$data['referral'] = $leads['referral'];
$data['project_detail'] = $leads['project_detail'];
$data['note'] = $leads['note'];
$this->load->view('layouts/header', $data);
$this->load->view('layouts/sidebar', $data);
$this->load->view('pages/leads', $data);
$this->load->view('layouts/footer', $data);
}
Model code:
class Leads_model extends CI_Model {
public function __construct() {
$this -> load -> database();
}
public function get_leads($id) {
if ($id != FALSE) {
$query = $this -> db -> get('leads', array('lead_id' => $id));
return $query -> row_array();
}
else {
return FALSE;
}
}
view code:
<td>
<?php echo $data['name']; ?>
</td>
<td>
<?php echo $data['email']; ?>
</td>
<td>
<?php echo $data['contact']; ?>
</td>
<td>
<?php echo $data['referral']; ?>
</td>
<td>
<?php echo $data['project_detail']; ?>
</td>
<td>
<?php echo $data['note']; ?>
</td>
$leads is an array so there should be foreach loop in the view
In controller :
public function show($id)
{
$this->load->model('leads_model');
$leads = $this->leads_model->get_leads($id);
$data['leads'] = $leads;
$this->load->view('layouts/header', $data);
$this->load->view('layouts/sidebar', $data);
$this->load->view('pages/leads', $data);
}
Your Model :
public function get_leads($id) {
if ($id != FALSE) {
$this->db->where(array('lead_id' => $id));
$query = $this->db->get('leads');
return $query->row_array();
}
else {
return FALSE;
}
}
Your view should be like this:
<?php if ($leads) {
foreach($leads as $lead) {?>
<?php echo $lead['name']; ?>
<?php echo $lead['email']; ?>
<?php echo $lead['contact']; ?>
<?php echo $lead['referral']; ?>
<?php echo $lead['project_details']; ?>
<?php echo $lead['note']; ?>
}
}?>
if single row data: Use variable without $data in your view
if your model method return $query->row();
Your view :
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $contact; ?></td>
<td><?php echo $referral; ?></td>
<td><?php echo $project_detail; ?></td>
<td><?php echo $note; ?></td>
for more : https://www.codeigniter.com/user_guide/database/query_builder.html

How to view record from DB with CodeIgniter

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();
}
}
?>

delete and update specific row in codeigniter

I am new in codeigniter.In my view page I am showing the data from database in a table i have two buttons in table to edit and delete the row..I want to delete a specific row from database through id.
## view
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>last name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td><button name="edit">Edit</button></td>
<td><button name="delete">Delete</button></td>
</tr>
<?php } ?>
</tbody>
----------
## Controller ##
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message',$data);
}
public function register()
{
$this->load->view("register");
}
public function receive()
{
$fname= $this->input->post("fname");
$this->load->model('Model');
$this->Model->insertdata($fname);
// echo $this->input->post("fname");
// echo $this->input->post("lname");
// echo $this->input->post("password");
// echo $this->input->post("Email");
}
public function all_user(){
$this->load->model('Model');
$data['records'] = $this->Model->get_all_users();
$this->load->view("store_data",$data);
}
public function row_delete(){
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
----------
## Model ##
class Model extends CI_Model{
public function insertdata($fname)
{
$data = array
('fname'=> $this->input->post("fname"),
'lname' => $this->input->post("lname"),
'email' => $this->input->post("email"),
'password' => $this->input->post("password"),
);
$this->db->insert('register',$data);
}
public function get_all_users()
{
$query= $this->db->get('register');
return $query->result();
}
public function delete_row()
{$this->db->where('id', $id);
$this->db->delete('');
}
}
Try this in ur code
//view
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td>Delete</td> <td>Edit</td>
</tr>
<?php } ?>
</tbody>
make sure u pass the record id to the view and base_url is configured on config file
//controller
public function row_delete(){
if(isset($_GET['id'])){
$id=$_GET['id'];
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);}
}
//model
public function delete_row()
{
$this->db->where('id', $id);
$this->db->delete('register');
}
reference links here and here
Just pass the parameters to the where function and table name to delete function:
public function delete_row()
{
// Set where
$this->db->where('id', $id);
// Run query
return $this->db->delete('register');
}
Cheers

Codeigniter shopping cart, view the contents in the cart page

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>

Categories