My search code is not outputting any result on codeigniter - php

Please help!!
I am trying to search database to produce results for my search query but it does not output any result even when the searched term exist in the database
Here is my controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Search extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("profile_model");
$this->load->model("model_home");
}
public function index(){
$data = array();
$search_term = $this->input->post('search');
if($query = $this->model_home->car_search())
{
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
}
Here is my model
public function car_search($search_term='default')
{
$this->db->select('*');
$this->db->like('car_make',$search_term);
$this->db->or_like('car_model',$search_term);
$this->db->or_like('car_year',$search_term);
$this->db->or_like('registration_number',$search_term);
$this->db->or_like('engine_number',$search_term);
$this->db->or_like('chasis_number',$search_term);
$query = $this->db->get('cars');
return $query->result_array();
}
My search form
<?php
echo form_open('search');
echo form_input(array('name'=>'search'));
echo form_submit('search_submit','submit');
?>
My search result page
<?php
if(isset($car)) :
foreach($car as $row) {
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif; ?>
Its always outputting "Not Working"

This code is a bit of a mess, if you tidy it up you will see the error
Your code without all the <?php and ?>
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
}
echo $row['car_make']
else :
echo '<h1> Not working </h1>';
endif;
?>
So you should see the } in the wrong place ending the foreach loop, quite easily now, so change it to
<?php
$count = 1;
if(isset($car)) :
foreach($car as $row) {
$count++;
echo $row['car_make'];
} //<-- moved this line
else :
echo '<h1> Not working </h1>';
endif;
?>
As you are getting the Not Working message I have to assume there is a problem with your querying of the database.
Probably as you are not passing the search term into the car_search method call. So try passing the paramter you get from the POSTed data.
public function index(){
$data = array();
if($query = $this->model_home->car_search($this->input->post('search'))) {
$data['car'] = $query;
}
$this->load->view('search_results', $data);
}
You should also look at the default value used in the car_search method as the string 'default' will almost definitely cause the query to return no results.

Related

Display an array data from two tables in codeigniter

I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}

how to get the data from array when multiple model functions pass to the view from the controller

