Error in getting all the array values using associative array method - php

now i am having a hard time in placing all my values in an associative array.
Because I can only get the last value from query. Not all the values. I can't spot the error.
Here's my code:
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE cscart_order_data.type = 'I'
AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
echo "<pre>";
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = array(
'id' => $row1['order_id'],
'total' => $row1['total'],
'status' => $row1['status'],
'data' => unserialize($row1['data_serialize']),
'date' => $row1['date']
);
}
$range = array();
foreach($var_data as $data){
$id = $data['id'];
$total = $data['total'];
$cost = $data['data']['cost'];
$var_date = $data['date'];
$status => $data['status'];
$date_var = date('Y-m-d H:i:s',$var_date);
$grand_total = $total + $cost;
$cost_ratio = ($cost/$grand_total) * 100;
$paid_ratio = ($total/$grand_total) * 100;
$range[] = $cost_ratio;
$test = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
}
echo "</table>";
print_r($test); //this will return the last index from my array
That;s my problem guys i hope you can help me. Thanks.

$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE cscart_order_data.type = 'I'
AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
echo "<pre>";
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = array(
'id' => $row1['order_id'],
'total' => $row1['total'],
'status' => $row1['status'],
'data' => unserialize($row1['data_serialize']),
'date' => $row1['date']
);
}
$range = array();
$test = array();
foreach($var_data as $data){
$id = $data['id'];
$total = $data['total'];
$cost = $data['data']['cost'];
$var_date = $data['date'];
$status => $data['status'];
$date_var = date('Y-m-d H:i:s',$var_date);
$grand_total = $total + $cost;
$cost_ratio = ($cost/$grand_total) * 100;
$paid_ratio = ($total/$grand_total) * 100;
$range[] = $cost_ratio;
$test[] = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
}
echo "</table>";
print_r($test); //this will return the last index from my array

This piece of code is loading a scalar value
$test = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
And should be
$test[] = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);

Related

Create json with php from all data from the database

I want to create json which contains all orders from datebase. I tried to write this code but it returns only one order.
$query = mysql_query("SELECT * FROM orders WHERE id_user = '".$userdata['user_login']."' ORDER BY `nom` ASC ");
if ($query)
{
$i = 0;
while ($row = mysql_fetch_assoc($query))
{
$where=$row["where"];
$time_min=$row["time_min"];
$time_max=$row["time_max"];
$date1=$row["date1"];
$date2=$row["date2"];
$from=$row["from"];
$id=$row["id"];
$orders =[
'from' => $from,
'where' => $where,
'time_min' => $time_min,
'time_max' => $time_max,
'date1' => $date1,
'date2' => $date2,
'id' => $id];
$i++;
}
}
$data = [
'count' =>$i,
'orders' => $orders
];
header('Content-type: application/json');
echo json_encode( $data );
exit;
Now the response looks like this:
But I want like this:
Append to the $orders array:
$query = mysql_query("SELECT * FROM orders WHERE id_user = '".$userdata['user_login']."' ORDER BY `nom` ASC ");
if ($query)
{
$i = 0;
while ($row = mysql_fetch_assoc($query))
{
$where=$row["where"];
$time_min=$row["time_min"];
$time_max=$row["time_max"];
$date1=$row["date1"];
$date2=$row["date2"];
$from=$row["from"];
$id=$row["id"];
$orders[] =[ // <--- The difference
'from' => $from,
'where' => $where,
'time_min' => $time_min,
'time_max' => $time_max,
'date1' => $date1,
'date2' => $date2,
'id' => $id];
$i++;
}
}
$data = [
'count' =>$i,
'orders' => $orders
];
header('Content-type: application/json');
echo json_encode( $data );
exit;

jQuery Datatables server side processing no result return

