I have two arrays of values:
First array contain user id's:
Array ([0]=>1 [1]=>2 [2]=>3 [3]=>4 [4]=>5 [5]=>6)
Second array contain attendance status:
Array([0]=>Present [1]=>Absent [2]=>Absent [3]=>Present [4]=>Absent [5]=>Present)
I want to insert these values in the database in separate rows like this:
U_id Status
1 Present
2 Absent
3 Absent
4 Present
5 Absent
6 Present
Currently, I am using this code to insert values in database.
My Controller Code:
public function usr_att(){
if(isset($_POST['submit'])){
$uid = $_POST['uid'];
$status= $_POST['stat'];
$id = implode("," , $uid );
$status = implode("," , $status );
$data = array (
'u_id' => $id,
'status' => $status
);
$this->db->insert('attendence' , $data);
redirect("usr/usr_list", "refresh");
}
}
But this code inserts data like this:
U_id Status
1 Present,Absent,Absent,Present,Absent,Present
How can I insert these values in separate rows using CodeIgniter?
Simply you can do like this
public function usr_att()
{
if(isset($_POST['submit']))
{
$uid = $_POST['uid'];
$status = $_POST['stat'];
foreach ($uid as $key => $item)
{
$data = array (
'u_id' => $item,
'status' => $status[$key]
);
$this->db->insert('attendence' , $data);
}
redirect("usr/usr_list", "refresh");
}
}
For your intended purpose, once you have the values for $uid and $stat in an array, then you could do this:
<?php
// You have your u_id values in an array
$uid = array('1','2','3','4','5','6');
// You have your status values in an array
$stat = array('Present', 'Absent', 'Absent', 'Present', 'Absent', 'Present');
// As long as the number of u_id values and status values are the same
if( count($uid) == count($stat) )
{
// Use the count of uid values and loop through
for( $x = 0; $x <= count($uid) -1; $x++ )
{
// Entering each on in the database
$this->db->insert('attendence', array(
'u_id' => $uid[$x],
'status' => $stat[$x]
));
}
}
Related
I want to make function that update client.client_status data from given $id parameters from controller end function that fetched from booking.booking_id
Here my controller
function end($book_id = null) {
if (!empty($book_id)) {
// Booking Table
$table = 'booking';
$where = [
'book_id' => $book_id,
];
$data = [
'room_status' => 1,
];
$this->Model_Data->booking_update($table, $where, $data);
// Client Table
$table = 'client';
$client_id = $???????; // How to get booking.client_id from given id parameters
$where = [
'client_id' => $client_id,
];
$data = [
'room_status' => 1,
];
$this->Model_Data->booking_update($table, $where, $data);
$this->session->set_flashdata('book_ended', 'Book Ended');
redirect('book');
}
else {
$this->session->set_flashdata('book_end_error', 'Book End Error');
redirect('book');
}
}
Here my SQL Tables
According to your comment in Question , book_by_client_id is equal to client_id then for :
Accessing a single row:
(1) Result as an Object
$data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row();
$book_by_client_id =$data_result->book_by_client_id;
$client_id = $book_by_client_id; // client_id
(2) Result as an Array
$data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row_array();
$book_by_client_id =$data_result['book_by_client_id'];
$client_id = $book_by_client_id; // client_id
Note : for more info about row(); && row_array();
https://codeigniter.com/userguide3/database/results.html#result-rows
i have 1 million data using foreach.
ex table:
table
the data
data
i want to inserting that's data using batch/multipleinsert, but i have problem when i got duplicate data. if data duplicate i want the field amount will sum and update amount field with sum amount duplicated data.
this is my code before
<?php
foreach ($data_transaksi as $key)
{
if($key['STATUS_COA'] != $key['CHART_OF_ACCOUNT_STATUS'])
{
if($key['ACCOUNT_CATEGORY_CODE'] == '9')
{
$amount = round($key['AMOUNT'],2);
}
else
{
$amount = round($key['AMOUNT'],2) *-1;
}
}
else
{
if($key['ACCOUNT_CATEGORY_CODE'] == '9')
{
$amount = round($key['AMOUNT'],2)*-1;
}
else
{
$amount = round($key['AMOUNT'],2);
}
}
$dt_exsis = $this->ledger_model->cek_data_coa_exsis($key['COA_CODE'],$modul,$ID_USER);
if(empty($dt_exsis['id']))
{
//TRYINSERTINGBATCH
// $datainsert[] = '('.$key['COA_CODE'].','.$amount.','.$ID_USER.',"'.$modul.'")';
// $test = $key['COA_CODE'];
$datainput = array(
'COA_CODE' => $key['COA_CODE'],
'AMOUNT' => $amount,
'MODUL' => $modul,
'USER_ID' => $ID_USER
);
$this->ledger_model->save_rows_to_table($datainput,'finance_lapkue_temp');
}
else
{
$amount_fix = $amount + $dt_exsis['AMOUNT'];
$data=array(
'AMOUNT' => $amount_fix
);
$this->ledger_model->edit_rows_to_table_where($data,'finance_lapkue_temp','id',$dt_exsis['id']);
// $q = "UPDATE finance_lapkue_temp set AMOUNT = '$amount_fix' where id = '".$dt_exsis['id']."'";
// $this->db->query($q);
}
// $data_amount[$key['COA_CODE']] += $amount;
}
?>
if i using this code, the proccess so slow
Good option will to pass only data to DB that you want to insert. All the data cleaning task can be done in controller.
// Create a data array and add all info
$data = [];
//if user does not exist add it in array
if (empty($dt_exist($id))) {
$data[$ID_USER] = array(
'COA_CODE' => $key['COA_CODE'],
'AMOUNT' => $amount,
'MODUL' => $modul,
'USER_ID' => $ID_USER
);
}
else {
//if user exist in $data just modify the amount
if (!empty($data[$ID_USER])) {
$data[$ID_USER]['AMOUNT'] += $dt_exsis['AMOUNT'];
}
else {
// if user does not exist in data create add all info
$data[$dt_exsis['ID_USER']] = array(
'COA_CODE' => $dt_exsis['COA_CODE'],
'AMOUNT' => $dt_exsis['amount'],
'MODUL' => $dt_exsis['modul'],
'USER_ID' => $dt_exsis['ID_USER']
);
}
}
This will save multiple calls to DB and at the end you can pass $data and do multiple insert.
I have an array of two rows i need to satisfy two condition and based on that condition i should insert the value into the database.
Am getting the values using this query and am comparing $record['QTY'] & $record['FILLED'],the condition should be satisfied by each row in the database and when the condition satisfies it should be inserted or not inserted
In this case how can check for each row and satisfy the condition
How do i check the loop?
<?php
// Include confi.php
include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data
$employee_id = isset($_POST['EMPLOYEE_ID']) ? mysql_real_escape_string($_POST['EMPLOYEE_ID']) : "";
$mobile = isset($_POST['DRMOBILE']) ? mysql_real_escape_string($_POST['DRMOBILE']) : "";
$qty = isset($_POST['QTY']) ? mysql_real_escape_string($_POST['QTY']) : "";
$createdon = isset($_POST['CREATEDON']) ? mysql_real_escape_string($_POST['CREATEDON']) : "";
$status = isset($_POST['STATUS']) ? mysql_real_escape_string($_POST['STATUS']) : "";
$select="SELECT IFNULL(A.QTY,0)QTY,IFNULL(SUM(B.FILLED),0)FILLED,A.PHY_ID
FROM PHYSAMPLE A
LEFT JOIN FILLEDQTYS B ON A.DRMOBILE=B.DRMOBILE AND A.EMPLOYEE_ID=B.EMPLOYEE_ID AND A.PHY_ID=B.PHY_ID
WHERE A.DRMOBILE='$mobile' GROUP BY B.PHY_ID";
$query=mysql_query($select);
while($record=mysql_fetch_assoc($query)){
// print_r($record);
if($record['QTY'] < 0.7*($record['FILLED']) )
{
$json = array("status" => 2,"msg" => "Error In adding Data");
}else{
echo $sample="INSERT INTO `PHYSAMPLE` (EMPLOYEE_ID,DRMOBILE,QTY,CREATEDON)
VALUES('$employee_id','$mobile','$qty','$createdon')";
$qry=mysql_query($sample);
}
}
}else{
$json = array("status" => 0, "msg" => "Request method not accepted");
}
#mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json);
?>
Sample array
Array
(
[QTY] => 30
[FILLED] => 27
[PHY_ID] => 1
)
Array
(
[QTY] => 20
[FILLED] => 24
[PHY_ID] => 2
)
i am very new to code igniter /php .
Before i was using randomly generated invoice number like
$invoice_no = rand(9999,9999999999);
But now i wanted to increment invoice number and add current year as a prefix to it . But somewhere i am doing wrong as this code failed execute . Can some one point me in the right direction .
My model is ...
function insertInvoice($data)
{
$this->db->trans_begin();
$invoice = array();
if(!empty($data['client_id']))
{
$invoice['invoice_client_id'] = $data['client_id'];
}else{
$client_data = array(
'client_name' => $data['customername'],
'client_address1' => $data['address1']
);
$this->db->insert('client_details', $client_data);
$insert_id = $this->db->insert_id();
$invoice['invoice_client_id'] = $insert_id;
}
$query = $this->db->query("SELECT * FROM invoice ORDER BY invoice_id DESC LIMIT 1");
$result = $query->result_array(0);
$result ++;
$curYear = date('Y');
$invoice_no = $curYear . '-' .$result;
$invoice['invoice_no'] = $invoice_no;
$invoice['invoice_subtotal'] = $data['subTotal'];
$invoice['invoice_tax'] = $data['tax'];
$invoice['invoice_tax_amount'] = $data['taxAmount'];
$invoice['invoice_total'] = $data['totalAftertax'];
$invoice['invoice_total_extra'] = $data['totalextra'];
$invoice['invoice_rent'] = $data['rent'];
$invoice['invoice_paid'] = $data['amountPaid'];
$invoice['invoice_due'] = $data['amountDue'];
$invoice['invoice_desc'] = $data['notes'];
$invoice['invoice_items_count'] = $data['item_count'];
$invoice['invoice_extra_count'] = $data['extra_count'];
$invoice['invoice_miscellaneous'] = $data['miscellaneous'];
$this->db->insert('invoice', $invoice);
$i=1;
do {
$items = array(
'invoice_no' => $invoice_no,
'item_name' => $data['invoice']['product_name'][$i],
'item_price' => $data['invoice']['product_price'][$i],
'item_qty' => $data['invoice']['product_qty'][$i],
'item_total' => $data['invoice']['total'][$i],
'item_noof_crate_wait' => $data['invoice']['noof_crate_wait'][$i],
'item_crate_wait' => $data['invoice']['crate_wait'][$i],
'item_choot' => $data['invoice']['choot'][$i],
'item_net_quantity' => $data['invoice']['net_qty'][$i]
);
$this->db->insert('invoice_items',$items);
$i++;
} while($i<$data['item_count']);
$j=1;
do {
$extraitems = array(
'invoice_no' => $invoice_no,
'extra_item_name' => $data['extra']['name'][$j],
'extra_item_qunatity' => $data['extra']['qty'][$j],
'extra_item_price' => $data['extra']['price'][$j],
'extra_item_total' => $data['extra']['total'][$j]
);
$this->db->insert('extra_items',$extraitems);
$j++;
} while($j<$data['extra_count']);
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return FALSE;
}
else
{
$this->db->trans_commit();
return TRUE;
}
}
invoice_id is primary key in DB .
You're attempting to increment the result array but what you really need is to acquire and increment a field value.
//you only need one field so ask only for that
$query = $this->db->query("SELECT invoice_id FROM invoice ORDER BY invoice_id DESC LIMIT 1");
//you really should check to make sure $query is set
// before trying to get a value from it.
//You can add that yourself
//Asked for only one row, so only retrieve one row -> and its contents
$result = $query->row()->invoice_id;
$result ++;
...
I'm guessing you're getting an "Object conversion to String error" on line $invoice_no = $curYear . '-' .$result;
Since $result contains an object and you're using it as a string. Print the $result variable to check how to use the data assigned to it.
i am adding products to session as array but before adding that product to session how to check that product is present in session array or not.
If the product is present i want to increase the count of that product, if not add that product to session array. My php code is given below.
session_start();
if(empty( $_SESSION['fields1'] )) {
$_SESSION['fields1'] = array();
}
$qty = 1;
$id = $_POST['id'];
$name = $_POST['name'];
$description = $_POST['description'];
$cnt = 0;
print_r($_SESSION['fields1']);
if (! empty($_SESSION['fields1'])){
foreach ($_SESSION['fields1'] as $key=>$val){
if ($id == $val['id']){
$qty = $val['qty']++;
//echo "qty ===".$qty;
$_SESSION['fields1'][$cnt]['qty'] = $val['qty']++;
}
else
{
$arrayval = array('id' => $id,'name' => $name,'description' => $description,'qty' => $qty);
array_push($_SESSION['fields1'] ,$arrayval );
}
$cnt++;
}
}else{
$arrayval = array('id' => $id,'name' => $name,'description' => $description,'qty' => $qty);
array_push($_SESSION['fields1'] ,$arrayval );
}
//print_r($_SESSION['fields1']);
echo json_encode($_SESSION['fields1']);
If $id is ID of product and fields is array of products, try something like this:
if(empty($_SESSION['fields'][$id]))
$_SESSION['fields'][$id]['qty'] = 1;
else
$_SESSION['fields'][$id]['qty']++;