how to count matched id from excel and database - php

i have an excel file and i want to upload it and save data to database.
And data will be updated based on IC Number (Identity card).
If in excel file there are 4 IC are matched with IC from the database, it will count and there will be a notification if 4 data match and update to database.
The question is how to count matched ic from excel and database?
it always show zero (0)
if($request->hasFile('file')){
$extension = File::extension($request->file->getClientOriginalName());
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
$path = $request->file->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $value) {
$insert['data'] = [
'no' => $value->no,
'name' => $value->name,
'ic_no' => $value->ic_no,
'disburse_date' => $value->disburse_date,
'product_type_description' => $value->product_type_description,
'amount_release' => $value->amount_release,
'amount' => $value->amount,
'date_disburse' => $value->date_disburse,
'net_disbursement' => $value->net_disbursement,
];
if(!empty($insert))
{
$user = Auth::user()->id;
$request = new DisburseFromMbsb;
//$request->cus_id = $data->id_cus;
$request->ic_no = $value['ic_no'];
$request->name = $value['name'];
$request->disburse_date = $value['disburse_date'];
$request->product_type_description = $value['product_type_description'];
$request->amount_release = $value['amount_release'];
$request->amount = $value['amount'];
$request->date_disburse = $value['date_disburse'];
$request->net_disbursement = $value['net_disbursement'];
$request->user_id = $user;
$request->save();*/
Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',0)->update(array(
'status_upload'=>'1',
'amount_release' => $value['amount_release'],
'amount' => $value['amount'],
'date_disburse' => $value['date_disburse'],
'net_disbursement' => $value['net_disbursement'],
'disburse_date' => $value['disburse_date'],
'product_type_description' => $value['product_type_description']
));
$number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count(); // this is my code to count
}
}
return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
}
this is my code to count
$number = Loandisburse::where('ic', $value['ic_no'])->where('stage','W11')->where('status_upload',1)->count();

Assuming all Code you wrote is correct (I have no idea how the Excel Package works ),
there are a few lines that need altering and you are good to go.
First you have to declare the variable number outside the scope of forEach
After that, instead of
$number = Loandisburse.......
You have to use
$number += Loandisburse.........
In order not to overwrite your previous values.
That means you have to try the following code:
if($request->hasFile('file')){
$extension = File::extension($request->file->getClientOriginalName());
$number = 0;
if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") {
$path = $request->file->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $value) {
$insert['data'] = [
'no' => $value->no,
'name' => $value->name,
'ic_no' => $value->ic_no,
'disburse_date' => $value->disburse_date,
'product_type_description' => $value->product_type_description,
'amount_release' => $value->amount_release,
'amount' => $value->amount,
'date_disburse' => $value->date_disburse,
'net_disbursement' => $value->net_disbursement,
];
if(!empty($insert)) {
$user = Auth::user()->id;
$request = new DisburseFromMbsb;
$request->ic_no = $value['ic_no'];
$request->name = $value['name'];
$request->disburse_date = $value['disburse_date'];
$request->product_type_description = $value['product_type_description'];
$request->amount_release = $value['amount_release'];
$request->amount = $value['amount'];
$request->date_disburse = $value['date_disburse'];
$request->net_disbursement = $value['net_disbursement'];
$request->user_id = $user;
$request->save();
Loandisburse::where('ic', $value['ic_no'])
->where('stage','W11')
->where('status_upload',0)->update(array(
'status_upload'=>'1',
'amount_release' => $value['amount_release'],
'amount' => $value['amount'],
'date_disburse' => $value['date_disburse'],
'net_disbursement' => $value['net_disbursement'],
'disburse_date' => $value['disburse_date'],
'product_type_description' => $value['product_type_description']
));
$number += Loandisburse::where('ic', $value['ic_no'])
->where('stage','W11')
->where('status_upload',1)
->count(); // this is my code to count
}//Closing If Not Empty
}//Closing For Each
}//Closing If
}//Closing If
return redirect('/upload/amount')->with(['update' => 'Data saved successfully '.$number.' rows' ]);
}//Closing If

Related

Parsing returns an empty value