As per title described, i have following code to fetch data in the table using datatables server side processing, but end up has no result is return:
include(mysqli_db.php);
$request = $_REQUEST;
$col = array(
0 => 'id',
1 => 'name',
2 => 'phone',
3 => 'ic_pp_no',
4 => 'passport_no',
5 => 'email',
6 => 'address1',
7 => 'address2',
8 => 'country',
9 => 'pp_expiry_date',
10 => 'created_date'
);
$q = "SELECT * FROM account";
$sql = $mysqli->query($q);
$totalData = $sql->num_rows;
$totalFilter = $totalData;
$data = array();
while($row = $sql->fetch_object()){
$subdata = array();
$subdata[] = $row->name;
$subdata[] = $row->phone;
$subdata[] = $row->ic_pp_no.$row->passport_no;
$subdata[] = $row->email;
$subdata[] = $row->address1.$row->address2;
$subdata[] = $row->country;
$subdata[] = $row->id;
$data[] = $subdata;
}
$json_data = array(
'draw' => intval($request['draw']),
'recordsTotal' => intval($totalData),
'recordsFiltered' => intval($totalFilter),
'data' => $data
);
echo json_encode($json_data);
I can see json output with exact num rows return but no data:
{"draw":0,"recordsTotal":81,"recordsFiltered":81,"data":[]}
thanks.

How to add an array to a nested array

My PHP Code is the following, I need some assistance please:
//DB connection
$result = mysqli_query($con,"SELECT * FROM `clients`");
$info = array();
$count = 0;
$meta = array('page' => 1, 'pages' => 1, 'perpage' => -1, 'total' => 14, 'sort' => "asc", 'field' => "ID");
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$info[$row['clientid']]['id'] = $row['id'];
$info[$row['clientid']]['name'] = $row['name'];
$info[$row['clientid']]['email'] = $row['email'];
$info[$row['clientid']]['cell'] = $row['cell'];
$count++;
}
$data = json_encode(array_values($info));
echo $data;
My Result;
[{"ID":1,"name":"A","email":"a#a.com","cell":"082"},
{"ID":2,"name":"B","email":"b#b.com","cell":"083"},
{"ID":3,"name":"C","email":"c#c.com","cell":"079"}]
The JSON should add the meta array with the following result:
{"meta":
{"page": 1,"pages": 1,"perpage": -1,"total": 3,"sort": "asc","field": ID"},
"data": [{"ID":1,"name":"A","email":"a#a.com","cell":"082"},
{"ID":2,"name":"B","email":"b#b.com","cell":"083"},
{"ID":3,"name":"C","email":"c#c.com","cell":"079"}]
},
Create array of required structure and json_encode it:
$data = json_encode(array(
'meta' => $meta,
'data' => array_values($info),
));

How to sum the total value

I am trying to sum up the value. I have stored in the database price and qty. The total = price * qty for that particular item.
I would like to sum all of the items for a sub total.
$sql = "SELECT * FROM parts WHERE jobno = '$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$results['entrants'][] = array(
'partno' => $row['partno'],
'description' => $row['description'],
'price' => $row['price'],
'qty' => $row['qty'],
'total' => $row['price'] * $row['qty'],
);
}
} else {
// Create an empty placeholder record if no entrants were found
$results['entrants'][] = array(
'partno' => NULL,
'description' => NULL,
'price' => NULL,
'qty' => NULL,
'total' => NULL,
);
}
I only know SELECT sum - which I don't think will work because I am not getting the information from the database but from the above.
Just keep track as you go:
$subTotal = 0;
while ($row = $result->fetch_assoc()) {
$lineTotal = $row['price'] * $row['qty'];
$subTotal += $lineTotal;
$results['entrants'][] = array(
//...
'total' => $lineTotal;
);
}
$results['subtotal'] = $subTotal;

sms credit deduction concurrency issue

