Looping in array (php codeigniter) - php

The problem is looping in $t array on controller page
Any Solution?
Here's my code
Controller:
function save($id)
{
$i=0;
$t = array(
for($i;$i<=34;$i++)
'j'.$i => $this->input->post('j'.$i),
'status' => $this->input->post('1')
);
$this->mmeeting->save($id,$t);
redirect('admin/meeting','refresh');
}
Model:
function save($t){
$this->db->insert("meeting", $t);
return $this->db->insert_id();
}
View:
<?
$i=1;
foreach ($im as $row):
$i++;
?>
<input name="j.<? echo $i; ?>" type="hidden" value="<? echo $row['when'] ?">>
<? endforeach; ?>
<input type="submit" value="register" tabindex="7" />
Meeting table in database
CREATE TABLE IF NOT EXISTS `meeting`
(
`j1` date ,
`j2` date ,
....
`j34` date ,
status int(1),
)ENGINE=InnoDB DEFAULT CHARSET=armscii8;

This has nothing to do with codeigniter, just php. The following is not valid:
$t = array(
for($i;$i<=34;$i++)
'j'.$i => $this->input->post('j'.$i),
'status' => $this->input->post('1')
);
Use this instead:
$t = array('status' => $this->input->post('1'));
for ($i; $i <= 34; $i++) {
$t["j$i"] = $this->input->post("j$i");
}

If i understood correctly what you want
function save($id)
{
$i=0;
$t = array();
for($i;$i<=34;$i++)
$t["j$i"] = $this->input->post("j$i");
$t['status'] => $this->input->post('1');
$this->mmeeting->save($id,$t);
redirect('admin/meeting','refresh');
}

Related

how to update multiple value in codeigniter

I have input data that I will fill with different values, I do multiple updates
but I have an error which is:
Unknown column 'Array' in 'field list' UPDATE service SET
charges_order = WHERE array id_service = '1'
how to overcome this?
example img
View
<?php $no = 1; foreach ($invoice as $m) { ?>
<tbody id="tbody">
<form class="form-signin" method="post" action="<?php echo base_url();?>backend/report/update/<?php echo $m->id_service; ?>">
<tr class="deleted">
<td><input type="text" class="form-control" name="charges_order[]" value="<?php echo $m->charges_order;?>"></td>
</tr>
</form>
</tbody>
<div class="box-footer">
<button type="submit" class="btn bg-blue btns-flat margin">Simpan</button>
</div>
<?php } ?>
Controller
public function update($id_service)
{
foreach ($this->input->post('charges_order') as $data) {
$data = array(
'charges_order' => $this->input->post('charges_order')
);
// echo '<pre>', print_r($data);
$this->M_report->update($id_service, $data);
redirect('backend/report');
}
}
Model
public function update($id_service, $data)
{
$this->db->where('id_service', $id_service);
$this->db->update('service', $data);
}
Change your controller to this-
public function update($id_service){
$charges_order = json_encode($this->input->post('charges_order'));
$data = array(
'charges_order' => $charges_order
);
$this->M_report->update($id_service, $data);
redirect('backend/report');
}
This should work for you.
You are in a loop. You must use the variable.
$data = array(
'charges_order' => $data
);
You can convert array into a string and then update the table.
//example
[1,2,3,4] -> "1,2,3,4"
Here is the code
public function update($id_service)
{
$data = array(
'charges_order' => implode(",", $_POST['charges_order'])
);
// echo '<pre>', print_r($data);
$this->M_report->update($id_service, $data);
redirect('backend/report');
}
When you are retrieving, you can reverse the process
public function get_orders($id_service)
{
$this->db->select('charges_order');
$this->db->where('id_service', $id_service);
$result = $this->db->get('service')->result_array();
return explode(",", $result["charges_order"]); //returns an array
}

How to combine two SQL tables in codeigniter without using SQL queries?

