Print array in Codeigniter - codeigniter

Sorry if the questions seams stupid but I have created an array in my controller and I pass it to the view with the $data['sample'] array.
Here is my MYSQL Query:
SELECT workplace.id, workplace.type,
MAX(IF(workplace.region_id = 1, workplace.name,0)) 'Dhaka South',
MAX(IF(workplace.region_id = 2, workplace.name,0)) 'Dhaka North',
MAX(IF(workplace.region_id = 3, workplace.name,0)) 'Savar',
MAX(IF(workplace.region_id = 4, workplace.name,0)) 'Narayangonjh',
MAX(IF(workplace.region_id = 5, workplace.name,0)) 'Mymensingh',
MAX(IF(workplace.region_id = 6, workplace.name,0)) 'Barisal',
MAX(IF(workplace.region_id = 7, workplace.name,0)) 'Faridpur',
MAX(IF(workplace.region_id = 8, workplace.name,0)) 'Jessore'
FROM workplace
WHERE workplace.type='area'
GROUP BY workplace.id
If f print_r the sample it looks like
SELECT workplace.id, workplace.type, MAX(IF(workplace.region_id = 1, workplace.name,0)) 'Dhaka South', MAX(IF(workplace.region_id = 2, workplace.name,0)) 'Dhaka North', MAX(IF(workplace.region_id = 3, workplace.name,0)) 'Savar', MAX(IF(workplace.region_id = 4, workplace.name,0)) 'Narayangonjh', WHERE workplace.type='area' GROUP BY workplace.id
Array
(
[0] => stdClass Object
(
[id] => 15
[type] => area
[Dhaka South] => Jatrabari
[Dhaka North] => 0
[Savar] => 0
[Narayangonjh] => 0
[Mymensingh] => 0
[Barisal] => 0
[Faridpur] => 0
[Jessore] => 0
)
[1] => stdClass Object
(
[id] => 16
[type] => area
[Dhaka South] => Lalbag
[Dhaka North] => 0
[Savar] => 0
[Narayangonjh] => 0
[Mymensingh] => 0
[Barisal] => 0
[Faridpur] => 0
[Jessore] => 0
)
[2] => stdClass Object
(
[id] => 17
[type] => area
[Dhaka South] => Dhanmondi
[Dhaka North] => 0
[Savar] => 0
[Narayangonjh] => 0
[Mymensingh] => 0
[Barisal] => 0
[Faridpur] => 0
[Jessore] => 0
)
[3] => stdClass Object
(
[id] => 18
[type] => area
[Dhaka South] => 0
[Dhaka North] => Gulshan
[Savar] => 0
[Narayangonjh] => 0
[Mymensingh] => 0
[Barisal] => 0
[Faridpur] => 0
[Jessore] => 0
)
I want to use these data in a datatable where id,type,Dhaka South,Dhaka North,Savar,Narayangonjh,Mymensingh,Barisal,Faridpur,Jessore will be in thead as heading and values in tbody as value.
Please help if you can.

May this help you
controller
$data['persons'] = '' // obj your query result
view
<?php
$arrKey = array();
foreach ($persons[0] as $key => $person):
$arrKey[] = $key;
endforeach ?>
<table>
<thead>
<tr>
<?php foreach ($arrKey as $akey): ?>
<td><?php echo $akey; ?></td>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($persons as $key => $person): ?>
<tr>
<?php foreach ($arrKey as $akey): ?>
<td><?php echo $person->$akey; ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>

Do like this
$data['sample'] = $your_array;
//Getting column names here
foreach ($data['sample'] as $key){
$data['column_names'] = array_keys((array) $key);
break;
}
$this->load->view('file', $data);
View
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr><!-- Showing column names here -->
<?php foreach($column_names as $column){
echo $column;
?>
</tr>
</thead>
<tbody>
<?php foreach($sample as $row){?>
<tr>
<td><?= $row->id ?> </td>
<td><?= $row->type ?> </td>
<td><?= $row->Dhaka Soutd ?> </td>
<td><?= $row->Dhaka Nortd ?> </td>
<td><?= $row->Savar ?> </td>
<td><?= $row->Narayangonjh ?> </td>
<td><?= $row->Mymensingh ?> </td>
<td><?= $row->Barisal ?> </td>
<td><?= $row->Faridpur ?> </td>
<td><?= $row->Jessore ?> </td>
</tr>
<?php } ?>
</tbody>

Related

Saving images to db from dynamically added file uploader, Laravel

I have a table in which I am adding rows dynamically using JS which works fine, I am able to add those text fields to db as well using for loop, now one of the row has to be an image uploader, by current code I am only able to save one image, the rest images from different rows doesnt save, please help. Thank You.
blade file:
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Tags</th>
<th>Image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="button" class="btn-link cursor-pointer" value="Add Menu Item" href="javascript:void(0);">
<input type="hidden" class="tbl_row" name="count_row[]" id="count_row[]" value="1"/>
</td>
<td>
{{ Form::text('item_name[]', null, ['id' => 'name', 'class' => 'form-control'] ) }}
</td>
<td>
{{ Form::text('price[]', null, ['id' => 'price', 'class' => 'form-control'] )}}
</td>
<td>
{{ Form::select('tags[]', $tags, null, ['class' => 'form-control']) }}
</td>
<td>
<input type="file" name="image[]">
</td>
<td>
{{ Form::textarea('description[]', null, ['id' => 'description', 'rows' => 2, 'class' => 'form-control']) }}
</td>
</tr>
</tbody>
</table>
Controller:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$i]) && is_file($attachment[$i])) {
$fileNameWithExt = $attachment[$i]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$i]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . $extention;
// Upload Image
$path = $attachment[$i]->storeAs('public/content-images/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => $fileNameToStore[$ln], **even the name is not saving properly in db**
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}
Thanks to azeós's comment managed to solve by changing the code to this, had to change $i to $ln and fileNameToStore[$ln] to fileNameToStore in order to save proper filename to db:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$ln]) && is_file($attachment[$ln])) {
$fileNameWithExt = $attachment[$ln]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$ln]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . '.' . $extention;
// Upload Image
$path = $attachment[$ln]->storeAs('public/menu/'.$category->id.'/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => 'menu/' . $category->id . '/' . $fileNameToStore,
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}

