Table content does not showing correctly - php

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

Related

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 Pagination : Show Records from SQL Server

In codeigniter, I use sqlsrv and pagination library to show records from database,
but after i click on page 2 on pagination link, the Records will show at the end of current page instead of showing in new page, any help would be greatly appreciated,
here is the Controller:
public function example1() {
$config = array();
$config["base_url"] = base_url() . "index.php/posts/example1";
$config["total_rows"] = $this->PostModels->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config["num_links"] = 5;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->PostModels->
fetchdata($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
Here is the Model:
public function record_count() {
return $this->db->count_all("BlawBlaw");
}
public function fetchdata($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("BlawBlaw");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
& here is the View:
<table>
<tr>
<th><strong>Post Id</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
</tr>
<?php foreach($results as $data){?>
<tr>
<td><?php echo $data->a;?></td>
<td><?php echo $data->b;?></td>
<td><?php echo $data->c;?></td>
<td><?php echo $data->d;?></td>
</tr>
<?php }?>
<p><?php echo $links; ?></p>
</table>
you can used this this model and view changes make in your code
Here is the Model:
function fetch_data($limit, $id)
{
$this->db->select('*')->from('category ')->limit($limit, $id);
//$this->db->limit($limit);
//$this->db->where('Cid', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
& here is the View:
<table>
<tr>
<th><strong>Post Id</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
</tr>
<?php foreach($results as $data){?>
<tr>
<td><?php echo $data->a;?></td>
<td><?php echo $data->b;?></td>
<td><?php echo $data->c;?></td>
<td><?php echo $data->d;?></td>
</tr>
<?php }?>
<p><?php foreach ($links as $link) {
echo $link; ?></p>
</table>

javascript:void(0) on ajax pagination code igniter

I'm developing this pagination page with ajax on code igniter hmvc but I get this javascript:void(0) on the uri of my link to the next pages. how can I fix this?
here is my codes.
controllers
<?php
class Job_Titles extends MY_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Job_Titles_Model');
$this->load->library('Ajax_pagination');
$this->perPage = 10;
}
// VIEW REDIRECTING /////////////////////////////////////////////////////////
public function index(){
/// view ajax config/////
$data = array();
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//configuration
$config['first_link'] = 'First';
$config['div'] = 'postList'; //parent div tag id
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
$data['content_view'] = 'Job_Titles/jobtitles_read';
$data['content'] = $this->Job_Titles_Model->getRows(array('limit'=>$this->perPage));
$data['false'] = FALSE;
$this->templates->admin_template($data);
}
//// pagination
public function ajaxPaginationData(){
$data = array();
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//pagination config
$config['first_link'] = 'First';
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get post data
$data['content_view'] = 'Job_Titles/ajax-pagination-data';
$data['content'] = $this->Job_Titles_Model->getRows(array('start'=>$offset,'limit'=>$this->perPage));
$this->templates->admin_template($data);
}
}
models
public function getRows($params = array()){
$this->db->select('*');
$this->db->from($this->table);
if(array_key_exists("start", $params) && array_key_exists("limit", $params)){
$this->db->limit($params['limit'],$params['start']);
}elseif (!array_key_exists("start", $params) && array_key_exists("limit", $params)) {
$this->db->limit($params['limit']);
}
$query = $this->db->get();
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
views
<?php
echo anchor('Job_Titles/add_view','ADD ');
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div id = "container" class = "page-header">
<table class= "table table-bordered">
<thead>
<tr>
<th>JOB CODE</th>
<th>JOB NAME</th>
<th></th>
<th></th>
<tr>
</thead>
<tbody>
<?php if(!empty($content)){foreach ($content as $job_title) {?>
<tr>
<td><?php echo $job_title['JOB_CODE']; ?></td>
<td><?php echo $job_title['JOB_NAME']; ?></td>
<td></i></td>
<td><i class="fa fa-trash-o"></i></td>
</tr>
<?php }}else{ ?>
<li class="err_msg">Post(s) not available.</li>
<?php } ?>
</tbody>
</table>
<?php echo $this->ajax_pagination->create_links(); ?>
</div>
I'm developing this pagination page with ajax on code igniter hmvc but I get this javascript:void(0) on the uri of my link to the next pages. how can I fix this?

codeigniter pagination 404 not found

I am trying to use pagination with codeigniter 3 and bootstrap twitter. When i click the link pagination it's always give me 404 not found.
I think it's my fault for not fully understanding codeigniter URI that's why i need help in my code
Here is my model :
class Mcrud extends CI_Model {
public function record_count() {
return $this->db->count_all("crud");
}
function view() {
$ambil = $this->db->from('crud')->order_by('idcrud', 'DESC')->limit(5, 3)->get();
if ($ambil->num_rows() > 0) {
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
$ambil->free_result();
return $hasil;
}
}
here is my controller :
class Chome extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Mcrud');
}
function index() {
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['nama'] = $session_data['fullname'];
$data['username'] = $session_data['username'];
$data['id'] = $session_data['idlogin'];
$this->load->view('Header', $data);
$this->suratkeluar();
} else {
redirect('welcome', 'refresh');
}
}
function suratkeluar()
{
$result_per_page = 2; // the number of result per page
$config['base_url'] = base_url() . '/Chome/index';
$config['total_rows'] = $this->Mcrud->record_count();
$config['per_page'] = $result_per_page;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$data['data_get'] = $this->Mcrud->view();
$data['pagination'] = $this->pagination->create_links();
$this->load->view('Vhome', $data);
$this->load->view('Footer');
}
and this is my Vhome view :
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped">
<caption>List Data</caption>
<thead>
<tr>
<th width="80px">ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Address</th>
<th width="80px">Action</th>
</tr>
</thead>
<tbody>
<?php
if ($data_get == NULL) {
?>
<div class="alert alert-info" role="alert">Data masih kosong, tolong di isi!</div>
<?php
} else {
foreach ($data_get as $row) {
?>
<tr>
<td><?php echo $row->idcrud; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->age; ?></td>
<td><?php echo $row->address; ?></td>
<td>
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</td>
<?php
}
}
?>
</tr>
</tbody>
</table>
<?php echo $pagination; ?>
</div>
and last this is my base url in config.php :
$config['base_url'] = 'http://localhost/arsip/';
try to amend this section:
Model:
function view($opset) {
$ambil = $this->db->from('crud')->order_by('idcrud', 'DESC')->limit(5, $opset)->get();
if ($ambil->num_rows() > 0) {
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
$ambil->free_result();
return $hasil;
}
}
Controllers:
function suratkeluar($opset=NULL)
{
$result_per_page = 2; // the number of result per page
$config['base_url'] = base_url('Chome/suratkeluar');
$config['total_rows'] = $this->Mcrud->record_count();
$config['per_page'] = $result_per_page;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$data['data_get'] = $this->Mcrud->view($opset);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('Vhome', $data);
$this->load->view('Footer');
}
work for me :)

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

Categories