Multiple row print in table - codeigniter

I want to print multiple data from the database in one table, however, it shows one row.
Model code is below. Please look at my codes and give some suggestion on how I can solve this issue.
Model CODE
function show_list()
{
$query=$this->db->get('student');
return $query;
}
Here is my controller code below:
Controller Code
<?php
class Showlist extends CI_Controller {
function index()
{
//$this->load->view('showlist');
$this->load->model('main_model');
$data['no']=$this->main_model->show_list();
//return the data in view
$this->load->view('showlist', $data);
}
}
?>
Here is my view code below:
View Code
<style>
table
{
border-collapse: collapse;
}
table, th, td
{
border: 1px solid black;
}
</style>
<body>
<html>
<center>
<table>
<tr>
<th>NAME </th>
<th>Student ID</th>
<th>Address</th>
<th>Fathers Name</th>
<th>Mothers Name</th>
<th>Education</th>
</tr>
<?php
if($no->num_rows()> 0){
foreach ($no->result() as $row) {
?>
<tr>
<td><?php echo $row->Name; ?></td>
<td><?php echo $row->StudentID; ?></td>
<td><?php echo $row->Address; ?></td>
<td><?php echo $row->FatherName; ?></td>
<td><?php echo $row->MotherName; ?></td>
<td><?php echo $row->Eduation; ?></td>
<!-- <td>EDIT</td>
<td>Delete</td>-->
</tr>
<?php
}
?>
<tr>
<td><center><a href='add_new_student.php'>ADD NEW STUDENT</a></center></td>
<td><a href='logout.php'>Logout</a></center></td>
</tr>
<?php
}
else
{
echo 'NO data Found';
}
?>
</table>
</center>
</html>
</body>
It only prints one row:
There are more data in my database. I try result() but it is not printing the rows that I want.
THis is my table data 4 rows

result() display only one row:
Add this line in model code:
return $query->result_array();
And change the foreach loop :
foreach ($no as $row) {..}
Change the line to all the rows:
<td><?php echo $row->Name; ?></td> to
<td><?=$row['Name'];?><td>

Related

Paging in codeigniter doesn't work - could not be converted to string

I'm not able to insert the pager function inside the readHunters() method so that the records in my table are not all on the same page (I want to put 10 records per page).
I know that in app->Config->Pager.php there is public $perPage = 10; which helps to define the number of records in the table with pagination.
HunterController.php
public function readHunters()
{
try {
$hunter = new HunterModel();
$data['hunter'] = $hunter->findAll();
$data['pager'] = $this->$hunter->page;
return view('read_hunters', $data);
} catch (\Exception $e) {
exit($e->getMessage());
}
}
read_hunters.php
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Year</th>
<th>Height</th>
<th>Weight</th>
<th>Type hunter</th>
<th>Type nen</th>
<th>Type blood</th>
<th>Date registration</th>
<th>Date upgrade</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($hunter as $hxh): ?>
<tr>
<td><?= $hxh['id_hunter']?></td>
<td><?= $hxh['name_hunter']?></td>
<td><?= $hxh['year_hunter']?></td>
<td><?= $hxh['height_hunter']?> m</td>
<td><?= $hxh['weight_hunter']?> kg</td>
<td><?= $hxh['type_hunter']?></td>
<td><?= $hxh['type_nen']?></td>
<td><?= $hxh['type_blood']?></td>
<td><?= date('d/m/Y H:i:s', strtotime($hxh['date_registration']))?></td>
<td><?= $hxh['date_update'] == $hxh['date_registration'] ? '' : date('d/m/Y H:i:s', strtotime($hxh['date_update'])) ?></td>
<td>
"><i class="fa fa-edit"></i> Update
<i class="fa fa-trash"></i> Delete
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?= $pager->links(); ?>
To provide a paginated list in the app, your controller's method looks like this:
$data = [
'hunter' => $hunter->paginate(10),
'pager' => $hunter->pager,
];
return view('read_hunters', $data);

foreach variable not defined in laravel 5.2

I am new to laravel and want to populate my view using foreach .
This is view:
<thead>
<tr>
<th>Sr. No.</th>
<th>Name</th>
<th>Mobile No</th>
<th>Email ID</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($result as $res){?>
<tr>
<td>1</td>
<td><?php echo $res->vendor_name;?></td>
<td><?php echo $res->mobile;?></td>
<td><?php echo $res->email;?></td>
<td><?php echo $res->city;?></td>
<td>
<i class="fa fa-edit"></i>
<i class="fa fa-trash"></i>
</td>
</tr>
<?php }?>
Controller:
class VendorController extends Controller
{
public function getVendorList(){
$vendors=DB::table('vendor_master')->get();
$data['result']=$vendors;
$data['header'] = View::make('header')->render();
$data['footer'] = View::make('footer')->render();
return view('vendor_details', $data);
}
}
Route:
Route::controller('vendor','VendorController');
I am trying from quite sometime but not getting through.A liitle help will be appreciated.Thanks in advance
Data should be passed like this using compact() based on version it will little bit change read documentation here laravel documenation
class VendorController extends Controller
{
public function getVendorList(){
$vendors=DB::table('vendor_master')->get();
$result=$vendors;
$header = View::make('header')->render();
$footer = View::make('footer')->render();
return view('vendor_details',compact( 'result','header','footer'));
}
}
Note : your trying to apply codigniter data send format in laravel

How to retrieve imploded images path in database to view in code igniter

