csv file emport from php to mysql - file-import

i wanna to try import .csv file using php & mysql but its showing error . see my code:
function import()
{
$this->load->library('csvimport');
$file_data = $this->csvimport->get_array($_FILES["csv_file"]["tmp_name"]);
//echo $file_data;
foreach ($file_data as $row){
//error_reporting(0);
if($row["Check-in at"] > '0:00:00'){
$date = date('Y-m-d',strtotime($row["Date"]));
$duplicate = $this->attendance_model->getDuplicateVal($row["Employee No"],$date);
//print_r($duplicate);
if(!empty($duplicate)){
$data = array();
$data = array(
'signin_time' => $row["Check-in at"],
'signout_time' => $row["Check-out at"],
'working_hour' => $row["Work Duration"],
'absence' => $row["Absence Duration"],
'overtime' => $row["Overtime Duration"],
'status' => 'A',
'place' => 'office'
);
$this->attendance_model->bulk_Update($row["Employee No"],$date,$data);
} else {
$data = array();
$data = array(
'emp_id' => $row["Employee No"],
'atten_date' => date('Y-m-d',strtotime($row["Date"])),
'signin_time' => $row["Check-in at"],
'signout_time' => $row["Check-out at"],
'working_hour' => $row["Work Duration"],
'absence' => $row["Absence Duration"],
'overtime' => $row["Overtime Duration"],
'status' => 'A',
'place' => 'office'
);
//echo count($data);
$this->attendance_model->Add_AttendanceData($data);
}
} else {
}
}
echo "successfully Updated";
}
}
error is:
A PHP Error was encountered
severty: Notice
Message: Undifined index: Check-in at
File name: controllers/Attendence.php
line number: 241
Backtrace:
File: ...../../../attendance.php
line: 241
function: error_handler

Related

Sending variable to search page

