Jump to content

beginnerForPHP

New Members
  • Posts

    6
  • Joined

  • Last visited

Everything posted by beginnerForPHP

  1. I have a start period and end period selection which displays the result based on the start and end period. The function call is having redundant data. How can i improve it ? Also i have a bug in the code. In the edit form i am able to see the already saved start , end and payment days. When i change the start or end period already existing data is lost. Is there a better way to fix this as well? <date-monthly type=“text” name=“start_period” v-model=“startPeriod” @on-change=“getStart” required > <date-monthly type=“text” name=“end_period” v-model=“endPeriod” @on-change=“getEnd” required > <script> new Vue({ el: 'div#payment', name: 'appPayment', data: () => ({ startPeriod: @json(old('start_period', $paymentCalendar->start_period->format('Y-m-d'))), endPeriod: @json(old('end_period', $paymentCalendar->end_period->format('Y-m-d'))), paymentDays: @json(old('paymentDays', $paymentCalendar->paymentDays->keyBy('payment_period'))), }), methods: { getStart(_, startDate){ let dateObj = {}; let currentDate = moment(startDate); const endDate = moment(this.endPeriod); while (currentDate <= endDate) { dateObj[`${moment(currentDate).format('YYYY-MM-01')}`] = 0; currentDate = moment(currentDate).add(1, 'days'); } const output = Object.entries(dateObj).map(([payment_period]) => ({ payment_period, })); this.paymentDays = output; }, getEnd(_, endDate) { let dateObj = {}; let currentDate = moment(this.startPeriod); const endDateTemp = moment(endDate); while (currentDate <= endDateTemp) { dateObj[`${moment(currentDate).format('YYYY-MM-01')}`] = 0; currentDate = moment(currentDate).add(1, 'days'); } const output = Object.entries(dateObj).map(([payment_period]) => ({ payment_period, })); this.paymentDays = output; }, }, }) </script> Note: the onchange of my custom date picker is having parameter as follows: this.$emit(‘on-change’, $event, $date, $instance). So only the second param is date. So is there a better way to remove the duplicate code in my getStart and getEnd functions? Screenshot is as follows:
  2. How do I get a range of dates between the start and end dates using vue? My jsfiddle is as follows: i need to get the proper range of dates instead of the hard coded values in my jsfiddle. Any help is appreciated. https://jsfiddle.net/4b0z1exr/3/
  3. <?php $test['0000'] = ['address' =>'tes0']; $test['1111'] = ['id'=>'tes1', 'name'=>'tes11', 'address' =>'tes111']; $test['2222'] = ['id'=>'tes2', 'name'=>'tes22', 'address' =>'tes222']; $test['3333'] = ['id'=>'tes3', 'name'=>'tes33']; $keys = []; foreach ($test as $t) { $keys = array_merge($keys, array_keys($t)); } $keys = array_fill_keys(array_unique($keys), 'NIL'); foreach ($test as &$t) { $t = array_merge($keys, $t); } print_r($test); Is there a more better way to reduce the above code to fill in the missing keys and get the expected result as: Array ( [0000] => Array ( [id] => NIL [name] => NIL [address] => tes0 ) [1111] => Array ( [id] => tes1 [name] => tes11 [address] => tes111 ) [2222] => Array ( [id] => tes2 [name] => tes22 [address] => tes222 ) [3333] => Array ( [id] => tes3 [name] => tes33 [address] => NIL ) )
  4. @BarandHow do i use the range() in case if my array is as follows with time range or is there any other alternative as time difference can be based on hour and half-hour. In example it is based on every hour.
  5. $test['0000'] = [2 =>'tes000']; $test['1111'] = [0=>'tes1', 1=>'tes11', 2 =>'tes111']; $test['2222'] = [0=>'tes2', 1=>'tes22', 2 =>'tes333']; How do i prefill the $test array with missing keys with value as 'NIL' to get the result as : $test['0000'] = [0=>'NIL', 1=>'NIL', 2 =>'tes000']; $test['1111'] = [0=>'tes1', 1=>'tes11', 2 =>'tes111']; $test['2222'] = [0=>'tes2', 1=>'tes22', 2 =>'tes333'];
  6. I would like to read from an existing .csv file and create and write the contents in a different format to a new test.csv.gz file. private function createFile($id, $date, $file) { if(file_exists($file)){ $outputDirHandle = gzopen($file, 'r'); $newfile = $id . DIRECTORY_SEPARATOR . $date . DIRECTORY_SEPARATOR.'test.csv.gz'; $handle = gzopen($newfile, 'w'); $temp = []; while (! gzeof($outputDirHandle) && $values = fgetcsv($outputDirHandle)) { foreach($values as $key => $val) { $data = explode(';', $val); } $temp[$data[0]][] = $data[2]; } gzclose($handle); } } The content of the csv file that i am reading is in the following format(date1;hours;price): The content of the csv file that i am reading is in the following format: 2021-06-30;01:00:00;78 2021-06-30;02:00:00;75.9 2021-06-30;03:00:00;72 2021-06-30;04:00:00;70 2021-06-30;05:00:00;78 2021-06-30;06:00:00;84 2021-06-30;07:00:00;92 2021-06-30;08:00:00;99.7 2021-06-30;09:00:00;101.35 2021-06-30;10:00:00;99.2 2021-06-30;11:00:00;96.6 2021-06-30;12:00:00;95.4 2021-06-30;13:00:00;86 2021-06-30;14:00:00;79.5 2021-06-30;15:00:00;81 2021-06-30;16:00:00;90 2021-06-30;17:00:00;100.9 2021-06-30;18:00:00;102 2021-06-30;19:00:00;97.9 2021-06-30;20:00:00;87 2021-06-30;21:00:00;85 2021-06-30;22:00:00;80.1 2021-06-30;23:00:00;87.4 I need to read the above file and write to test.csv.gz in the below format. (date1;price;price;price.....price) 2021-06-30;78;75.9;72;70;78;84;92;99.7;101.35;99.2;96.6;95.4;86;79.5;81;90;100.9;102;97.9;87;85;80.1;87.4 Any help is appreciated.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.