Fetching the values from db before searching - php

In this below code when i execute it .It shows all the values from the database and when i search it is searching .But my expected result is when i execute it should not display data from the database and only when i search it should display the result.
Controller search1_site.php
<?php
class Search1_site extends ci_controller
{
function index()
{
$data = array();
$keyword = $this->input->post('keyword');
$data['results'] = $this->search1_model->search($keyword);
$this->load->view('result_view', $data);
}
}
?>
model search_model.php
<?php
class Search_model extends CI_Model
{
function search($keyword)
{
$this->db->like('course_code', $keyword);
$query = $this->db->get('coursemaster');
return $query->result();
}
}
?>
view result_view.php
<form action="<?php echo site_url('search1_site/index');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" id="opn" value = "Search" />
</form>
<table>
<tr>
<th>course_code</th>
<th>course name</th>
</tr>
<?php
foreach ($results as $row) {
?>
<tr>
<td><?php echo $row->course_code;?></td>
<td><?php echo $row->course_name;?></td>
</tr>
<?php
}
?>
</table>

In controller :
class Search1_site extends ci_controller
{
function index()
{
$data = array();
$keyword = $this->input->post('keyword');
if($keyword!=""){
$data['results'] = $this->search1_model->search($keyword);
}
$this->load->view('result_view', $data);
}
}
in view :
<?php
if($results){
foreach ($results as $row) {
?>
<tr>
<td><?php echo $row->course_code;?></td>
<td><?php echo $row->course_name;?></td>
</tr>
<?php
}
}else{
//Display somthing
}
?>

When your page loads the keyword in the following line in your controller is going blank and its searching all the records as your query might be like %%.
$keyword = $this->input->post('keyword');
First check if $keyword is not blank. If its not blank then search the data.

do something like:
if ( isset( $_POST['keyword'] ) ) { ... }

Related

How to display data in view of the CodeIgniter?

Unable to display the data in a CodeIgniter view. Need to print the builders_name in a CodeIgniter view. So far I have the following code:
Model :
public function get_builders()
{
$query1 = $this->db->select('name')->from('builders')->where("name LIKE 'a%'")->limit(3)->get();
$query2 = $this->db->select('name')->from('builders')->where("name LIKE 'b%'")->limit(3)->get();
$result1 = $query1->result();
$result2 = $query2->result();
return array($result1, $result2);
}
Controller:
public function index(){
$this->load->database();
$data['h']= $this->Builders_Model->get_builders();
$this->load->view('home', $data);
}
View:
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Don't like this try it
public function get_builders()
{
$query1 = $this->db->select('name')
->from('builders')
->like('name','a')
->or_like('name','b')
->limit(3)->get();
$result = $query1->result();
return $result;
}
views code
<?php foreach ($h as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Hope fully works . Thank You
What you pass to the view in Codeigniter is accessible by its name. Just <php echo $h; ?> and you should be good to go
you can use :
<?php
foreach($h as $row){?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
}?>
You have to use $h variable in foreach for notation :
<?php
<tr>
<td><?php echo $rowDetail->name; ?></td>
</tr>
}
?>
First this is you haven't loaded the Builder_modal in your controller try to do some thing like
$this->load->model('Builder_model');
after $this->load->database();
Next thing in your view you have to echo like
echo $h; or var_dump($h) if you have an array
It's really working
model
public function get_builders()
{
$this->db->select('name');
$this->db->from('builders');
$this->db->like('name','a');
$this->db->or_like('name','b');
$this->db->limit(3);
$query = $this->db->get();
return $query->result();
}
controller
public function index(){
$this->load->model('Builder_model');
$data= $this->Builders_Model->get_builders();
$this->load->view('home', ['data'=>$data]);
}
view
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>

Codeigniter anchor tag hyperlinked to a form in controller but the data shown outside the table

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,

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

Select average from database codeigniter