I`ve made this action:
add_action( 'init', 'advanced_search_query' );
function advanced_search_query( $query ) {
if ( isset($_GET['make']) && isset($_GET['model']) && isset($_GET['fuel']) ) {
$all_products = [];
$inner_cats = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $_GET['model']]);
foreach ($inner_cats as $cat) {
if ($_GET['fuel'] == 0)
$fuel_cat = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $cat->term_id, 'name' => 'Diesel']);
elseif ($_GET['fuel'] == 1)
$fuel_cat = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $cat->term_id, 'name' => 'Benzin']);
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $fuel_cat[0]->term_id,
'operator' => 'IN',
)
)
);
$products = get_posts( $args );
if ($products) {
foreach ($products as $prd) {
$all_products[] = $prd;
}
}
}
}
}
Now what I am strugling with is sending the $all_products to my search page and loop them. Tried to redirect to the search page like wp_safe_recirect('domain.com?s=""') with empty s parameter and use the all_results there. Also tried to set the variable as global but again couldnt reach it.
Did you try the following?
if ($products) {
foreach ($products as $prd) {
$all_products[] = $prd;
?>
<script>window.location.href="domain.com?s=''"</script>
<?php
}
}

how to make a multypart/data request with browserkit client?

$data = [
'name' => 'Merry Christamas',
'description' => 'Merry Christamas',
'starts_at' => '2018-9-15 12:45:56',
'ends_at' => '2050-9-15 12:45:56',
'priority' => -1,
'coupon_based' => false,
'action' => [
'type' => 'order_fixed_discount',
'configuration' => [
'amount' => 100
]
],
'rules' => [
[
'type'=> 'item_total',
'configuration' => [
'amount' => 2500,
'base_amount' => 100,
]
]
]
];
$multipartStream = new MultipartStream($this->flatten($data)); //use GuzzleHttp\Psr7\MultipartStream;
I use following method to convert the associative array to MultipartStream needs.
protected function flatten(array $array, string $prefix = '', string $suffix = ''): array
{
$result = [];
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, $this->flatten($value, $prefix . $key . $suffix . '[', ']'));
} else {
if ($value instanceof UploadedFile) {
$result[] = [
'name' => $prefix . $key . $suffix,
'filename' => $value->getClientOriginalName(),
'Mime-Type' => $value->getClientMimeType(),
'contents' => file_get_contents($value->getPathname()),
];
} else {
$result[] = [
'name' => $prefix . $key . $suffix,
'contents' => $value,
];
}
}
}
return $result;
}
then make request with browserkit client
$this->client->request(
'POST',
'/api/admin/promotions',
[],
[],
[
'CONTENT_TYPE' => 'multipart/form-data; boundary=--'.$multipartStream->getBoundary() ,
'HTTP_Authorization'=> "blabla"
],
$multipartStream->getContents()
);
but the request->request->all() is empty, what is the correct way to make a multipart/data request with symfony browserkit client? I have searched a lot , but have no lucky to find an example.

WordPress plugin not installing(instagram-auto-poster)

I want to install instagram-auto-poster but it shows error in these lines:
$data = [
'phone_id' => $this->generateUUID(true),
'_csrftoken' => $token[0],
'username' => $this->username,
'guid' => $this->uuid,
'device_id' => $this->device_id,
'password' => $this->password,
'login_attempt_count' => '0',
];
$login = $this->request('accounts/login/', $this->generateSignature(json_encode($data)), true);
if ($login[1]['status'] == 'fail') {
throw new InstagramException($login[1]['message']);
return;
}

symfony test undefined function

i have problem with my test. I don't know why my function is undefined. I add use statetment, phpstorm see this class. But when run test error with undefined.
namespace tests\AppBundle\Parser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use AppBundle\Parser\CommissionDataParser;
class CommissionDataParserTest extends WebTestCase
{
public function testGroupOrdersByWeek()
{
$orders = [
0 => [
'date' => '2016-01-10',
'client_id' => '2',
'client_type'=> 'natural',
'operation_type' => 'cash_in',
'operation_sum' => '200.00',
'operation_currency' => 'EUR',
],
1 => [
'date' => '2016-01-05',
'client_id' => '1',
'client_type'=> 'legal',
'operation_type' => 'cash_out',
'operation_sum' => '300.00',
'operation_currency' => 'EUR',
],
2 => [
'date' => '2016-01-11',
'client_id' => '1',
'client_type'=> 'natural',
'operation_type' => 'cash_out',
'operation_sum' => '30000',
'operation_currency' => 'JPY'
]
];
$expected = [
0 => [
'date' => '2016-01-05',
'client_id' => '2',
'client_type'=> 'natural',
'operation_type' => 'cash_in',
'operation_sum' => '200.00',
'operation_currency' => 'EUR',
],
1 => [
'date' => '2016-01-10',
'client_id' => '1',
'client_type'=> 'legal',
'operation_type' => 'cash_out',
'operation_sum' => '300.00',
'operation_currency' => 'EUR',
],
2 => [
'date' => '2016-01-11',
'client_id' => '1',
'client_type'=> 'natural',
'operation_type' => 'cash_out',
'operation_sum' => '30000',
'operation_currency' => 'JPY'
]
];
$um = new CommissionDataParser();
$result = $um->groupOrdersByWeek($orders);
$this->assertEquals($expected, $result, '**** -->>>>> result array wrong');
}
there is function that i want to test: i put small part of this class, for example
namespace AppBundle\Parser;
class CommissionDataParser
{
public function getData($file)
{
$orders = $this->extractOrders($file);
if (is_array($orders)) {
$orders = $this->groupOrdersByWeek($orders);
}
// ...
}
public function extractOrders($file)
{
$orders = [];
$data = [];
//$lines = explode(PHP_EOL, file_get_contents($file));
if (($handle = fopen($file, "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($row);
if ($num !== 6) {
return 'Badly structured file';
} else if ($num == 0) {
return 'file is empty';
}
$data[] = $row;
}
fclose($handle);
}
foreach ($data as $row)
{
$orders[] = [
'date' => $row[0],
'client_id' => $row[1],
'client_type' => $row[2],
'operation_type' => $row[3],
'operation_sum' => $row[4],
'operation_currency' => $row[5]
];
}
return $orders;
}
In first, you must check that your phpunit uses app/autoload.php as bootstrap. Open your phpunit.xml.dist file in your project root, and find this line:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="app/autoload.php"
>
If u see this line bootstrap="app/autoload.php" in your file, then all right.
Next, check that your file CommissionDataParser.php physically located in this directory AppBundle\Parser. Full path to this file must be YOUR_PROJECT_ROOT\src\AppBundle\Parser\CommissionDataParser.php
If you did everything correctly then it should work. At least I was able to run your code.
yes really have :) i take a picture for example : )

Drupal first module -- 500 Internal Server Error

This is my first module. I have been trying to successfully install my module since a week now.
I have gone through every line. Even the schema is being installed and entry is made at system table. But still after enabling the module It shows 500 Internal Server Error until I delete the entry from system table.
Please guide me what I am doing wrong.
Note: sisattribute table is already created in drupal database
My .install file
<?php
/**
* #file
*/
function sisinstitute_install() {
drupal_install_schema('sisinstitute');
variable_set('node_options_sisinstitute', array('status'));
$attributes = array();
$attributes['Country'] = array(
'US' => 'United States of America',
'AD' => 'Andorra',
'AE' => 'United Arab Emirates',
'AF' => 'Afghanistan',
);
$s = "INSERT INTO {sisattribute} (domain, akey, avalue, weight) VALUES ('%s', '%s', '%s', %d)";
$prevdomain = '';
$weight = 0;
foreach ($attributes as $domain => $attribute) {
if ($domain != $prevdomain) $weight=0;
foreach ($attribute as $key => $value) {
db_query($s, $domain, $key, $value, $weight);
$weight++;
}
$prevdomain = $domain;
}
}
function sisinstitute_disable() {
drupal_set_message(t('Please note that they will now have reduced functionality, and will not be protected by previous access controls.'), 'warning');
}
function sisinstitute_uninstall() {
drupal_uninstall_schema('sisinstitute');
db_query($s = "DELETE FROM {sisattribute} WHERE domain IN ('Country')");
}
function sisinstitute_schema() {
$schema['sisinstitute'] = array(
'fields' => array(
'vid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
'country' => array('type' => 'varchar', 'length' => 100),
'www' => array('type' => 'varchar', 'length' => 100),
'phone' => array('type' => 'varchar', 'length' => 100),
'email' => array('type' => 'varchar', 'length' => 50),
'provstate' => array('type' => 'varchar', 'length' => 50),
'zip' => array('type' => 'varchar', 'length' => 10),
'city' => array('type' => 'varchar', 'length' => 100),
'address' => array('type' => 'varchar', 'length' => 100),
'orglanguage' => array('type' => 'varchar', 'length' => 100),
'isactive' => array('type' => 'int', 'default' => 1),
),
'primary key' => array('vid'),
'indexes' => array(
'nid' => array('nid')
),
);
return $schema;
}
And my .module file:
<?php
// $Id$
/**
*#File
*Module for Institution support in SIS package
*/
/**
*hook_help()
*/
/**
*hook_menu()
*/
/**
*hook_perm()
*/
function sisinstitute_perm() {
return array('access institute', 'create institute', 'edit institute', 'delete institute', 'view belonged institute', 'view all institutes');
}
/**
*hook_access()
*/
function sisinstitute_access($op, $node. $account=NULL) {
if (empty($account)) {
global $user;
$account = $user;
}
if (is_numeric($node)) $node = node_load($node);
if (!isset($account->sisinstitute_nid) && module_exists('sisstudent')) {
_sisstudent_load($account);
}
if (!isset($account->sisinstitute_nid) && module_exists('sisstaff')) {
_sisstaff_load($account);
}
switch($op) {
case 'create': return user_access('create institute', $account);
case 'update': return user_access('edit institute', $account);
case 'delete': return user_access('delete institute', $account);
case 'view' : {
if (user_access('view all institutes', $account))
return TRUE;
elseif (user_access('view belonged institute', $account) && $account->sisinstitute_nid == $node->nid)
return TRUE;
else return FALSE;
}
}
}
/**
*hook_node_info()
*/
function sisinstitute_node_info() {
return array(
'sisinstitute' => array(
'name' => t('Institute'),
'module' => 'sisinstitute',
'description' => t("Institute for SIS"),
'title_label' => t("Name"),
'body_label' => t("Note"),
)
);
}
/**
*hook_form()
*/
function sisinstitute_form(&$node) {
$type = node_get_types('type', $node);
//$form['#attributes']['class'] = 'sismcomponent_node_form';
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
// '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'title') : -18,
);
$form['isactive'] = array(
'#type' => 'checkbox',
'#title' => t('Active'),
'#default_value' => $node->isactive,
);
return $form;
}
Hmm Got it:-)(after 8 hours)
function sisinstitute_access($op, $node. $account=NULL) {
has a period instead of a comma after $node

Resources