I make a parser of items from DotA 2 user inventory in the Steam service. Every time I try to parse user data, I get an empty value:
{"success":true,"items":[]}, but there are items in my Steam inventory.
My function to parse items:
public function loadMyInventory() {
if(Auth::guest()) return ['success' => false];
$prices = json_decode(Storage::get('prices.txt'), true);
$response = json_decode(file_get_contents('https://steamcommunity.com/inventory/'.$this->user->steamid64.'/570/2?l=russian&count=5000'), true);
if(time() < (Session::get('InvUPD') + 5)) {
return [
'success' => false,
'msg' => 'Error, repeat in '.(Session::get('InvUPD') - time() + 5).' сек.',
'status' => 'error'
];
}
//return $response;
$inventory = [];
foreach($response['assets'] as $item) {
$find = 0;
foreach($response['descriptions'] as $descriptions) {
if($find == 0) {
if(($descriptions['classid'] == $item['classid']) && ($descriptions['instanceid'] == $item['instanceid'])) {
$find++;
# If we find the price of an item, then move on.
if(isset($prices[$descriptions['market_hash_name']])) {
# Search data
$price = $prices[$descriptions['market_hash_name']]*$this->config->curs;
$class = false;
$text = false;
if($price <= $this->config->min_dep_sum) {
$price = 0;
$text = 'Cheap';
$class = 'minPrice';
}
if(($descriptions['tradable'] == 0) || ($descriptions['marketable'] == 0)) {
$price = 0;
$class = 'minPrice';
$text = 'Not tradable';
}
# Adding to Array
$inventory[] = [
'name' => $descriptions['market_name'],
'price' => floor($price),
'color' => $this->getRarity($descriptions['tags']),
'tradable' => $descriptions['tradable'],
'class' => $class,
'text' => $text,
'classid' => $item['classid'],
'assetid' => $item['assetid'],
'instanceid' => $item['instanceid']
];
}
}
}
}
}
Session::put('InvUPD', (time() + 5));
return [
'success' => true,
'items' => $inventory
];
}
But should return approximately the following value:
{"success":true,"items":[{"classid":"2274725521","instanceid":"57949762","assetid":"18235196074","market_hash_name":"Full-Bore Bonanza","price":26}]}
Where my mistake?
First of all, you are iterating on descriptions for every assets, which is assets*descriptions iteration, it's quite a lot, but you can optimize this.
let's loop once for descriptions and assign classid and instanceid as object key.
$assets = $response["assets"];
$descriptions = $response["descriptions"];
$newDescriptions=[];
foreach($descriptions as $d){
$newDescriptions[$d["classid"]][$d["instanceid"]] = $d;
}
this will give as the ability to not loop over description each time, we can access the description of certain asset directly $newDescriptions[$classid][$instanceid]]
foreach($assets as $a){
if(isset($newDescriptions[$a["classid"]]) && isset($newDescriptions[$a["classid"]][$a["instanceid"]])){
$assetDescription = $newDescriptions[$a["classid"]][$a["instanceid"]];
$inventory = [];
if(isset($prices[$assetDescription["market_hash_name"]])){
$price = $prices[$assetDescription['market_hash_name']]["price"]*$this->config->curs;
$class = false;
$text = false;
if($price <= $this->config->min_dep_sum) {
$price = 0;
$text = 'Cheap';
$class = 'minPrice';
}
if(($assetDescription['tradable'] == 0) || ($assetDescription['marketable'] == 0)) {
$price = 0;
$class = 'minPrice';
$text = 'Not tradable';
}
$inventory["priceFound"][] = [
'name' => $assetDescription['market_name'],
'price' => floor($price),
'color' => $this->getRarity($assetDescription['tags']),
'tradable' => $assetDescription['tradable'],
'class' => $class,
'text' => $text,
'classid' => $a['classid'],
'assetid' => $a['assetid'],
'instanceid' => $a['instanceid']
];
}else{
$inventory["priceNotFound"][] = $assetDescription["market_hash_name"];
}
}
}
About your mistake:
are you Sure your "prices.txt" contains market_hash_name?
I don't see any other issue yet, operationg on the data you have provided in comment, I got print of variable $assetDescription. Please doublecheck variable $prices.

How to read Images from excel and store them in database using laravel?