Here is my codes,
//FROM MY CONTROLLER
$companies = $this->Uploads_model->getallcompanies();
$general = $this->Uploads_model->getallcontract();
$this->data['companieslist'] = $companies;
$this->data['uploads'] = $general;
$this->render('contracts/index_view');
// MY VIEW
foreach($companieslist as $company){
$general_c[] = $company->company_id;
$general_c[] = $company->company_name;
}
foreach ( $uploads as $key => $con ) {
?>
<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace"/>
<span class="lbl"></span>
</label>
</td>
<td>
<a href="#">
<?php
if($con->Company_id == $general_c[$company_id]){ // MY QUESTION IS HERE
echo $con->Company_name;
}
?>
</a>
</td>
So, am working on this web app where am supposed to display in table all contract files followed with company owned this contract.
I have two tables in my Database as follow Companies_tbl and contract_tbl.
I have Company_id as a foreign key in contract_tbl.
Create a custom function to filter your contracts based on Company_id
function getContractsForId($contracts,$Company_id) {
return array_filter($contracts,function(){
return $contracts->Company_id === $Company_id;
});
}
template loop:
foreach( getContractsForId($uploads,$Company_id) as $key => $con) {
//...html...
//remove the if
}
You are not building the array of companies the right way.
foreach($companieslist as $company){
$general_c[] = $company->company_id;
$general_c[] = $company->company_name;
}
After you looped once, the $general_c array will look like this:
$general_c = [
0 => 1, //company id
1 => 'Company name'
]
After the second time:
$general_c = [
0 => 1, //company id
1 => 'Company name',
2 => 2, //company id
3 => 'Company name 2'
]
What you want is the following:
foreach($companieslist as $company){
$general_c[$company->company_id] = $company->company_name;
}
And below:
<?php
if($general_c[$con->Company_id]) {
echo $general_c[$con->Company_id];
} ?>
This will let you output the company name based on ID.
But i suggest you do a MySQL join in your model. Add something like this to your Uploads_model:
function getContractsWithCompany() {
$query = $this->db->select('*')
->from('contract_tbl')
->join('Companies_tbl', 'Companies_tbl.id = contract_tbl.Company_id')
->get();
return $query->result();
}
You need first to group files by Company_id then list the specific company files
$companies = $this->Uploads_model->getallcompanies();
$general = $this->Uploads_model->getallcontract();
$this->data['companieslist'] = $companies;
$this->data['uploads'] = array();
//group file list by Company_id
foreach ($general as $key => $file) {
if(!isset($this->data['uploads'][$file->Company_id ]))
$this->data['uploads'][$file->Company_id] = array();
$this->data['uploads'][$file->Company_id][] = $file;
}
// in your view
foreach($companieslist as $company){
$general_c[] = $company->company_id;
$general_c[] = $company->company_name;
if(isset($uploads[$company->company_id])) {
foreach ( $uploads[$company->company_id] as $key => $con ) {
?>
<tr>
<td class="center">
<label class="pos-rel">
<input type="checkbox" class="ace"/>
<span class="lbl"></span>
</label>
</td>
<td>
<?=$company->company_name?>
</td>
</tr>
<!-- and the rest of logic -->
<?php
}
}
}

codeigniter array and loop to insert for multiple data

I want to make an invoice form. For this reason i need to insert multiple data at once. for example I want to input db 5 category sale or purchase product at a time. But I am not interested with insert_batch. I try but i got some null value in db.
My Model is:
<?php
class Purchase_model extends CI_Model{
public function purchase(){
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$date = $this->input->post('date');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
$invoice_no = $this->input->post('invoice');
//$temp = count($this->input->post('vendor_name'));
for($i=0; $i<10; $i++){
$data = array(
'date'=>$date[$i],
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no[$i]
);
$insert = $this->db->insert('purchase',$data);
return $insert; }
}}
My controller is:
public function purchase(){
if($this->Purchase_model->purchase()){
$this->session->set_flashdata('Success',
'You are entered data successfully');
redirect('home/purchase_form');
}
}
My view for example:
<?php echo form_label ('Price:'); ?>
<select name="price[]" id="price" class="input-xlarge">
</select>
<?php echo form_label ('Quantity'); ?>
<?php
$data = array ('name' =>'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')); ?>
<?php echo form_input ($data); ?>
<?php echo form_label ('Price:'); ?>
<select name="price[]" id="price2" class="input-xlarge">
</select>
<?php echo form_label ('Quantity'); ?>
<?php
$data = array ('name' =>'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')); ?>
<?php echo form_input ($data); ?>
Please help.
The problem has been solved. My mistake in my model. The correct model is
public function purchase(){
$data = array();
$temp = count($this->input->post('quantity'));
for($i=0; $i<$temp; $i++){
$invoice_no = $this->input->post('invoice');
$date = $this->input->post('date');
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
if( $quantity[$i]!= '') {
$data[] = array(
'date'=>$date,
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no
);} }
$insert = count($data);
if($insert)
{
$this->db->insert_batch('purchase', $data);
}
return $insert;
}
Thanks every one who try to help me.

