I would like to post a value to my MySQL database in this format;
01, 02, 03...
011, 012, 013...
0101, 0102, 0103,
etc.
(with a 0 before each value).
If I do it like this "01+1" I (understandable) get the value 2 and not "02".
is there a default php function that let's me set the default amount of characters or is there an other option?
I'm using laravel combined with eloquent and some own functions like so;
$lastSKU = $data->GetData('get', 'products', 'filter')->max('SKU');
$newValue = $lastSKU+1;
Thanks in advance.
It sounds like you won't know how the format will be, if there is always a 0 in front it is easy enough to do
$value = '0'. ( $value + 1 );
PHP will automatically convert it to a number between the brackets and by adding a string to it it will become a string again.
Now if you do not know the the length or the padding in advance but you do need to keep the current padding I would suggest something like
$value = str_pad(($value + 1), strlen($value), '0', STR_PAD_LEFT);
Edit, str_pad is a bit slow these days, sprintf can do the same trick but much faster
$value = sprintf("%'.0" . strlen($value) . "d\n", ($value +1));
In my database field i have varchar field like this format HPDO1209180000.What i am doing is if i dont have any data i am adding four digits at last it is working fine.And if if it is there i am incrementing one each time.but the problem is after 0001 also incrementing all the 0001 only not 0002 like that.I am doing like this please help me.thanks in Advance.
$this->db->select('grmno');
$this->db->from('procurement_receive_material');
$this->db->where('DATE(createdon)',$currentdate);
$this->db->where('SUBSTRING(grmno,1,10)',$grn_code);
$this->db->order_by('prmid','desc');
$query = $this->db->get();
if($query->num_rows()>0){
$output = $query->row();
$grmNumber = $output->grmno;
//after 0001 also it incrementing 0001 not incrementing;
$inrno=str_pad((int)$grmNumber+1, 4, 0, STR_PAD_LEFT);
$grmNumber=$vendor_result.$branch_result.$currentDate.$currentmonth.$currentyear.$inrno;
}else{
$grmNumber = $vendor_result.$branch_result.$currentDate.$currentmonth.$currentyear.'0000';
}
If I'm reading this correctly, it looks like this line:
$inrno=str_pad((int)$grmNumber+1, 4, 0, STR_PAD_LEFT);
Is taking the int value of $output->grmno. And this field, again if I'm interpreting your problem directly, can be evaluated as something like this:
HPDO1209180001
The integer value of such a string would be 0. Hence, why they always end up as 0001.
To fix this, you need to grab the last 4 characters of the string, and increment only that. For example:
$suffix = substr($grmNumber, -4);
$newsuffix = intval($suffix) + 1;
$irno = str_pad($newsuffix, 4, 0, STR_PAD_LEFT);
This would properly grab the last 4 digits, add one to the value, and again pad it with zeros to be added to the new string.
I've been searching for a way to count the amount of numbers in one row column spaced by ",". (But nothing found, I use MySQL and what to count with the use of PHP) This is going to be done for whole the database to find the total amount of numbers (by 100'reds). What I do is storing multiple numbers like the following.
100, 200, 300, 400
What I want is to get the amount of numbers in this column to be 4 and add this to the next row column number. Which could be
200
and therefore equal to 1 + 4 = 5.
To show it the database way see here
See database sample if it helps
The total number should then endup beeing 6.
I want to hear if it is possible in someway to do this?
Thanks in advance :)
You can write code as:-
<?php
$rows = array( '303, 117, 210, 211', '117', '444');
$count = 0;
foreach( $rows as $row ) {
$count += count(explode(',', $row));
}
echo "total : " . $count;
?>
To count the amount of numbers in this column you could use following:
$count_of_numbers = sizeof(explode(",",$row['yourColumn']));
Now add em up and you are done ;-)
Ok, so the question is kind of awkwardly phrased, but I hope this will clear things up.
I have this sample 2d array.
$array = array(
array(1, 0, 0, 0, 1, 0, 0, 1),
array(0, 0, 1, 1, 1, 1, 0, 1),
array(0, 1, 1, 0, 1, 0, 0, 0),
array(0, 1, 1, 0, 0, 0, 1, 0),
array(1, 0, 0, 0, 1, 1, 1, 1),
array(0, 1, 1, 0, 1, 0, 1, 0),
array(0, 0, 0, 0, 0, 0, 0, 1)
);
When iterated by rows (and terminating each row with \n), and for every row then iterated by column, it will echo something like this: (░░ = 0, ▓▓ = 1)
▓▓░░░░░░▓▓░░░░▓▓
░░░░▓▓▓▓▓▓▓▓░░▓▓
░░▓▓▓▓░░▓▓░░░░░░
░░▓▓▓▓░░░░░░▓▓░░
▓▓░░░░░░▓▓▓▓▓▓▓▓
░░▓▓▓▓░░▓▓░░▓▓░░
░░░░░░░░░░░░░░▓▓
But what I'd like to do is to "analyse" the array and only leave 1 contiguous shape (the one with the most "cells"), in this example, the result would be:
░░░░░░░░▓▓░░░░░░
░░░░▓▓▓▓▓▓▓▓░░░░
░░▓▓▓▓░░▓▓░░░░░░
░░▓▓▓▓░░░░░░░░░░
▓▓░░░░░░░░░░░░░░
░░▓▓▓▓░░░░░░░░░░
░░░░░░░░░░░░░░░░
My initial approach was to:
Assign each ▓▓ cell a unique number (be it completely random, or the current iteration number):
01 02 03
04050607 08
0910 11
1213 14
15 16171819
2021 22 23
24
Iterate through the array many, MANY times: every iteration, each ▓▓ cell assumes the largest unique number among his neighbours. The loop would go on indefinitely until there's no change detected between the current state and the previous state. After the last iteration, the result would be this:
01 21 08
21212121 08
2121 21
2121 24
21 24242424
2121 24 24
24
Now it all comes down to counting the value that occurs the most. Then, iterating once again, to turn all the cells whose value is not the most popular one, to 0, giving me the desired result.
However, I feel it's quite a roundabout and computationally heavy approach for such a simple task and there has to be a better way. Any ideas would be greatly appreciated, cheers!
BONUS POINTS: Divide all the blobs into an array of 2D arrays, ordered by number of cells, so we can do something with the smallest blob, too
Always fun, these problems. And done before, so I'll dump my code here, maybe you can use some of it. This basically follows every shape by looking at a cell and its surrounding 8 cells, and if they connect go to the connecting cell, look again and so on...
<?php
$shape_nr=1;
$ln_max=count($array);
$cl_max=count($array[0]);
$done=[];
//LOOP ALL CELLS, GIVE 1's unique number
for($ln=0;$ln<$ln_max;++$ln){
for($cl=0;$cl<$cl_max;++$cl){
if($array[$ln][$cl]===0)continue;
$array[$ln][$cl] = ++$shape_nr;
}}
//DETECT SHAPES
for($ln=0;$ln<$ln_max;++$ln){
for($cl=0;$cl<$cl_max;++$cl){
if($array[$ln][$cl]===0)continue;
$shape_nr=$array[$ln][$cl];
if(in_array($shape_nr,$done))continue;
look_around($ln,$cl,$ln_max,$cl_max,$shape_nr,$array);
//SET SHAPE_NR to DONE, no need to look at that number again
$done[]=$shape_nr;
}}
//LOOP THE ARRAY and COUNT SHAPENUMBERS
$res=array();
for($ln=0;$ln<$ln_max;++$ln){
for($cl=0;$cl<$cl_max;++$cl){
if($array[$ln][$cl]===0)continue;
if(!isset($res[$array[$ln][$cl]]))$res[$array[$ln][$cl]]=1;
else $res[$array[$ln][$cl]]++;
}}
//get largest shape
$max = max($res);
$shape_value_max = array_search ($max, $res);
//get smallest shape
$min = min($res);
$shape_value_min = array_search ($min, $res);
// recursive function: detect connecting cells
function look_around($ln,$cl,$ln_max,$cl_max,$nr,&$array){
//create mini array
$mini=mini($ln,$cl,$ln_max,$cl_max);
if($mini===false)return false;
//loop surrounding cells
foreach($mini as $v){
if($array[$v[0]][$v[1]]===0){continue;}
if($array[$v[0]][$v[1]]!==$nr){
// set shape_nr of connecting cell
$array[$v[0]][$v[1]]=$nr;
// follow the shape
look_around($v[0],$v[1],$ln_max,$cl_max,$nr,$array);
}
}
return $nr;
}
// CREATE ARRAY WITH THE 9 SURROUNDING CELLS
function mini($ln,$cl,$ln_max,$cl_max){
$look=[];
$mini=[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]];
foreach($mini as $v){
if( $ln + $v[0] >= 0 &&
$ln + $v[0] < $ln_max &&
$cl + $v[1] >= 0 &&
$cl + $v[1] < $cl_max
){
$look[]=[$ln + $v[0], $cl + $v[1]];
}
}
if(count($look)===0){return false;}
return $look;
}
Here's a fiddle
I can only think of a few minor improvements:
Keep a linked list of the not empty fields. In step 2 you do not need to touch n² matrix-elements, you only need to touch the ones in your linked list. Which might be much less depending how sparse your matrix is.
You only need to compare to the right, right-down, left-down and down directions. Otherwise The other directions are already checked from the former row/column. What I mean: When I am greater that my right neighbour, I can already change the number of the right neighbour. (same for down and right-down). This halfs the number of compairs.
If your array size isn't huge and memory won't be a problem maybe a recursive solution would be faster. I found a c++ algorithm that does this here:
https://www.geeksforgeeks.org/find-length-largest-region-boolean-matrix/
Can somebody tell me please how to generate column indexes (like BB) if the number of cells is optional? Currently my code explicitly sets the cell like
$list->setCellValue("D1", "Date"));
but is there a way to generate column index "D" automatically or not? I would like to have col index like GA it means from A to Z and than double the char part if columns number exceeds the range of A-Z. Is PHPExcel able to generate this indexes automatically or not?
$colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
Instead of using $cell->getColumn() you can set your letter manually
So for now I has to write a function to do this:
public static function indexToExcelIndex( $index )
{
$div = intdiv( $index, 26 );
$modulo = $index % 26;
$result = '';
do
{
$result .= chr( 65 + $modulo );
}
while( --$div > -1 );
return $result;
}
EDIT: As find out there is a method setCellValueByColumnAndRow() which accepts numeric coordinates of col and row. So I dont need alphabetical coordinates.