I am trying to select average from several column from my database in using codeigniter. But I only manage to show out the average of last column from my database. What is the problem here?
View
<!--$noise is the field form database!-->
<div id = "conclusion">
<fieldset>
<h1>Tourism Rating</h1><br>
<table cellpadding = "0" cellspacing="0" border="0"><div id = "rating">
<?php foreach ($post as $row): ?>
<tr>
<td>Overall Rating</td>
<td>:</td>
<td><?php echo $row->rating;?></td>
</tr>
<?php endforeach; ?>
<?php foreach ($post as $row): ?>
<tr>
<td>% Annoying Noise</td>
<td>:</td>
<td><?php echo $row->noise;?></td>
</tr>
<?php endforeach; ?>
<?php foreach ($post as $row): ?>
<tr>
<td>Air Quality</td>
<td>:</td>
<td><?php echo $row->air;?></td>
</tr>
<?php endforeach; ?>
<?php foreach ($post as $row): ?>
<tr>
<td>Water Quality</td>
<td>:</td>
<td><?php echo $row->water;?></td>
</tr>
<?php endforeach; ?>
<?php foreach ($post as $row): ?>
<tr>
<td>Facilities</td>
<td>:</td>
<td><?php echo $row->facility;?></td>
</tr>
<?php endforeach; ?>
<?php foreach ($post as $row): ?>
<tr>
<td>Friendliness</td>
<td>:</td>
<td><?php echo $row->friend;?></td>
</tr>
<?php endforeach; ?>
</table>
</fieldset>
</div>
Controller
<?php
class viewreview extends CI_Controller {
public function view($page = 'viewreview') //writereview page folder name
{
$this->load->model('viewreview_model');
$data['query'] = $this->viewreview_model->get_data();
$this->load->vars($data);
$data['post'] = $this->viewreview_model->tourismrating();
$this->load->vars($data);
$data['post'] = $this->viewreview_model->tourismnoise();
$this->load->vars($data);
$data['post'] = $this->viewreview_model->tourismair();
$this->load->vars($data);
$data['post'] = $this->viewreview_model->tourismwater();
$this->load->vars($data);
$data['post'] = $this->viewreview_model->tourismfacility();
$this->load->vars($data);
$data['post'] = $this->viewreview_model->tourismfriend();
$this->load->vars($data);
if ( ! file_exists('application/views/viewreview/'.$page.'.php')) //link
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = 'View Review';
//$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('templates/header', $data);
$this->load->view('viewreview/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
Model
<?php
class viewreview_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_data()
{
$match = $this->input->post('search');
$this->db->like('sitename',$match);
$this->db->or_like('titlereview',$match);
$this->db->or_like('yourreview',$match);
$this->db->or_like('suggestion',$match);
$query = $this->db->get('tourism'); //pass data to query
return $query->result();
}
public function tourismrating()
{
$match = $this->input->post('search');
$this->db->select('*');
$this->db->from('tourism');
$this->db->select_avg('rating');
$this->db->where('sitename', $match);
$post = $this->db->get();
return $post->result();
}
public function tourismnoise()
{
$match = $this->input->post('search');
$this->db->select('*');
$this->db->from('tourism');
$this->db->select_avg('noise');
$this->db->where('noise', $match);
$post = $this->db->get();
return $post->result();
}
public function tourismair()
{
$match = $this->input->post('search');
$this->db->select('*');
$this->db->from('tourism');
$this->db->select_avg('air');
$this->db->where('sitename', $match);
$post = $this->db->get();
return $post->result();
}
public function tourismwater()
{
$match = $this->input->post('search');
$this->db->select('*');
$this->db->from('tourism');
$this->db->select_avg('water');
$this->db->where('sitename', $match);
$post = $this->db->get();
return $post->result();
}
public function tourismfacility()
{
$match = $this->input->post('search');
$this->db->select('*');
$this->db->from('tourism');
$this->db->select_avg('facility');
$this->db->where('sitename', $match);
$post = $this->db->get();
return $post->result();
}
public function tourismfriend()
{
$match = $this->input->post('search');
$this->db->select('*');
$this->db->from('tourism');
$this->db->select_avg('friend');
$this->db->where('sitename', $match);
$post = $this->db->get();
return $post->result();
}
}
?>

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