I am trying to save the multiple images path in database and upload images in root's upload/images[directory]. I have done it and its working. Images name are saved in database and and the image files are being uploaded. I just did save the images name by imploding. Now I need to explode that image in view. How can i do that? I have tried the following code in view and its not working.
<?php foreach ($products as $p): ?>
<tr>
<td><?php echo $p['id']; ?></td>
<td><?php echo $p['product_name']; ?></td>
<td><?php echo $p['product_price']; ?></td>
<td><?php echo $p['produce_description']; ?></td>
<!-- <td><?php echo $p['picture']; ?> </td> -->
<td><img src="<?php echo base_url('uploads/images/').explode('|',$p['picture']);?>" /> </td>
<td>
View |
Edit |
Delete
</td>
</tr>
It throws this error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/dashboard.php
Line Number: 47
and this is my line 47:
<td><img src="<?php echo base_url('uploads/images/').explode('|',$p['picture']);?>" /> </td>
Incase there is mistake in my image upload function, here is my code for it:
public function set_product($id=0)
{
#code
// if($this->input->post('userSubmit')){
$picture=array();
$count=count($_FILES['picture']['name']);
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
foreach($_FILES as $value)
{
for($s=0; $s<=$count-1; $s++)
{
$_FILES['picture']['name']=$value['name'][$s];
$_FILES['picture']['type'] = $value['type'][$s];
$_FILES['picture']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['picture']['error'] = $value['error'][$s];
$_FILES['picture']['size'] = $value['size'][$s];
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture[] = $uploadData['file_name'];
}
else{
$picture = '';
}
}
}
}//end of first if
else{
$picture = '';
}
$data=array(
'product_name'=>$this->input->post('product_name'),
'produce_description'=>$this->input->post('produce_description'),
'product_price'=>$this->input->post('product_price'),
'picture'=>implode('|',$picture)
);
if ($id==0)
{
return $this->db->insert('products',$data);
}
else {
$this->db->where('id',$id);
return $this->db->update('products',$data);
}
}
And this is my model function for getting data:
public function get_product()
{
#code
$query=$this->db->get('products');
return $query->result_array();
}
Any kind of help are highly appreciated. Thank you.
You should try something like this
<?php foreach ($products as $p): ?>
<?php
// explode images into a variable/array
$images=explode('|',$p['picture']);
?>
<tr>
<td><?php echo $p['id']; ?></td>
<td><?php echo $p['product_name']; ?></td>
<td><?php echo $p['product_price']; ?></td>
<td><?php echo $p['produce_description']; ?></td>
<!-- <td><?php echo $p['picture']; ?> </td> -->
<td><img src="<?php echo base_url('uploads/images/').$images[0];?>" /> </td>
<td>
View |
Edit |
Delete
</td>
</tr>
Edit
I will give you an example from a code in production at this link
<div class="aa-properties-details-img" style="margin-bottom: 25px;">
<?php
$property[0]['images']=explode(',',$property[0]['img']);
if(count($property[0]['images'])>0){
for($i=0;$i<count($property[0]['images']);$i++)
{ ?>
<img src="<?php echo base_url().'img/'.$property[0]['images'][$i]?>" alt="img">
<?php }
}else{
?>
<img src="<?php echo base_url().'img/no-image.jpg'?>" alt="img">
<?php
}
?>
</div>
Try this:
<?php foreach ($products as $p): ?>
<tr>
<td><?php echo $p['id']; ?></td>
<td><?php echo $p['product_name']; ?></td>
<td><?php echo $p['product_price']; ?></td>
<td><?php echo $p['produce_description']; ?></td>
<td>
<?php $images=explode('|',$p['picture']);
foreach($images as $image) {
?>
<img src="<?php echo base_url('uploads/images/').$image; ?>" width="50" height="50" />
<?php } ?>
</td>
<td>
View |
Edit |
Delete
</td>
</tr>

CodeIgniter delete row from database

I'm trying to delete data from my database
Here's my code:
VIEW
<table border="1" class="table">
<tr>
<th>No</th>
<th>Nama Depan</th>
<th>Nama Belakang</th>
<th>TTL</th>
<th>Email</th>
<th>Keterangan</th>
</tr>
<?php
$i = 0;
foreach ($dataMember as $result) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $result['namaDepan'];?></td>
<td><?php echo $result['namaBelakang']; ?></td>
<td><?php echo $result['TTL']; ?></td>
<td><?php echo $result['email']; ?></td>
<td>
<button>Delete</button>
</td>
</tr>
<?php $i++; } ?>
</table>
MODEL
public function Hapusdata($id){
$this->db->where('email', $id);
$this->db->delete('daftar');
}
Controller
public function hapusMember()
{
$this->load->model('Member');
$this->load->helper('url');
$id = $this->uri->segment(3);
$this->Member->Hapusdata($id);
redirect (site_url('Belajarberhadiah/halaman_admin'));
}
and the problem is i get
Severity: Notice
Message: Trying to get property of non-object
Filename: views/halaman_dMember.php
Line Number: 105
What should i do?
Please write href for delete tag as below:
delete
Also be sure that the column[email] you have used for where condition in controller is the correct one. Because column name is "email" in where condition and you are passing an integer value.

Creating table dynamically using codeigniter?

I want to show my search output in a table using Codeigniter. Suppose if user wants to search all the volunteers of his location then the result should be shown in a table containing volunteer name,conatct no etc. Any kind of help will be realy appreciable.
You can use the CI active record class:
http://www.codeigniter.com/userguide2/database/active_record.html#select
<table>
<tr>
<th>Name</th>
<th>contact</th>
</tr>
<?php
$query = $this->db->get_where('mytable', array('feild_to_search' => 'search_query'));
foreach ($query->result() as $row)
{
?>
<tr>
<td><?php echo $row->name ?></td>
<td><?php echo $row->contact ?></td>
</tr>
<?php
}
?>
</table>
:)

Resources