Array to string conversion (searching in codeigniter)

I have problem with searching in my script. Problem is that i want to create filter. Filter is dropdown menu and i am entering data from this dropdown menu to array. I think this is problem but idk how to enter it different way.
Or how to pass data from my model as array and not as obj. (I think it is object)
My search:
<?php echo form_open("otk/triedit", 'class="form-inline"'); ?>
<th scope="col"></th>
<th scope="col">ČÍSLO ZÁKAZKY</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'p'], $pozicia); ?>POZÍCIA</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 's'], $stav); ?>STAV</th>
<th scope="col">PORADOVÉ ČÍSLO</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'st'], $technologia); ?>TECHNOLÓGIA</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'd'], $datum); ?>DÁTUM</th>
<th scope="col">DOKUMENT</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'z'], $zariadenie); ?>ZARIADENIE</th>
<th scope="col">OPERÁTOR</th>
<th scope="col"><center></center></th>
<?php echo form_submit(['type' => 'submit', 'class' => 'btn btn-warning', 'value' => 'Hľadať']); ?>
<?php echo form_close(); ?>
My Controller:
function triedit()
{
$this->load->model('base_model');
$udaje = array(
'hladat' => $this->input->post('p'),
'hladat' => $this->input->post('s'),
'hladat' => $this->input->post('st'),
'hladat' => $this->input->post('d'),
'hladat' => $this->input->post('z')
);
$keyword = $udaje;
$data['resulte'] = $this->base_model->triedit($keyword);
$this->load->view('base_triedit_view',$data);
}
My model:
function triedit($keyword)
{
$this->db->select('*');
$this->db->from('otk');
$this->db->like('ckod_otk',$keyword);
$this->db->join('technologia', 'technologia.id_technologia = otk.technologia_otk', 'left');
$this->db->join('zariadenie', 'zariadenie.id_zariadenie = otk.zariadenie_otk', 'left');
$this->db->join('zamestnanci', 'zamestnanci.id_zamestnanci = otk.operator_otk', 'left');
$this->db->join('stav', 'stav.id_stav = otk.stav_otk', 'left');
$query = $this->db->get();
return $query->num_rows();
}
And my preview of my view:
<?php if(is_array($resulte)): ?>
<?php
foreach ($resulte as $row) {?>
<tr>
<td></td>
<td><?php echo $row->czakazky_otk; ?></td>
<td><?php echo $row->pozicia_otk; ?></td>
.....
<?php } ?>
<?php endif; ?>
as far as i understand your problem
Data options format for form_dropdown is always in an array there is no alternative if you use form_dropdown
If you want your model should return data as an array
Do it like this :
function triedit($keyword)
{
$this->db->select('*');
$this->db->from('otk');
$this->db->like('ckod_otk',$keyword);
$this->db->join('technologia', 'technologia.id_technologia = otk.technologia_otk', 'left');
$this->db->join('zariadenie', 'zariadenie.id_zariadenie = otk.zariadenie_otk', 'left');
$this->db->join('zamestnanci', 'zamestnanci.id_zamestnanci = otk.operator_otk', 'left');
$this->db->join('stav', 'stav.id_stav = otk.stav_otk', 'left');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
return $query->result_array();
}
}
So changes in the view :
<?php if(is_array($resulte)): ?>
<?php
foreach ($resulte as $row) {?>
<tr>
<td></td>
<td><?php echo $row['czakazky_otk']; ?></td>
<td><?php echo $row['pozicia_otk']; ?></td>
..............
<?php } ?>
<?php endif; ?>
For more : https://www.codeigniter.com/user_guide/database/results.html#result-arrays

