Search product data not working in codeigniter - php

I tried to make a search bar so users will be able to search products by searching on the product name but its not working and I'm not able to search a product.
This is my controller function:
<?php
class SearchController extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Search_model');
}
public function index(){
$data = array();
}
function search_keyword(){
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
//laad de view
$this->load->view('result_view',$data);
}
}
?>
This is my model function:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Search_model extends CI_Model {
function __construct(){
parent::__construct();
}
function search($keyword) {
$this->db->select('product_naam');
$this->db->like('product_naam',$keyword);
$query = $this->db->get('products');
return $query->result_array();
}
}
?>
And this is my view file with searchbar:
<?php include_once('templates/header.php'); ?>
<center>
<h2>Zoek een cadeau</h2><br>
<form class="navbar-form" role="search" action="<?php echo base_url(); ?>SearchController/search_keyword" method="POST">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="keyword" size="30px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</center>
<div class="clearfix"></div>
<table>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo $row->product_naam?></td>
</tr>
<?php } ?>
</table>
<?php include_once('templates/footer.php'); ?>
I hope someone can help me with this problem!,
thanks

I don't think like() will help you much with your particular needs. Your best bet, I guess, would be to bypass the problem and use a where function containing your LIKE clause:
$this->db->select('product_naam')->from('products')
$this->db->where("product_naam LIKE '%$keyword%'")->get();
Or if you want pure ci you have to change your 'like' line :
$this->db->like('product_naam',$keyword);
For %$query you can use
$this->db->like('product_naam',$keyword,'before');
and for $query% you can use
$this->db->like('product_naam',$keyword,'after');

Because you use $query->result_array();, items should be taken by
<td><?php echo $row['product_naam']?></td>
result_array()
This method returns the query result as a pure array, or an empty
array when no result is produced
CI documentation

Related

Getting blank page when using dropdown for displaying the job list in codeigniter php

Hi i am having a job portal page where i will be displaying the list of the jobs and the users can apply for the jobs posted by clicking on apply now button a form will be where they need to fill their details etc..
The form contains position they are applying for, Name,Email,Mobile Number etc...
Position will be fetched automatically when clicking on applynow button.
Here i need to display all then jobs list in dropdown and the one which i have selected should be the first one in the text box but from my code it is displaying the total page as empty not displaying anything when we click on apply now..
Displaying the appplynow button(View):
<div class="applynow">Apply Now</div>
<div class="moreinfo" id="music" >More Info</div>
Controller(career/apply):
function apply($job_id)
{
$this->load->model('career_model');
$this->load->model('apply_model');
$data['joblist']=$this->apply_model->jobs_dropdown();
$data['records2']= $this->career_model->getcareerdatas($job_id);
$data['mainpage']='apply';
$this->load->view('templates/template',$data);
}
View:
<form name="applynow" id="applynow" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>apply/applynow" >
<div class ="applyus">
<?php if(isset($records2) && is_array($records2)):?>
<?php foreach ($records2 as $r):?>
<div class="applyposition ">
<input type="text" class="form-control positionapplied" name="positionapplied" id="positionapplied " value="<?php echo $r->job_name ;?>" readonly>
<?php
$joblist['']='--Select Category --';
$jobs_id="id='jobs_id' ";
echo form_dropdown('jobs_id',$joblist,$r->jobs_id,$jobs_id);
?>
</div>
<?php endforeach;endif;?>
<button type="submit" class="btn btn-success successss" id="sub" >Submit</button>
<a class="button cancel cancels" href="<?php echo site_url()?>career">Cancel</a>
<input type="reset" value="Reset" class="reset">
</div>
</form>
Model(apply_model):
function jobs_dropdown()
{
$this->table = 'jobs_list';
$this->where('status',1);
$joblist=$this->dropdown('jobs_id','job_name');
return $joblist;
}
In Error_Log got this error:
PHP Fatal error: Call to undefined method Apply_model::where() in /home/website/public_html/staging/application/models/apply_model.php on line 99
From the CodeIgniter documentation:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
}
This will make the database class available through the $this->db object.
So then in your model, you access the where() clause like so:
$this->db->where($blahblah);
Check their tutorial here: https://www.codeigniter.com/userguide3/tutorial/news_section.html
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Apply_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db = $this->load->database('default', true);
}
function jobs_dropdown()
{
$this->table = 'jobs_list';
$this->where('status',1);
$joblist=$this->dropdown('jobs_id','job_name');
return $joblist;
}
}
Save this as apply_model.php in application/model folder

Product search function not working in CodeIgniter