I currently writing an sms system for the end user. however, When I do the testing on the function i encounter sms credit deduction error.
There are three table in this function which are company, user and transaction.
I did many testing on this function. just cant get the credit deduction correctly.
for example there are 1000 credit in each table.
Initial value
company.sms_bal = 1000
user.user_credit_bal= 1000
transaction.transaction_balance = 1000
testing #1
deduct 1 credit on each table. it deducts correctly.
company.sms_bal = 999
user.user_credit_bal= 999
transaction.transaction_balance = 999
testing #2
deduct 2 credit on each table. it deducts incorrectly,
company.sms_bal = 997
user.user_credit_bal= 997
transaction.transaction_balance = 998 suppose to be 997.
P/S it does not always deduct incorrectly, just sometimes. Please kindly advice me. However if you are unsure my question just let me know I am willing to assist you.
Below is the function for your reference
function process_send_sms($data){
global $CONF, $Q, $OUT, $DB, $DBSH, $CODE, $LANG;
//
// check credit
//
$check_credit = check_credit($data);
if($check_credit['status'] == '100'){
//
// check credit and update credit and update track
//
// get $company_credit_bal
$DBSH->u_select('company', $company_data, array(
'company_id' => $company_id,
));
$company_credit_bal = $company_data['sms_bal'];
// get $user_credit_bal
$DBSH->u_select('user', $user_data, array(
'user_id' => $user_id,
));
$user_credit_bal = trim($user_data['user_credit_bal']);
$number_bulk_mt = trim($total_credit);
//
// update company
//
$rows = $DBSH->update(array(
'table' => 'company',
'set' => array(array('sms_bal = ?', $company_credit_bal - $number_bulk_mt),),
'where' => array(array('company_id = ?', $company_id),),));
$rows = $DBSH->update(array(
'table' => 'user',
'set' => array(array('user_credit_bal = ?', $user_credit_bal - $number_bulk_mt),),
'where' => array(array('user_id = ?', $user_id),),));
}else{
$return_data['error'] = $check_credit['error'];
}
if($check_credit['status'] == '100'){
//
// insert to the track
//
$arr = debug_backtrace();
$function_name = $arr[0]["function"];
$data = array(
'no_phones' => $total_credit,
'company_id' => $company_id,
'user_id' => $user_id,
'msg_type' => $function_name,
'track_mt_create_time' => $timestr,
'track_mt_update_date' => $timestr,
);
$DBSH->u_insert('bulk_mt_log_track', $data);
$ref_id = $DBSH->u_lastid('bulk_mt_log_track');
if($sendsmscsv == 'Y'){
foreach($phone_tem as $k => $v){
$phone_no = $v['phone'];
$message = $v['message'];
$coding = 1;
if($v['lang'] == '2'){
$coding = 3;
}
$check_credit = $credit_class->check_per_credit($phone_no,$is_intern);
$send_c_code = $check_credit['c_code' ];
$per_credit = $check_credit['per_credit'];
if($phone_no){
do{
$msgid = session_get()."_".$user_id;
$check_valid === false;
$found = $DBSH->u_count('bulk_mt_log_data', array(
'msgid' => $msgid,
));
if(!$found){
$check_valid === true;
}
$found = $DBSH->u_count('bulk_mt_log', array(
'msgid' => $msgid,));
if(!$found){
$check_valid === true;
}
}while($check_valid === false);
//
// Perform merge sms
//
$message = process_message_merge($phone_no,$user_id,$message,$company_id);
if($coding == '3'){
$len = mb_strlen($message, 'UTF-8');
$arr = array();
for( $i=0; $i<$len; $i++){
$arr[] = mb_substr($message,$i,1,'UTF-8');;
}
$message2="";
foreach($arr as $k => $substr){
$message2 .= uniord($substr);
}
$message = $message2;
}
else{
$coding = 0;
}
$data = array(
'bulk_mt_log_id' => null,
'user_id' => $user_id,
'company_id' => $company_id,
'mt_to' => $phone_no,
'campaign' => $campaign_name,
'mt_status' => 'P',
'mt_text' => $message,
'coding' => $coding,
'msgid' => $msgid,
'is_intern' => $is_intern?$is_intern:'NOT',
'per_credit' => $per_credit ? $per_credit:'0',
'mt_from' => $mt_from?$mt_from:'',
'send_c_code' => $send_c_code,
'mt_create_date' => $timestr,
'mt_update_date' => $timestr,
'ref_id' => $ref_id,
'gateway_id' => $gateway_id,
);
$DBSH->u_insert('bulk_mt_log', $data);
$rows = $DBSH->select(array(
'field' => array('*'),
'table' => 'transaction',
'where' => array(array('company_id = ?', $company_id),
array('transaction_balance > ?', 0),
array('transaction_sms_expire_date > ?', $timestr),),
'order' => 'transaction_sms_expire_date ASC',
));
$data_transaction_id = array();
while($row = $DBSH->fetchRow(DB_FETCHMODE_ASSOC)){
$data_transaction_id[] = $row[transaction_id];
}
$transaction_id = $data_transaction_id[0];
if($transaction_id){
$rows = $DBSH->update(array(
'table' => 'transaction',
'set' => array(array('transaction_balance = transaction_balance - '.$per_credit),),
'where' => array(array('company_id = ?', $company_id),
array('transaction_id = ?', $transaction_id),),
));
}
}
}
}else{
if ($phone){
foreach($phone as $k => $phone_no){
foreach($sms_message as $km => $message){
$check_credit = $credit_class->check_per_credit($phone_no,$is_intern);
$send_c_code = $check_credit['c_code' ];
$per_credit = $check_credit['per_credit'];
if($phone_no){
do{
$msgid = session_get()."_".$user_id;
$check_valid === false;
$found = $DBSH->u_count('bulk_mt_log_data', array(
'msgid' => $msgid,
));
if(!$found){
$check_valid === true;
}
$found = $DBSH->u_count('bulk_mt_log', array(
'msgid' => $msgid,));
if(!$found){
$check_valid === true;
}
}while($check_valid === false);
//
// Perform merge sms
//
$message = process_message_merge($phone_no,$user_id,$message,$company_id);
if($message_type == 'unicode'){
$coding = 3;
$len = mb_strlen($message, 'UTF-8');
$arr = array();
for( $i=0; $i<$len; $i++){
$arr[] = mb_substr($message,$i,1,'UTF-8');;
}
$message2="";
foreach($arr as $k => $substr){
$message2 .= uniord($substr);
}
$message = $message2;
}
else{
$coding = 0;
}
$data = array(
'bulk_mt_log_id' => null,
'user_id' => $user_id,
'company_id' => $company_id,
'mt_to' => $phone_no,
'campaign' => $campaign_name,
'mt_status' => 'P',
'mt_text' => $message,
'coding' => $coding,
'msgid' => $msgid,
'is_intern' => $is_intern?$is_intern:'NOT',
'per_credit' => $per_credit ? $per_credit:'0',
'send_c_code' => $send_c_code,
'mt_create_date' => $timestr,
'mt_update_date' => $timestr,
'ref_id' => $ref_id,
'mt_from' => $mt_from?$mt_from:'',
'gateway_id' => $gateway_id,
);
$DBSH->u_insert('bulk_mt_log', $data);
$rows = $DBSH->select(array(
'field' => array('*'),
'table' => 'transaction',
'where' => array(array('company_id = ?', $company_id),
array('transaction_balance > ?', 0),
array('transaction_sms_expire_date > ?', $timestr),
),
'order' => 'transaction_sms_expire_date ASC',
));
$data_transaction_id = array();
while($row = $DBSH->fetchRow(DB_FETCHMODE_ASSOC)){
$data_transaction_id[] = $row[transaction_id];
}
$transaction_id = $data_transaction_id[0];
if($transaction_id){
$rows = $DBSH->update(array(
'table' => 'transaction',
'set' => array(array('transaction_balance = transaction_balance - '.$per_credit),),
'where' => array(array('company_id = ?', $company_id),
array('transaction_id = ?', $transaction_id),),
));
}
}
}
}
}
}
}else{
$return_data['error'] = $check_credit['error'];
}
if($return_data){
$return_data['status'] ='102';
}else{
$return_data['status'] ='100';
}
return $return_data;
}

Categories