dynamic row column in codeigniter

Simply I can fetch an array with foreach and create a table as:
<?php foreach ($trainee as $key => $value): ?>
<tr>
<td><?= $value->TraineeID ?></td>
</tr>
<?php endforeach?>
But when numbers of table column not fixed then I can not create the columns and its value.
data array:
Array(
[0] => stdClass Object
(
[TraineeID] => 30012
[Inv.1] => 720
[Inv.2] => 2100
[Inv.3] => 3540
[Inv.4] => 4920
[Inv.5] => 6300
[Inv.6] => 7800
[Inv.7] => 8700
)
[1] => stdClass Object
(
[TraineeID] => 30033
[Inv.1] => 720
[Inv.2] => 2100
[Inv.3] => 3540
[Inv.4] => 4920
[Inv.5] => 6300
[Inv.6] => 7800
[Inv.7] => 8700
)
[2] => stdClass Object
(
[TraineeID] => 30037
[Inv.1] => 720
[Inv.2] => 2100
[Inv.3] => 3540
[Inv.4] => 4920
[Inv.5] => 6300
[Inv.6] => 7800
[Inv.7] => 8700
)
[3] => stdClass Object
(
[TraineeID] => 30038
[Inv.1] => 720
[Inv.2] => 2100
[Inv.3] => 3540
[Inv.4] => 4920
[Inv.5] => 6300
[Inv.6] => 7800
[Inv.7] => 8700
)
)
desired Output like below:
TraineeID Inv.1 Inv.2 Inv.3 Inv.4 Inv.5 Inv.6 Inv.7
30012 720 2100 3540 4920 6300 7800 8700
30033 720 2100 3540 4920 6300 7800 8700
30037 720 2100 3540 4920 6300 7800 8700
30038 720 2100 3540 4920 6300 7800 8700
Try This
<?php
foreach ($trainee as $traine):
echo "<tr>";
$tds = get_object_vars($traine);
foreach ($tds as $property => $value) {
echo sprintf("<td>%s</td>",$traine->{$property});
}
echo "</tr> ";
endforeach;
?>
Its very simple. Use this
<?php foreach ($trainee as $key => $value): ?>
<tr>
<td><?= $value->TraineeID ?></td>
<td><?= $value->Inv.1 ?></td>
<td><?= $value->Inv.2 ?></td>
<td><?= $value->Inv.3 ?></td>
<td><?= $value->Inv.4 ?></td>
<td><?= $value->Inv.5 ?></td>
<td><?= $value->Inv.6 ?></td>
<td><?= $value->Inv.7 ?></td>
</tr>
<?php endforeach?>

ERROR, when deleting multiple rows in codeigniter

