Select average from database codeigniter - php

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

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 } ?>

Table content does not showing correctly

I have an issue to show a content that not showing correctly:
my NAME is showing over and over again
my IKLAN (image) too
but my SIZE is not showing over and over again
so funny
like this the eror
This is my View:
data.php
<thead>
<tr>
<?php $i = 1;?>
<th>Nomer</th>
<th>Nama</th>
<th>Iklan</th>
<th>Ukuran</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$no = $this->uri->segment('3') + 1;
foreach ($list as $l) {?>
<td align="center"><h4><?php echo $i++; ?></h4></td>
<td align="center"><h4><span style="font-weight:bold"><?php echo $l->nama_client?></span></h4></td>
<td align="center"><h4><img src="./upload_iklan/<?php echo $l->iklan;?>" id="gambar_nodin" width="200"/></h4></td>
<td align="center"><h3><?php echo $l->size_ads?></h3></td>
this is my controller : Iklan.php
public function __construct()
{
parent::__construct();
$this->load->model('iklan_model');
$this->load->helper(array('form','url'));
$this->load->library('form_validation','upload','pagination');
$this->load->helper('url');
}
public function dashboard()
{
$data['konten'] = "dashboard";
$this->load->view('index', $data);
}
public function index($offset=0)
{
$this->load->database();
$data['list']=$this->iklan_model->show_iklan();
json_encode($data);
$this->load->library('pagination');
$config['base_url'] = base_url();
$config['total_rows'] = $data;
$config['per_page'] = 10;
$from = $this->uri->segment(3);
$this->pagination->initialize($config);
$data['list'] = $this->iklan_model->data($config['per_page'],$from);
$this->load->view('data',$data);
}
this is my model:
Iklan_model.php
public function __construct()
{
parent::__construct();
}
function data($number,$offset){
return $query = $this->db->get('client,size,advertisement',$number,$offset)->result();
}
function jumlah_data(){
return $this->db->get('data')->num_rows();
}
Loop must be start before tag so it will print each row with proper data.
<?php
$no = $this->uri->segment('3') + 1;
foreach ($list as $l) {?>
<tr>
<td align="center"><h4><?php echo $i++; ?></h4></td>
<td align="center"><h4><span style="font-weight:bold"><?php echo $l->nama_client?></span></h4></td>
<td align="center"><h4><img src="./upload_iklan/<?php echo $l->iklan;?>" id="gambar_nodin" width="200"/></h4></td>
<td align="center"><h3><?php echo $l->size_ads?></h3></td>
</tr>
<?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

Fetching the values from db before searching

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'] ) ) { ... }

search a database record using codeigniter

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

Categories