Creating table dynamically using codeigniter? - 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>
:)

Related

Multiple row print in table

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>

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

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.

Stop for loop repeating tables

I have a for loop inserting data into my table, works well.
Problem is each loop I wish for it to start a new row, but it starts a new table. any ideas how to fix this?
<?php if (isset($records)) : foreach ($records as $row) : ?>
<div id="mobile-content">
<table class="table table-hover">
<thead>
<tr>
<th>Account Name</th>
<th>Balance</th>
<th>Start Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row->bank_name; ?></td>
</tr>
<tr>
<td><?php echo $row->bank_balance; ?></td>
</tr>
<tr>
<td><?php echo $row->bank_start_date; ?></td>
</tr>
</tbody>
</table>
</div>
<?php endforeach; ?>
You should change the code to loop inside the table body, not in the whole table code:
<div id="mobile-content">
<table class="table table-hover">
<thead>
<tr>
<th>Account Name</th>
<th>Balance</th>
<th>Start Date</th>
</tr>
</thead>
<tbody>
<?php if (isset($records)) : foreach ($records as $row) : ?>
<tr>
<td><?php echo $row->bank_name; ?></td>
<td><?php echo $row->bank_balance; ?></td>
<td><?php echo $row->bank_start_date; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
Note: I've also remove extra <tr> because you need one row for the three cells.
I hope that helps :D

Two calendar form in one page Joomla

I implemented two calendar form in one view page in joomla component.
the code is like this:
<tr>
<td>Start Date</td>
<td><?php echo JHTML::calendar(date("Y-m-d"),'from', 'date', '%Y-%m-%d',array('size'=>'8','maxlength'=>'10','class'=>' validate[\'required\']',));
?></td>
</tr>
<tr>
<td>End Date</td>
<td><?php echo JHTML::calendar(date("Y-m-d"),'to', 'date', '%Y-%m-%d',array('size'=>'8','maxlength'=>'10','class'=>' validate[\'required\']',));
?></td>
</tr>
But now only the first calendar form would pop up a jQuery calendar that let me select a date, the second form, when clicking on it, there is no effect.
Hope someone could help me solve this problem.
You are giving same id for both calendar (Third parameter). it must be different ids for both calendar. try below code. Reference code.
<tr>
<td>Start Date</td>
<td><?php echo JHTML::calendar(date("Y-m-d"),'from', 'from_date', '%Y-%m-%d',array('size'=>'8','maxlength'=>'10','class'=>' validate[\'required\']',));
?></td>
</tr>
<tr>
<td>End Date</td>
<td><?php echo JHTML::calendar(date("Y-m-d"),'to', 'to_date', '%Y-%m-%d',array('size'=>'8','maxlength'=>'10','class'=>' validate[\'required\']',));
?></td>
</tr>

Resources