my table name is inbox (id, date, message). and here is my code,
in view : inbox.php
<?php $attribute = array('class' => 'check', 'id' => 'myform'); ?>
<?php echo form_open('messages/remove_checked', $attribute); ?>
<table class="table table-striped table-bordered tablesorter" id="mytable">
<tr><th width="5px" class=specalt ><input type="checkbox" id="select_all" name="select_all"/></th><th>date</th><th>Message</th></tr>
<?php foreach($inbox as $row) : ?>
<tr>
<td width="5px" class=spec><input type="checkbox" name="cntact[]" class="check" value="<?php echo $row->ID; ?>"></td>
<td><? echo $row->Date; ?></td>
<td><? echo $row->Message; ?></td>
</tr>
<?php endforeach;?>
</table>
i add this one to check all the checkbox,
<script>
$(document).ready(function() {
$('#select_all').click(function() {
var c = this.checked;
$(':checkbox').prop('checked',c);
});
});
</script>
my controller : messages.php
function remove_checked()
{
//validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('cntact[]', 'Private Contact', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$remove = "No <strong>Data</strong> deleted!";
$this -> json_response(FALSE, $remove);
redirect("messages/inbox");
}
else //success
{
$checked_messages = $this->input->post('cntact'); //selected messages
$this->Mysms_model->delete_checked($checked_messages);
//redirect to
redirect("messages/inbox");
}
}
and my model : Mysms_model.php
function delete_checked($cntact)
{
$this->db->where_in('ID', $cntact)
->delete('inbox');
return $this->db->affected_rows() > 0;
}
the problem is, when i run this code, i found an error.
"DELETE FROM inbox WHERE ID IN ('on', 'on')".
so, what s on? it should number of ID, any answer?
thank you.
This is not meant to be an answer to the question, however, I just wanted to show you an alternative way to write your view using codeigniters built in libraries and helpers:
<?php
$this->load->helper('form');
$this->load->library('table');
echo form_open('messages/remove_checked', array('class' => 'check', 'id' => 'myform'));
$this->table->set_template(array('table_open' => '<table class="table table-striped table-bordered tablesorter" id="mytable">'));
$this->table->set_heading(
array('width' => '5px', 'class' => 'specalt', 'data' => form_checkbox(array('id' => 'select_all', 'name' => 'select_all'))),
'date',
'message'
);
foreach ($inbox as $row) {
$this->table->add_row(
array('width' => '5px', 'class' => 'spec', 'data' => form_checkbox(array('class' => 'check', 'name' => 'cntact[]', 'value' => $row->ID))),
$row->Date,
$row->Message
);
}
echo $this->table->generate();
echo form_close();

Invalid argument supplied for foreach() in codeigniter (tiara)