I have a controller it take multiple model function results and pass it to controller I did it like this
adpreview_ctrl.php
public function showBusinessReviews($vehicleid){
$data=array();
$data['details']=$this->ads_model->getBusinessReviews($vehicleid);
$data['noOfReviews']=$this->ads_model->countReviews($vehicleid);
$this->load->view('pages/templates/header');
$this->load->view('pages/viewReviews',$data);
$this->load->view('pages/templates/footer');
}
public function countBusinessReviews($vehicleid){
$data['details']=$this->ads_model->countReviews($vehicleid);
$this->load->view('pages/templates/header');
$this->load->view('pages/viewReviews',$data);
$this->load->view('pages/templates/footer');
}
}
viewReviews.php
<?php
foreach($noOfReviews as $reviewAmount){
echo $reviewAmount.'Reviews';
}
foreach($details as $review){
$Breview=$review->rating;
if($details==null)
{?>
<?php echo '<center><b><h3>No any reviews has been posted yet!</h3></b></center>';?>
<input type="submit" name="ok" class="btn btn-primary btn-lg" value="ok">
<?php }
else{
?>
Ads_model.php
public function getBusinessReviews($Vehicleid){
$status="Approved";
$this->db->select("*");
$query=$this->db->where('Vehicleid',$Vehicleid);
$query=$this->db->where('Status',$status);
$query=$this->db->get('businessreviews');
return $query->result();
}
public function countReviews($Vehicleid){
$status="Approved";
$this->db->select("*");
$query=$this->db->where('Vehicleid',$Vehicleid);
$query=$this->db->where('Status',$status);
$query=$this->db->get('businessreviews');
return $query->num_rows();
}
what I need to know is, it gives error saying it cannot identify $noOfReviews.
foreach($noOfReviews as $reviewAmount){
echo $reviewAmount.'Reviews';
}
I need to know how to retrieve multiple model function data in view,
and $noOfReviews only gives the no of reviews, a user has given. So their, without using a foreach loop how can i get the value of that, using a foreach loop is not necessary here.
Try this:
model:
public function countReviews($Vehicleid) {
$status = "Approved";
$this->db->select("*");
$query = $this->db->where('Vehicleid', $Vehicleid);
$query = $this->db->where('Status', $status);
// it will return with only the number of data
return $query->count_all_results('businessreviews');
}
viewReviews.php
<?php
// show number of reviews
echo $noOfReviews . 'Reviews';
// show details if exists
if(!empty($details)) {
foreach($details as $review) {
// echo what you want
}
} else {
echo 'No details.';
}
?>

How to access multiple values to code igniter view

Passing multiple values as an array is working. But when i want access the variable only 1st array variable can access.
I think the problem comes from model. when i used $this->db->select('*'); no issue occur. Why it happen? Then how to access other variable.
Controller
public function index() {
$this->load->model('prop_model');
$pro_data['pro'] = $this->prop_model->get_data_all();
$this->load->view('home/main_view', $pro_data);
}
Model (prop_model)
function get_data_all() {
$this->db->select('prop_id', 'content', 'added_date');
$query = $this->db->get('tble_prol');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
view
<div class="col-md-9">
<?php
foreach ($pro as $add) {
echo '<div class="grid-item well"><p>'
. $add->content .'<br>' . $add->added_date //error here when access added_date
. '</p></div>';
}
?>
</div>
Try this:
$this->db->select('prop_id,content,added_date');
The query syntax is wrong. See for the reference: ActiveRecordsSyntax

Displaying data from database using Codeigniter

I am trying to get data from a database and display it using a model, controller, and view.
Here is my model
public function waitlist_view() {
$data = array();
$this->load->database();
$this->db->select('*');
$this->db->from('waitlist');
$query = $this->db->get();
return $query->row();
}
Here is my controller
public function waitlist() {
$data['title']="parents_viewlist";
//redirect if not logged in
if(($this->session->userdata('logged_in')!= 1) && ($this->session->userdata('type')!='parent')) {
redirect('login/index');
}
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
}
Here is my view
<div>
<?php echo $row->waitlist_id; ?>
<?php echo $row->ay_code; ?>
<?php echo $row->school_id; ?>
<?php echo $row->waitlist_status; ?>
</div>
It doesnt display anything on the page when I pull it up. Any help would be appreciated!
in your model use result() to get data :
public function waitlist_view() {
$this->load->database();
$query = $this->db->get('waitlist')->result();
return $query;
}
in your controller :
$this->load->model('parents_model');
$data['row'] = $this->parents_model->waitlist_view();
$this->load->view('templates/cpsheader', $data);
$this->load->view('templates/cpsmenu');
$this->load->view('parents/parents_viewlist', $data);
$this->load->view('templates/cpsfooter');
Now use loop on $row in your view to print data.
In your controller print the data returned from db as:
echo "<pre>";print_r($data);echo "</pre>";exit;
add this line just after $data['row'] = $this->parents_model->waitlist_view();
Let us know what result are you getting.

Populating <select> with mysql data dynamically using CodeIgniter

I'm stacked on this matter. I have a model that retrieves data from a mysql database
class load_model extends CI_Model{
function __construct(){
parent::__construct();
}
Function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
foreach ($records->result() as $row)
{
$data[$row->SupplierID] = $row->Name;
}
return ($data);
}
}
?>
This model submits value to a function in my controller
public function getSupplier()
{
$this->load->model('load_model');
$data['unit'] = $this->load_model->loadsuppliers();
$this->load->view('SupplierMGT', $data);
}
and I want to display the retrieved data to my view as a combo box. I tried to check if I am able to retrieve database values using echo json_encode($data) and it returns {"unit":{"2":"test","3":"Delta"}} ,
Could you help me with this? I tried using
<?php foreach($unit as $result):
print_r($result);
endforeach;?>
to check if i am able to pass the value but i failed.
Small changes in the model:
function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
if($records->num_rows() > 0){
$data = $records->result_array();
}
return ($data);
}
In your view SupplierMGT.php write this:
<select name="" id="" multiple="">
<?php
if(isset($unit) && is_array($unit) && count($unit) > 0){
foreach($unit as $key=>$each){
?>
<option value="<?=$each['SupplierID']?>"><?=$each['Name']?></option>
<?php
}
}
?>
</select>

Categories