Count post data in a CodeIgniter controller - php

I have five text fields that are not mandatory. Sometimes enter data in one field, sometimes 2 or 3 or 4 or 5. I have to count this data:
$sib1=$this->input->post(sib1);
$sib2=$this->input->post(sib2);
$sib3=$this->input->post(sib3);
Like this, I want to count this. Now 3

You can use a for loop with length = number of text fields. Put it to the database afterwards. sibcount is the number of filled entries.
$data = array();
$length = 5;
$sibcount = 0;
for ($i=0;$i<$length;$i++) {
$entry = $this->input->post("sib".$i);
if (!empty($entry)) {
$data["sib".$i] = $entry;
$sibcount++;
}
}
$data["sibcount"] = $sibcount;
// use in controller or model
if (!empty($data))
$this->db->insert('mytable', $data);

this is way to do it:
Take an array $arr=array();
$arr['sib1']=$this->input->post("sib1",true);
$arr['sib2']=$this->input->post("sib2",true);
$arr['sib3']=$this->input->post("sib3",true);
$arr['count']=count($this->input->post($arr)); //this will count your post
$result = $this->Model_name->model_function($arr);
In Model
function model_function($arr)
{
$this->db->insert('tbl',$arr);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}

Related

Is there a way to update 12000+ rows from txt file in less then 2mins?

I need to update a table with more then 12000 row using php Codeigniter and a txt file.. reading the file and the foreach loop are fine but when updating line by line it takes like 30 mins, I guess the problem is I'm searching by name because I have no id in the txt file...
Here is my code:
controller:
$fn = fopen($this->upload->data('full_path'),"r");
$update = true;
while(! feof($fn) && $update) {
$pieces = explode("|", fgets($fn));
if(sizeof($pieces) == 9 && is_numeric(trim($pieces[1]))) {
$update = $this->model_products->update3s($pieces);
}
}
fclose($fn);
Model:
public function update3s($product) {
if ($product) {
$product[2] = trim(str_replace("'","''",$product[2]));
$product[1] = trim($product[1]);
$product[6] = trim($product[6]);
$product[3] = trim($product[3]);
$sql = "UPDATE products set qty = $product[3], price_vente = $product[6] where (name = '$product[2]')";
echo $sql.'<br>';
$update = $query = $this->db->query($sql);
return $update;
}
return false;
}
You can use transaction and add index for column name in database table.
$fn = fopen($this->upload->data('full_path'),"r");
$update = true;
$updatedCount = 0;
while(! feof($fn) && $update) {
$pieces = explode("|", fgets($fn));
if(sizeof($pieces) == 9 && is_numeric(trim($pieces[1]))) {
if ($updatedCount == 0) {
$databaseInstance->beginTransaction();
}
$update = $this->model_products->update3s($pieces);
++$updatedCount;
if ($updatedCount > 500) { //in one transaction update 500 rows
$databaseInstance->commit();
$updatedCount = 0;
}
}
}
if ($updatedCount > 0) { // if we have not commited transaction
$databaseInstance->commit();
}
fclose($fn);
Some tips
Add index to field name
Use prepared statements
Disable the MySQL forgeign key check Read more
writing sql function can do that even in much lesser time .
using feature like :
REPLACE()
cursors
SPLIT_STRING(custom)
in a mysql user defined function
CREATE FUNCTION update3s(hole_file_content LONGTEXT) RETURNS Boolean
BEGIN
-----Your implementation(same logic in sql ) ------
END
then coll it just by if it is CI 3
$this->db->call_function('update3s', file_get_contents($this->upload->data('full_path')));
else
$this->db->query("select update3s(".file_get_contents($this->upload->data('full_path')).")");

Count Array where Column Value Equals Given