I tried to make a function in the CodeIgniter framework so users can search products from my database and display (echo) them if a certain product is found.
The problem is if I try to search for a keyword I see a blank page and nothing happens.
Search controller:
<?php
class SearchController extends CI_Controller {
public function index(){
$data = array();
//load view
$this->load->view('result_view',$data);
}
function search_keyword()
{
$this->load->model('Search_model');
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
}
}
My search model:
<?php
class Search_model extends CI_Model {
function search($keyword) {
$this->db->select('product_naam');
$this->db->like('product_naam',$keyword);
$query = $this->db->get('products');
return $query->result();
}
}
?>
My view file:
<h2>Zoek een cadeau</h2><br>
<form class="navbar-form" role="search" action="<?= base_url('SearchController/search_keyword')?>" method = "post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name = "keyword" size="30px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</center>
<div class="clearfix"></div>
Change
return $query->result();
to
return $query->result_array();
Also, you are not calling result page in function and passing data in view.
function search_keyword()
{
$this->load->model('Search_model');
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
$this->load->view('result_view',$data);
}

dropdown value to db codeigniter

I am working with CodeIgniter and have some problems with the database. I populate my dropdown list with customer names from table 'customers' in DB. I need to select the name from the dropdown, to input the name of the project and to add it to DB table 'projects', but in this table, I should have not the name of the customer, but its id. how can I implement it? And when I want to send not the id, but the name - I have an error: that customerName has NULL value.
Here is my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Add extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url', 'form'));
$this->load->model("addmodel");
$this->load->library('session');
}
private function view($page, $data=false) {
$this->load->view("header.php", $data);
$this->load->view($page, $data);
$this->load->view("footer.php", $data);
}
public function add_project() {
$this->load->database();
$data['customers']=$this->addmodel->select_customer();
$this->view('add/add_project.php', $data);
}
public function new_project() {
$this->load->database();
$data['customers']=$this->addmodel->select_customer();
$this->load->view("add/add_project.php", $data);
$projectName = $this->input->post("projectName");
$customerID = $this->input->post("customerName");
$this->addmodel->add_project($projectName, customerID);
redirect("add_project", "refresh");
}
}
Here is my model
<?php
class AddModel extends CI_Model {
public function __construct() {
$this->load->database();
}
function add_project($projectName, $customerID) {
$this->db->insert('projects', array(
'projectName' => $projectName,
'customerID' => $customerID
));
}
function select_customer() {
$query = $this->db->get('customers');
return $query;
}
}
?>
and, finally, here is my view
<?php echo form_open('new_project'); ?>
<div id="form-main">
<div id="form-div">
<form class="form" id="form1">
<select class="feedback-input" id="customer_selecter">
<option name="customerName">Select customer</option>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
<p class="projectName">
<input type="text" name="projectName" placeholder="Project name" required class="feedback-input" id="projectName" />
</p>
<div class="submit">
<button type="submit" id="button-red">Add</button>
<div class="ease"></div>
</form>
</div>
</form>
</div>
Change option value to $row->customerName, so that you can get customer name
<select class="feedback-input" id="customer_selecter" name="customerName>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
Change your Html code select box name like this:
<select class="feedback-input" id="customer_selecter" name="customerName">
<option >Select customer</option>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
And need to pass corrct variable for customerId with $ like this:
$this->addmodel->add_project($projectName, $customerID);

Incorrect use of the CodeIgniter form_validation() function throwing fatal error