I am using Maatwebsite\Excel\ExcelServiceProvider::class plugin ,I want to store images and product details through Excel sheet.
How can I upload images .excel data ?
please help me...
ProductController.php
public function excelupload()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
dd($value);
$insert[] = ['product_name' => $value->product_name, 'cataloge_number' => $value->cataloge_number, 'cas_number' => $value->cas_number, 'product_image' => $value->product_image, 'chemical_name' => $value->chemical_name , 'synonyms' => $value->synonyms , 'molecular_formula' => $value->molecular_formula, 'molecular_mass' => $value->molecular_mass ,'product_stock' => $value->product_stock];
}
if(!empty($insert)){
DB::table('products')->insert($insert);
return redirect('admin/product')->with('message', 'New Product Added Successfully!');
}
}
}
return back();
}
If product_image column in excel has URL of image then you need to install Laravel Image Intervention Package:
http://image.intervention.io/
or
https://github.com/Intervention/image
After install, try this code for store images from URL in Local Laravel Storage and save the path in Database:
public function excelupload()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
dd($value);
$file = $value->product_image;
if ($file)
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
$main_image = Image::make(file_get_contents($file))->widen(860, function ($constraint) {
$constraint->upsize();
});
$dir = 'photos/';
$path = uniqid().'.jpg';
$main_image->encode();
Storage::put($dir.$path, (string)$main_image);
$product_image = $path;
}else{
$product_image = null;
}
$insert[] = [
'product_name' => $value->product_name,
'cataloge_number' => $value->cataloge_number,
'cas_number' => $value->cas_number,
// 'product_image' => $value->product_image,
'product_image' => $product_image,
'chemical_name' => $value->chemical_name ,
'synonyms' => $value->synonyms ,
'molecular_formula' => $value->molecular_formula,
'molecular_mass' => $value->molecular_mass,
'product_stock' => $value->product_stock
];
}
if(!empty($insert)){
DB::table('products')->insert($insert);
return redirect('admin/product')->with('message', 'New Product Added Successfully!');
}
}
}
return back();
}

getting multiple customer details In codeigniter