Codeigniter/PHP How to properly use a for loop?

<?php
print_r($optimum);
$dataNumRows = count($optimum);
?>
<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
<?php echo $cFirstName; ?>
<?php echo $cLastName; ?>
<?php endfor; ?>
My print_r inserted in my VIEW shows the following:
Array ( [cFirstName] => Array ( [0] => Tom [1] => Alexa ) [cLastName] => Array ( [0] => Jones [1] => Planter ) )
My MODEL is the following
//Get all the customers currently pending
//install for the user making the request.
function getAllCustomersPendingInstall()
{
$data=array();
//Need to use sessions to display proper
//records for each user. Temp set id to user #7
$id = 7;
//query the db and return all record where SalesRepId == $id
$query = $this->db->get_where('customers', array('SalesRepId' => $id));
//check logic, if rows exist RETURN all rows, else
//return message that no pending installs is available.
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data['cFirstName'][] = $row->customerFirstName;
$data['cLastName'] [] = $row->customerLastName;
}
} else {
$data = "No pending installs available!";
return $data;
}
//the following var_dump is only showing the last record.
//need to show all rows (which should be 2)
//var_dump($data); exit;
return $data;
}
My CONTROLLER is the following
{
$this->load->library('table');
$this->load->model('GetData');
$data['optimum'] = $this->GetData->getAllCustomersPendingInstall();
$this->load->view('welcome_message', $data);
}
And my question is how do I properly use the FOR loop in my VIEW so that I can loop through all the returned rows. As you can see the print_r is properly returning the proper rows- However I am unable to loop through them. Thanks for the help! Much appreciated!
Try this in your view:
<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
<?php echo $optimum['cFirstName'][$i]; ?>
<?php echo $optimum['cLastName'][$i]; ?>
<?php endfor; ?>
I think what you're trying to do is get an associative array for each row returned from the database. Correct me if I'm wrong about that.
Should fix your problem
$data = array();
$data_index = 0;
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data[$data_index]['cfirst'] = $row->customerFirstName;
$data[$data_index]['clast'] = $row->customerLastName;
$data_index++;
}
} else {
$data = "No pending installs available!";
return $data;
}
then in your view (where $customer is the $data array)
<?php foreach($customer as $c):?>
<?php echo $c['cfirst'];?>
<?php endforeach;?>

how to send array from controller to view

I have a array in controller I have to send it to view and then show the value
The Controller page code
public function showbookedmenu(){
$rowLength=$this->input->post('rowno');
$check=$this->input->post('checkeed');
$j=0;
for($i=1;$i<=$rowLength;$i++)
{
$check=$this->input->post('checkeed'.$i);
if($check== "checked")
{
$orderedItem[$j][$i] = $this->input->post('txtMenuItem'.$i);
$orderedItem[$j][$i+1] = $this->input->post('quantity'.$i);
$orderedItem[$j][$i+2] = $this->input->post('lebPrice'.$i);
$orderedItem[$j][$i+3]= $this->input->post('grandtotal'.$i);
$j++;
}
else{}
}
$data['order']= $orderedItem;
$this->load->view('orderpage', $data);
}
The view page
What will be the code??
First rewrite your controller action like this:
public function showbookedmenu()
{
$rowLength = $this->input->post('rowno');
$orderedItem = array();
for($i = 1; $i <= $rowLength; $i++)
{
$check = $this->input->post('checkeed'.$i);
if($check == "checked")
{
$orderedItem[] = array(
'menu_item' => $this->input->post('txtMenuItem'.$i),
'quantity' => $this->input->post('quantity'.$i),
'leb_price' => $this->input->post('lebPrice'.$i),
'grand_total' => $this->input->post('grandtotal'.$i)
);
}
}
$data['order'] = $orderedItem;
$this->load->view('orderpage', $data);
}
Then in your view you can do this:
<? foreach($order as $order_item): ?>
<? echo $order_item['menu_item']; ?>
<? echo $order_item['quantity']; ?>
<? echo $order_item['leb_price']; ?>
<? echo $order_item['grand_total']; ?>
<? endforeach; ?>
first try to print array value in your view file..
print_r($order);
if it is coming fine..
you can give it on
foreach ($order as $value):
and use it :

Categories