I'm trying to apply CodeIgniter's form validation feature for an input form in one of my view pages. In that form, all fields are required. If all the fields are successfully filled up and then if the user clicks the submit button, it will redirect to another view page with the list of entries. If any field is left empty, validation messages will be displayed.
Here's my form in first view page - employee_add.php:
<div id="content" class="box">
<form action="<?php echo base_url();?>index.php/employee_adds/insert_employee_db" method="post">
<?php echo validation_errors(); ?>
<fieldset>
<legend id="add_employee_legend">Add New Employee</legend>
<div>
<label id= "emp_id_add_label">Employee ID:</label>
<input type="text" name="emp_id" id = "employee_id_add" placeholder="Employee ID"/>
</div>
<div>
<label id= "emp_name_add_label">Employee Name:</label>
<input type="text" name="emp_name" id = "employee_name_add" placeholder="Employee Name"/>
</div>
<input type="submit" name="submit" id = "employee_info_submit" value="Send"/>
</fieldset>
</form>
</div>
Here's my controller - employee_adds.php:
<?php
if ( ! defined('BASEPATH')){ exit('No direct script access allowed');}
class Employee_adds extends CI_Controller
{
function __construct()
{
parent::__construct();
#$this->load->helper('url');
$this->load->model('employee_model');
}
//Show all Employees or validate employees
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('emp_id', 'Employee ID', 'required');
$this->form_validation->set_rules('emp_name', 'Employee Name', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin_logins/employee_add');
}
else
{
$data['employee_list'] = $this->employee_model->get_all_employees();
$this->load->view('admin_logins/employee_list');
}
}
//Insert an employee
public function insert_employee_db()
{
$edata['emp_id'] = $this->input->post('emp_id');
$edata['emp_name'] = $this->input->post('emp_name');
$res = $this->employee_model->insert_employee($edata);
if($res)
{
header('location:'.base_url()."index.php/employee/".$this->index());
}
}
}
?>
Here's controller for master page named admin_logins.php:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_logins extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('admin_logins/index');
}
function employee()
{
$this->load->view('admin_logins/employee_add');
}
}
Here's my model - employee_model.php:
<?php
class Employee_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
//To retrieve all employees
public function get_all_employees()
{
$query = $this->db->get('employee');
return $query->result();
}
//To add a new employee to the database
public function insert_employee($data)
{
return $this->db->insert('employee', $data);
}
}
?>
Another view page named employee_list.php is going to load the list of successful entries in its central div:
<div id="content" class="box">
<table id = "employee_list_table" width="600">
<tr>
<th scope="col">Employee Id</th>
<th scope="col">Employee Name</th>
</tr>
<?php foreach ($employee_list as $e_key){ ?>
<tr>
<td><?php echo $e_key->emp_id; ?></td>
<td><?php echo $e_key->emp_name; ?></td>
</tr>
<?php }?>
</table>
</div>
Whenever I try to run the employee_add.php view page, I get the following error:
Fatal error: Call to undefined function validation_errors() in C:\wamp\www\CI\application\views\admin_logins\employee_add.php
From what I'm guessing, it may be due to one of the two following reason:
My code designing and layout was incorrect (disorganized coding in views, controller and model)
My code syntax was incorrect (most likely in the uses of form and form validation)
Or maybe, there are other reasons. I just can't figure out why, and exactly where in my code the errors occurred.
EDIT-1: As per user Ghost's solution, I've loaded the form_validation library in my admin_logins/employee method in controller - admin_logins.php. The fatal PHP error's gone, but still the validation is not working. As soon as I click submit button after keeping those fields empty, instead of validation message, I get the following error message:
A Database Error Occurred
Error Number: 1146
La table 'ci_test.employee' n'existe pas
INSERT INTO `employee` (`emp_id`, `emp_name`) VALUES ('', '')
Filename: C:\wamp\www\CI\system\database\DB_driver.php
It seems I still have more syntactical mistakes in my code.
It seems that both:
Employee_adds -> index() and Admin_logins ->employee() share the same view file:
'admin_logins/employee_add'
And when you put <?php echo validation_errors(); ?> What triggers the error is that looking on Admin_logins ->employee()
It seems that $this->load->library('form_validation'); is not loaded:
function employee()
{
$this->load->view('admin_logins/employee_add');
}
So by the time you access admin_logins/employee, it causes the error since the validation library hasn't been initialized.

get search value from one view to other codeigniter

I'm new to CodeIgniter and PHP.
I have search box and I have two views in my project related to this. My first header.php view has search input box and in second view search.php is displaying results. Here I need to get search value to display like (10 results found for "search value should be here").
Here is my header.php view:
<div class="left">
<div class="content _relative row-fluid">
<a id="menuCollection" role="button" class="dropdown-toggle" data-toggle="dropdown">
<input type="text" id="search" class="span12" placeholder="Search" autocomplete="off"/>
<i class="modern-pictograms-search">s</i>
</a>
</div>
</div>
Here is my search.php view.
<div class="search-result font-large"><?php echo count($value); ?> results found
<span class="font-bold">(this is the place i need to get search value)</span>
</div>
Please help me resolve this.
Follow this steps may be it will help you to solve your problem:
make chances in your files as i mention it.
header.php - view
<html>
...
<form action='controller/method' method='POST'>
<input type='text' name='search' />
<input type='submit' value='search' />
</form>
search.php - view
...
<div class='results'>
<?php foreach($results as $result){ ?>
<div><?php echo $result; ?></div>
<?php } ?>
</div>
...
search.php - Controller
class Search extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('database');
}
public function index(){
$this->db->select('*');
$this->db->from('yourtablename');
$this->db->where('yourcondition'.$this->input->post('search'));
$data['results']=$this->db->get();
$this->load->view('header');
$this->load->view('searchview',$data);
$this->load->view('footer');
}

Categories