i have written this code to receive data from the Android device. it was inserted just one customer data I need to receive multiple customer details if app goes offline. but it was inserting one data into DB in offline mode also.how can i change this for multiple customer data insertions.
function index_post($customerID = false) {
if ($customerID) {
//update the record
$updateData = array();
$allowedparams = array('streetid' => 'streetid', 'name' => 'name', 'mobile' => 'mobile', 'adhaar' => 'adhaar', 'profession' => 'profession', 'address' => 'address', 'pincode' => 'pincode', 'nearby' => 'nearby', 'paddress' => 'paddress', 'isOwned' => 'isOwned');
foreach ($allowedparams as $k => $v) {
if (!$this->IsNullOrEmptyString($this->post($k, true))) {
$updateData[$v] = $this->post($k, true);
}
}
if ($this->model_customer->update($customerID, $updateData)) {
$data = array('status' => true, 'messsage' => 'cusotmer updated succesfully');
$http_code = REST_Controller::HTTP_OK;
} else {
$data = array('status' => false, 'messsage' => 'cusotmer failed to update.');
$http_code = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
}
} else {
//insert the record
$allowedparams = array('streetid' => 'streetid', 'name' => 'name', 'mobile' => 'mobile', 'adhaar' => 'adhaar', 'profession' => 'profession', 'address' => 'address', 'pincode' => 'pincode', 'cycle' => 'cycle', 'nearby' => 'nearby', 'paddress' => 'paddress', 'isOwned' => 'isOwned');
$requiredParam = array('streetid', 'name', 'mobile', 'cycle');
$insertdata = array();
foreach ($allowedparams as $k => $v) {
if (in_array($k, $requiredParam)) {
//check if its not null
if ($this->post($k) == null || trim($this->post($k)) == '') {
$data = array('status' => false, 'message' => $k . ' parameter missing or empty');
$http_code = REST_Controller::HTTP_BAD_REQUEST;
break;
}
}
$insertData[$v] = $this->post($k, true);
}
if ($customerID = $this->model_customer->create($insertData)) {
$data['customerID'] = $this->_frameCustomer2($this->model_customer->get($customerID)); //you need to put
$http_code = REST_Controller::HTTP_OK;
} else {
$data = array('status' => false, 'message' => 'unable to create customer');
$http_code = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
}
}
$this->response($data, $http_code);
}
private function _frameCustomer2($c) { //get value from index_get
$data = array();
$data['id'] = $c->id;
$data['name'] = $c->name;
$data['street'] = array('id' => $c->streetid);
$data['creationDate'] = $c->creationdate;
$data['mobile'] = $c->mobile;
$data['adhaar'] = $c->adhaar;
$data['profession'] = $c->profession;
$data['isOwned'] = ($c->isOwned == 1) ? true : false;
$data['address'] = $c->address;
$data['pincode'] = $c->pincode;
$data['status'] = $c->status;
$data['cycle'] = $c->cycle;
$data['balance'] = $c->balance;
$data['creditAvailable'] = $c->creditbalance;
$data['nearby'] = $c->nearby;
$data['accountNumber'] = $c->accountnumber;
$data['permanentAddress'] = $c->paddress;
$data['lastVisit'] = $this->model_customer->lastVisit($c->id);
return $data;
}
and my part of model function is
function create($insertdata = array()) { //new customer insert
if ($this->db->insert('customer', $insertdata)) {
return $this->db->insert_id();
} else {
return false;
}
}
function update($customerID = 0, $updateData = array()) {
$this->db->where('id', $customerID);
if ($this->db->update('customer', $updateData) && $this->db->affected_rows() == 1) {
return true;
} else {
return false;
}
Instead of customer Id, you can ask the mobile developers to send data in the form of array. In both online and offline. In case of online there will be just one element in the request array.
function index_post() {
$request_data = $this->request->body;
foreach($request_data as $key => $value)
{
//Check if customer id is present
if(Customer present)
{
Update the record
} else {
Insert the record
}
}
}

how to insert multiple records in table using laravel

Hi all I'm trying to insert multiple records using an api into a table. But I'm getting error like "Error connecting to database". How can i resolve it.
Below is my code
public function addToCart(){
$input = Input::all();
$data['status'] = 0;
$data['error'] = true;
if(isset($input['user_id']) && array_filter($input['cart']) > 0 ){
foreach($input['cart'] as $key => $val){
if(!empty($val['quantity']) && !empty($val['price']) && !empty($val['sku']) && !empty($val['qrcode']) && !empty($val['product_id']))
{
$totalPrice = $val['quantity']* $val['price'];
$cartId = DB::table('jocom_cart')->insertGetId(array(
'user_id' => $input['user_id'],
'product_id' => $val->product_id,
'sku' => $val->sku,
'quantity' => $val->quantity,
'price' => $val->price,
'total_price' => $totalPrice,
'qrcode' => $val->qrcode
)
);
}
else{
$data['message'] = 'All field are required.';
}
return Response::json($data);
}
Create one array of the data and then pass the array to insert function
try to avoid query in the loop
$insertRecord = [];
foreach($input['cart'] as $key => $val){
if(!empty($val['quantity']) && !empty($val['price']) && !empty($val['sku']) && !empty($val['qrcode']) && !empty($val['product_id']))
{
$totalPrice = $val['quantity']* $val['price'];
$insertRecord[$key] = ['user_id'=>$input['user_id'],'product_id' => $val->product_id,... ,... ,...]; All your column with value
}
}
if(!empty($insertRecord)){
Model::insert($insertRecord); // Eloquent approach
DB::table('table')->insert($insertRecord); // Query Builder approach
}
Your record should be in array.
Table::insert(Array)

Slow upload with .csv files and PHP

I've created a function to upload csv files using PHP and insert its contents into a Mongo database.
The upload is taking to much time to be completed and I don't have idea how to build a better code to do this.
Bellow my function in PHP:
public function importEmail($file,$list_name,$header){
$data = $this->ReadCSV($file, $has_header);
global $Email;
$falhas = 0;
array_shift($data);
if($data === false) {
return false;
} else {
foreach($data as $key => $value) {
$Email->email = $value[$header["email"]];
$Email->first_name = $value[$header["fName"]];
$Email->last_name = $value[$header["lName"]];
$Email->gender = strtoupper($value[$header["gender"]]);
$Email->status = $value[$header["status"]] !="" ? $value[$header["status"]] : "1";
$Email->list_name = $list_name;
$Email->opt_out = $v = (strtoupper($value[$header["opt"]]) != "" ? strtoupper($value[$header["opt"]]) : "N");
if(!$Email->update(trim($value[$header["email"]]))) $falhas++;
}
}
return $falhas === 0 ? true : false;
}
EDITED
This is the update() method which is responsible to insert records into MongoDB
public function update($id) {
global $mongo, $gClient;
$update = array(
'$set' => array(
'userData.name' => $this->first_name,
'userData.lastName' => $this->last_name,
'userData.gender' => $this->gender,
'userData.optOut' => $this->opt_out == "Y" || $this->opt_out == "N" ? $this->opt_out : "Y",
'userData.lastUpdate' => (int)(microtime(true) * 1000),
'userData.status' => $this->status == 0 || $this->status == 1 ? +$this->status : 0
),
'$setOnInsert' => array(
'userData.registerDate' => (int)(microtime(true) * 1000)
)
);
if(is_string($this->list_name) && strlen($this->list_name) > 0)
$update['$push'] = array(
'userData.meta' => array(
'type' => 'listname',
'name' => $this->list_name
)
);
$result = $mongo->{$gClient->slug}->usersHistory->update(array(
'userEmail' => $id
), $update, array(
'upsert' => true
));
if(!$this->email) $this->email = $id;
$mongo->{$gClient->slug}->usersHistory->update(array(
'userEmail' => $id
), array(
'$set' => array(
'userEmail' => $this->email
)
));
return $result['n'] > 0;
}
Those .csv files are always bigger than 50k (lines).
How can I build something faster than this method?

Categories