I have a controller where an array of checkbox values are passed on to the controller and then sent on to do a variety of tasks. My problem is focused on getting a count of the arrays' shipment's customer's billing method, but I'm not entirely sure how I would get those values since I'm posting an array and I want a count.
Here's my controller:
public function sendNow(Request $request)
{
$now = Carbon::now();
$sendNowShipment = array();
$method = array();
$sendNowShipment = request('send');
foreach($sendNowShipment as $SNS){
$shipment = Shipment::findOrFail($SNS);
if($shipment->billtoAccount->billingMethods->label == "Mail"){
$timestamp = Carbon::now()->timestamp;
$shipment_details = $shipment->shipment_details;
$path = 'temp/freightbill_'.$shipment->pro_number.'-'.$timestamp.'.pdf';
$sessionPath[] = $path;
$pdf = PDF::loadView('shipments.pdf', compact('shipment', 'shipment_details'))
->save($path);
}elseif($shipment->billtoAccount->billingMethods->label == "Email"){
$billToAccount = $shipment->billtoAccount;
$billToAccountUsers = $billToAccount->users;
if ($billToAccount->primary_email){
$billToEmail[] = $billToAccount->primary_email;
}
if ($billToAccountUsers->count() > 0){
foreach ($billToAccountUsers as $billToAccountUser){
$billToEmail[] = $billToAccountUser->email;
}
}
foreach ($billToEmail as $bte){
Mail::to($bte)->send(new newBillToShipment($shipment));
}
}
}
if(count($shipment->billtoAccount->billingMethods->label == "Mail") > 0){// count where shipments->customers->billingMethods = Mail) > 0
$pdf = new PDFMerger();
// Add 2 PDFs to the final PDF
foreach($sessionPath as $sp){
$pdf->addPDF($sp, 'all');
}
// Merge the files and retrieve its PDF binary content
$timestamp = Carbon::now()->timestamp;
$binaryContent = $pdf->merge('download', $timestamp."-printBatch.pdf");
// Return binary content as response
//return response($binaryContent)
// ->header('Content-type' , 'application/pdf');
return back();
//dd($sendNowShipment,$sendMethod);
}
}
If you look at this line (a little past the middle):
if(count($shipment->billtoAccount->billingMethods->label == "Mail") > 0){
// count where shipments->customers->billingMethods = Mail) > 0
You'll see that I'm going through a multitude of relationships just to get the value of "Mail" that I am looking for.
So I'm wondering if this should be a DB:: query through laravel, but I'm not sure how I would perform the query using the given checkbox array I have being POSTed to this controller.

php code to search multiple words in varchar

From my application, I send a list of users that I want to search the group they are subscribed:
$selectedUsers = ["John", "Carlos", "Anna", "Julia"]
I have in my database many different groups with many different users in
each of them:
$football = ["**John**" ,"**Carlos**" ,"Daniel" ,"Rob" ,"Frank" ,"Bob"]
$cooking = ["**John**" , "**Anna**" , "**Julia**" , "Claudia" , "Rob" , "Adriana"]
$startups = ["**John**", "**Carlos**", "**Anna**", "**Julia**", "Rob", "Adriana"]
The output I want to have is the sorted list of groups with the amount of the selectedUsers in it:
$returnArray[0] = $startups //4 users inside group
$returnArray[1] = $cooking //3 users inside group
$returnArray[2] = $football //2 users inside group
Here is the code I have so far, but the loop I'm using is based on the group_id I've stored and I want to change that:
<?php
//fetch groups with users
$returnValue = array();
$groupUsersNumber = 0;
$selectedUsers = htmlentities($_REQUEST["selectedUsers"]);
$lastGroupID = htmlentities($_REQUEST["lastGroupID"]); //remove
if($lastGroupID == ""){
$lastGroupID = getLastGroupID();
$lastGroupID = $lastGroupID + 1;
}
if($selectedUsers == ""){
//return all groups ordered by ID desc
$group = getGroupWithID($lastGroupID);
} else{
$usersArray = explode(', ', $selectedUsers);
$foundGroup = false;
while($foundGroup == false){
$group = getGroupWithID($lastGroupID);
$fetchedGroupUsers = explode(', ', $group["users"]);
for($i = 0; $i < count($usersArray); $i++){
if(in_array($usersArray[$i], $fetchedGroupUsers)){
$foundGroup = true;
break;
} else{
$lastGroupID = $group["group_id"];
}
}
}
}
for($i = 0; $i < count($usersArray); $i++){
if(in_array($usersArray[$i], $fetchedGroupUsers)){
$groupUsersNumber = $groupUsersNumber + 1;
}
}
if(empty($group))
{
$returnValue["status"]="403";
$returnValue["message"]="No more groups with that users.";
echo json_encode($returnValue);
return;
} else{
$returnValue=$group;
$returnValue["groupUsersNumber"]=$groupUsersNumber;
}
echo json_encode($returnValue);
?>
Is there any other way to have a better/ more efficient way to search into my database? Appreciated!
It seems that your database is not normalized. A normalized database may be the more efficient way. Do not store users in a describing varchar. Instead establish a many to many relation.
Beside that the FilterIterator class of PHP is something for you. It is reusable and a bit more efficient at iterating over arrays.
Here 's a short example.
class NameFilterIterator extends FilterIterator {
protected $filter = null;
public function __construct(Iterator $iterator, $filter) {
parent::__construct($iterator);
$this->filter = $filter;
}
public function accept() {
$current = $this->getInnerIterator()->current();
if (strpos($current, $this->filter) !== false) {
return true;
}
return false;
}
}
// Usage
$aUsers = [ 'John', 'Carlos', 'Anna', 'Julia' ];
$oFootball = new ArrayIterator(["**John**" ,"**Carlos**" ,"Daniel" ,"Rob" ,"Frank" ,"Bob"]);
foreach ($aUsers as $sUser) {
$oFilter = new NameFilterIterator($oFootball, $sUser);
foreach ($oFilter as $sName) {
var_dump($sName); // outputs: John, Carlos
}
}
The internal memory usage of the FilterIterator object is way more efficient.
You appear to be mixing up php and mysql, and it would be better to redesign your database.
However as a basic idea you can do roughly what you want in MySQL. It is not nice, and not efficient but something like this:-
SELECT a.group_description ,
GROUP_CONCAT(b.wanted_name)
FROM some_table a
LEFT OUTER JOIN
(
SELECT "John" AS wanted_name UNION SELECT "Carlos" UNION SELECT "Anna" UNION SELECT "Julia"
) b
ON FIND_IN_SET(b.wanted_name, a.group_users)
GROUP BY a. group_description

Putting data base values into array shows error in codeigniter

I am taking data from many tables. I want to display many objects in different places. I got the data from data base, but I want to put the data into an array for useful purpose, but it's not working.
This my controller code:
public function compare_by_business_sectors() {
//print_r($this->input->post());exit;
if ($this->input->post())
{
$solution_array = array();
//print_r (json_encode($business_sectors)); exit;
$business_sectors=$this->home_model->compare_business_sectors_data($this->input->post());
$tab_child_id = "";
$id="";
foreach ($business_sectors as $key=>$sectors) {
$solution_array[1]=$sectors->solution_name;
$solution_array[2]=$sectors->description;
$solution_array[3]=$sectors->vendor_name;
$solution_array[4]=$sectors->video_presentation;
$solution_array[5]=$sectors->start_free_trail;
$solution_array[6]=$sectors->hardware_package;
$solution_array[7]=$sectors->pos_market_rating;
//$solution_array[$sectors->field_id] = $sectors->value;
$id = "solution".$sectors->tab_child_id;
if ($tab_child_id != $sectors->tab_child_id) {
$id = array();
$id[$sectors->field_id] = $sectors->title;
}
else if ($tab_child_id == $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
}
//$solution_array[$id]= $id;
}
print_r($id);
//$this->load->view('compare_by_business_sectors.php');
}
This is my model code:
public function compare_business_sectors_data($sectorid) {
$query = $this->db->select('solutions.*,solution_tabs_child_fields.field_id,solution_tabs_child_fields.tab_child_id,solution_tabs_child_fields.title')
->from('solutions')
//->join('solutions', 'business_sector.sector_id = solutions.business_sector_id',"left")
->join('solution_features','solutions.entry_id = solution_features.entry_id',"left")
->join('solution_tabs_child_fields','solution_features.field_id = solution_tabs_child_fields.field_id')
->where('solutions.business_sector_id', $sectorid['id'])
->get();
return $query->result();
//print_r($query->result());exit;
}
change it as follow and try.
$id_string = "";
$id_array = array();
foreach ($business_sectors as $key=>$sectors) {
$solution_array[1]=$sectors->solution_name;
$solution_array[2]=$sectors->description;
$solution_array[3]=$sectors->vendor_name;
$solution_array[4]=$sectors->video_presentation;
$solution_array[5]=$sectors->start_free_trail;
$solution_array[6]=$sectors->hardware_package;
$solution_array[7]=$sectors->pos_market_rating;
//$solution_array[$sectors->field_id] = $sectors->value;
$id_string = "solution".$sectors->tab_child_id;
if ($tab_child_id != $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
else if ($tab_child_id == $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
}
print_r($id_string);
print_r($id_array);
First you are assigning string value to $id then, $id will convert to array only if first if() statement execute other wise it will not be a string. So to overcome from this keep $id_array before for loop and you can capture string in another variable.

Get All Credit Cards of a Customer in Braintree

With
$result = Braintree_Customer::find($id);
it's possible to fetch all data from a customer by providing an additional attribute. But if I want to fetch bulked data like all creditCards types it's not possible as I can't iterate through the creditCards attribute with a foreach or a for loop. Does this attribute contain a length or size value that I could use?
What here is the right approach?
I found a possible approach by simply checking for the last attribute of an object to be null.
public function getCustomerPaymentMethods($id)
{
try {
$i = 0;
$result = Braintree_Customer::find($id);
while($result->creditCards[$i]->token !== null) {
$card[$i] = new stdClass();
$card[$i]->token = $result->creditCards[$i]->token;
$card[$i]->type = $result->creditCards[$i]->cardType;
$card[$i]->default = $result->creditCards[$i]->default;
$card[$i]->expired = $result->creditCards[$i]->expired;
$card[$i]->last4 = $result->creditCards[$i]->last4;
$i++;
}
return $card;
} catch(Braintree_Exception_NotFound $e) {
return false;
}
}

Categories