i get error in the CI view page, as Invalid argument supplied for foreach().
this is my code :
my database:
(table of Penyewa)
**id_penyewa
nama_penyewa
alamat
no_telp**
(table of Jaminan)
**id_penyewa
jenis_jaminan
ket_jaminan**
in Controller (penyewa.php)
function Penyewa()
{
parent::Controller();
$this->load->database();
$this->load->model('model_tampil');
}
function tambah()
{
$ck=$this->input->post('id_penyewa');
if($ck!='')
{
$this->model_tampil->insertPenyewa();
}
$this->load->view('tampilpenyewa');
}
function sukses()
{
echo "Data berhasil di input!";
?>
<br />
Tambah Data
<br />
Lihat Data
<?php
}
in Model (model_tampil.php)
function getPenyewa(){
$this->db->select('*');
$this->db->from('penyewa');
$this->db->join('jaminan','jaminan.id_penyewa = penyewa.id_penyewa');
$q = $this->db->get();
$rows = $q->num_rows();
$q_result = $q->result();
if($rows>0){
foreach($q_result as $row){
$data[] = $row;
}
return $data;
}
}
function insertPenyewa()
{
/*$this->db->trans_start();
$this->db->query('INSERT INTO penyewa VALUES($id_penyewa, $nama_penyewa, $alamat, $no_telp, $jenis_jaminan)');
$table1_id = $this->db->insert_id();
$this->db->query('INSERT INTO jaminan VALUES(id_penyewa,' . $table1_id .',jenis_jaminan)');
$this->db->trans_complete(); */
$this->id_penyewa=$this->input->post('id_penyewa');
$this->nama_penyewa=$this->input->post('nama_penyewa');
$this->alamat=$this->input->post('alamat');
$this->no_telp=$this->input->post('no_telp');
$this->jenis_jaminan=$this->input->post('jenis_jaminan');
$this->db->insert('penyewa',$this);
redirect('penyewa/sukses');
}
in views (tampilPenyewa.php)
<center>
<h3>Tabel data Penyewa</h3>
<table border="1">
<tr align="center" bgcolor="#33CC99">
<td width="100">ID Penyewa</td>
<td width="200">Nama Penyewa</td>
<td width="120">Alamat</td>
<td width="150">Nomor Telepon</td>
<td width="150">Jenis Jaminan</td>
<td>Tindakan Lanjut</td>
</tr>
<?php foreach ($records as $row) : ?>
<tr height="35">
<td> <?php echo $row->id_penyewa; ?></td>
<td> <?php echo $row->nama_penyewa; ?></td>
<td> <?php echo $row->alamat; ?></td>
<td> <?php echo $row->no_telp; ?></td>
<td> <?php echo $row->ket_jaminan; ?></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
<br />
Tambah Data
and in views too (inputpenyewa.php)
<center>
<?php
$this->load->library('validation');
$id_penyewa=array(
'name' => 'id_penyewa',
'id' => 'id_penyewa',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$nama_penyewa=array(
'name' => 'nama_penyewa',
'id' => 'nama_penyewa',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$alamat=array(
'name' => 'alamat',
'id' => 'alamat',
'value' => '',
'maxlength' => '500',
'size' => '',
'validation' => "required");
$no_telp=array(
'name' => 'no_telp',
'id' => 'no_telp',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$ket_jaminan=array(
'name' => 'jenis_jaminan',
'id' => 'jenis_jaminan',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$this->load->helper('form');
echo validation_errors();
echo form_open('penyewa/tambah');
echo '<center><h3>Input Data Penyewa</h3></center>';
echo "<table border='0' class='tabledetail' align='center'>";
echo
"<tr>"."<td>".form_label('ID')."</td>"."<td>".form_input('id_penyewa')."</td>"."</tr>";
echo
"<tr height=50>"."<td>".form_label('Nama Penyewa')."</td>"."<td>".form_input('nama_penyewa')."</td>"."</tr>";
echo
"<tr height=220>"."<td>".form_label('Alamat')."</td>"."<td>".form_textarea('alamat')."</td>"."</tr>";
echo
"<tr>"."<td>".form_label('No Telp')."</td>"."<td>".form_input('no_telp')."</td>"."</tr>";
echo
"<tr height=220>"."<td>".form_label('Jaminan')."</td>"."<td>".form_textarea('jenis_jaminan')."</td>"."</tr>";
echo
"<tr height=50>"."<td colspan=2 align='center'>".form_submit('mysubmit','Simpan')."</td>"."</tr>";
echo "</table>";
echo form_close();
?>
Lihat Data
okay, i am sorry if my question is very much and much..
thank you :)
update:
okey, i've doing that, but nothing result, my page error again..
and, i want to ask again, any mistake in model_tampil->getPenyewa() ??
this is the script :
function getPenyewa(){
$this->db->select('*');
$this->db->from('penyewa');
$this->db->join('jaminan','jaminan.id_penyewa = penyewa.id_penyewa');
$q = $this->db->get();
$rows = $q->num_rows();
$q_result = $q->result();
if($rows>0){
foreach($q_result as $row){
$data[] = $row;
}
return $data;
}
}
i want to input data to 2 table in my localhost, from 1 page, inputpenyewa.php..
You are not passing any data to the view.
$this->load->view('tampilpenyewa');
change this line to this.
$this->load->view('tampilpenyewa',$data);
You have to get the values from db. So before this line ,get value from db as
$data['records'] = $this->model_tampil->getPenyewa();
and then load the view as above, like
$this->load->view('tampilpenyewa',$data);
Hope this helps
Regards
iijb
In the tambah function of your controller,you are directly calling the view file without giving any data to it..
Do it like this..
function tambah()
{
$ck=$this->input->post('id_penyewa');
if($ck!='')
{
$this->model_tampil->insertPenyewa();
}
$records = $this->model_tampil->getPenyewa(); // The array returned from your whatever model function
$this->load->view('tampilpenyewa',array('records' => $records));
}

Resources