Generate ean13 barcode and save it to png - php

I am using Kody kreskowe - EAN-13 by Jacek Kowalski (http://jacekk.info) to generate EAN13 barcode and save it to image file. I would like generate barcode dynamically during the ordering process in Prestashop. Save that image on server and put it in new_order.html email template.
There is whole code: https://jacekk.info/skrypty/ean13.phps
I modified it lightly by change $_GET['kod'] to $kod_in and put $kod_in = 1234567891011 at start of file and imagepng($i, $new_filename); at the end
Everything works well (I see generated barcode and script create new image file and save it on server) when I enter directly to file ean13.php or if I run that code (directly in web browser):
$kod_in = 1234567891011;
include (dirname(__FILE__)."/ean13.php");
But when I try include the above code into order process, exactly in mailalerts.php, it does not display barcode and generate blank (white) image file. I think it has something to do with the fact that the generated barcode is not displayed on the screen, and then is saved.
Please help modify the code to generate the images even though they are not displayed on the screen.

At the end of the script replace this:
header('Content-type: image/gif');
imagegif($i);
With this:
$imageLocation = "where/you/want/to/save/your/file";
imagegif($i, $imageLocation);
Here is imagegif Documentation
You can use this library as a Class this way:
<?php
/***************************************************
* Kody kreskowe - EAN-13 *
***************************************************
* Ostatnia modyfikacja: 01.11.2012 *
* Autor: Jacek Kowalski (http://jacekk.info) *
* *
* Strona WWW: http://jacekk.info/scripts/barcodes *
* *
* Utwór rozprowadzany na licencji *
* http://creativecommons.org/licenses/by-nc/2.5/ *
***************************************************/
/* Kodowanie znaków UTF-8 */
class BarCode {
public $kol = array(
'0' => array('A', 'A', 'A', 'A', 'A', 'A'),
'1' => array('A', 'A', 'B', 'A', 'B', 'B'),
'2' => array('A', 'A', 'B', 'B', 'A', 'B'),
'3' => array('A', 'A', 'B', 'B', 'B', 'A'),
'4' => array('A', 'B', 'A', 'A', 'B', 'B'),
'5' => array('A', 'B', 'B', 'A', 'A', 'B'),
'6' => array('A', 'B', 'B', 'B', 'A', 'A'),
'7' => array('A', 'B', 'A', 'B', 'A', 'B'),
'8' => array('A', 'B', 'A', 'B', 'B', 'A'),
'9' => array('A', 'B', 'B', 'A', 'B', 'A')
);
public $code = array(
'start' => '101',
'lewa' => array(
'A' => array(
'0' => '0001101',
'1' => '0011001',
'2' => '0010011',
'3' => '0111101',
'4' => '0100011',
'5' => '0110001',
'6' => '0101111',
'7' => '0111011',
'8' => '0110111',
'9' => '0001011'
),
'B' => array(
'0' => '0100111',
'1' => '0110011',
'2' => '0011011',
'3' => '0100001',
'4' => '0011101',
'5' => '0111001',
'6' => '0000101',
'7' => '0010001',
'8' => '0001001',
'9' => '0010111'
)
),
'srodek' => '01010',
'prawa' => array(
'0' => '1110010',
'1' => '1100110',
'2' => '1101100',
'3' => '1000010',
'4' => '1011100',
'5' => '1001110',
'6' => '1010000',
'7' => '1000100',
'8' => '1001000',
'9' => '1110100'
),
'stop' => '101'
);
public $b;
public function __construct($barcode) {
$len = strlen($barcode);
if(trim($barcode, '0123456789')!='' OR ($len!=12 AND $len!=13)) {
echo 'Znaki inne niż cyfry lub błędna długość ('.$len.')';
die();
}
$kod = str_split(substr($barcode, 0, 12));
$now = 1;
$sum = 0;
foreach($kod as $val) {
if($now==1) {
$sum += $val;
$now = 3;
}
else
{
$sum += $val*3;
$now = 1;
}
}
$sum = 10-($sum%10);
if($sum==10) $sum = 0;
if($len==12) {
$barcode .= $sum;
}
elseif(substr($barcode, -1)!=$sum) {
echo 'Błędna suma kontrolna '.$sum;
die();
}
unset($len, $kod, $now, $sum);
$sys = substr($barcode, 0, 1);
$lewa = substr($barcode, 1, 6);
$prawa = substr($barcode, 7);
$i = imagecreate(95, 40);
$w = imagecolorallocate($i, 255, 255, 255);
$this->b = imagecolorallocate($i, 0, 0, 0);
$this->print_code($this->code['start'].$this->gen_binary($lewa, 0, $sys).$this->code['srodek'].$this->gen_binary($prawa, 1, $sys).$this->code['stop'], $i);
imagegif($i, 'test.gif');
}
public function gen_binary($kod, $strona, $sys) {
$kod = str_split($kod);
$ret = '';
if($strona==0) {
foreach($kod as $key => $val) {
$ret .= $this->code['lewa'][$this->kol[$sys][$key]][$val];
}
}
else
{
foreach($kod as $val) {
$ret .= $this->code['prawa'][$val];
}
}
return $ret;
}
public function print_code($kod, $img) {
$now = 0;
$kod = str_split($kod);
foreach($kod as $val) {
if($val==1) {
imageline($img, $now, 0, $now, 40, $this->b);
$now++;
}
elseif($val==0) {
$now++;
}
}
}
}
You need to include it once at the top of your file before your class declaration:
include_once('path/to/BarCode.php');
Now instead of including the script to generate an image you'll have to create a new BarCode Object.
new BarCode('9780486425573');
Tested and working on my Prestashop 1.6.

Related

PHP - How do you find duplicate value groupings in an array

I have an array of string values which sometimes form repeating value patterns ('a', 'b', 'c', 'd')
$array = array(
'a', 'b', 'c', 'd',
'a', 'b', 'c', 'd',
'c', 'd',
);
I would like to find duplicate patterns based on the array order and group them by that same order (to maintain it).
$patterns = array(
array('number' => 2, 'values' => array('a', 'b', 'c', 'd')),
array('number' => 1, 'values' => array('c'))
array('number' => 1, 'values' => array('d'))
);
Notice that [a,b], [b,c], & [c,d] are not patterns by themselves because they are inside the larger [a,b,c,d] pattern and the last [c,d] set only appears once so it's not a pattern either - just the individual values 'c' and 'd'
Another example:
$array = array(
'x', 'x', 'y', 'x', 'b', 'x', 'b', 'a'
//[.......] [.] [[......] [......]] [.]
);
which produces
$patterns = array(
array('number' => 2, 'values' => array('x')),
array('number' => 1, 'values' => array('y')),
array('number' => 2, 'values' => array('x', 'b')),
array('number' => 1, 'values' => array('a'))
);
How can I do this?
Character arrays are just strings. Regex is the king of string pattern matching. Add recursion and the solution is pretty elegant, even with the conversion back and forth from character arrays:
function findPattern($str){
$results = array();
if(is_array($str)){
$str = implode($str);
}
if(strlen($str) == 0){ //reached the end
return $results;
}
if(preg_match_all('/^(.+)\1+(.*?)$/',$str,$matches)){ //pattern found
$results[] = array('number' => (strlen($str) - strlen($matches[2][0])) / strlen($matches[1][0]), 'values' => str_split($matches[1][0]));
return array_merge($results,findPattern($matches[2][0]));
}
//no pattern found
$results[] = array('number' => 1, 'values' => array(substr($str, 0, 1)));
return array_merge($results,findPattern(substr($str, 1)));
}
You can test here : https://eval.in/507818 and https://eval.in/507815
If c and d can be grouped, this is my code:
<?php
$array = array(
'a', 'b', 'c', 'd',
'a', 'b', 'c', 'd',
'c', 'd',
);
$res = array();
foreach ($array AS $value) {
if (!isset($res[$value])) {
$res[$value] = 0;
}
$res[$value]++;
}
foreach ($res AS $key => $value) {
$fArray[$value][] = $key;
for ($i = $value - 1; $i > 0; $i--) {
$fArray[$i][] = $key;
}
}
$res = array();
foreach($fArray AS $key => $value) {
if (!isset($res[serialize($value)])) {
$res[serialize($value)] = 0;
}
$res[serialize($value)]++;
}
$fArray = array();
foreach($res AS $key => $value) {
$fArray[] = array('number' => $value, 'values' => unserialize($key));
}
echo '<pre>';
var_dump($fArray);
echo '</pre>';
Final result is:
array (size=2)
0 =>
array (size=2)
'number' => int 2
'values' =>
array (size=4)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'd' (length=1)
1 =>
array (size=2)
'number' => int 1
'values' =>
array (size=2)
0 => string 'c' (length=1)
1 => string 'd' (length=1)
The following code will return the expected result, finding the longest portions with repeated values:
function pepito($array) {
$sz=count($array);
$patterns=Array();
for ($pos=0;$pos<$sz;$pos+=$len) {
$nb=1;
for ($len=floor($sz/2);$len>0;$len--) {
while (array_slice($array, $pos, $len)==array_slice($array, $pos+$len, $len)) {
$pos+=$len;
$nb++;
}
if ($nb>1) break;
}
if (!$len) $len=1;
$patterns[]=Array('number'=>$nb, 'values'=>array_slice($array, $pos, $len));
}
return $patterns;
}
This will match with your examples:
{['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd']}, ['c', 'd']
or {['x'], ['x']}, ['y'], {['x', 'b'], ['x', 'b']}, ['a']
The difficult part is more about examples like:
{['one', 'one', 'two'], ['one', 'one', 'two']}
Or the most difficult choice to make:
one, two, one, two, one, two, one, two
Because we can group this in both the form:
[one, two], [one, two], [one, two], [one, two]
[one, two, one, two], [one, two, one, two]
where there is no "evident" choice. My above algorithm will always consider the longest match, as this is the most easy implementation to consider any combination.
EDIT: You should also consider cases where the longest match is after a shorter one:
Example:
'one', 'two', 'one', 'two', 'three', 'four', 'one', 'two', 'three', 'four'
If you start from left to right, you might want to group as :
{['one', 'two'], ['one', 'two'],} 'three', 'four', 'one', 'two', 'three', 'four'
when you could group like:
'one', 'two', {['one', 'two', 'three', 'four'], ['one', 'two', 'three', 'four']}
This situation has to be resolved with recursive calls to get the better solution but this will result in longer execution time:
function pepito($array) {
if (($sz=count($array))<1) return Array();
$pos=0;
$nb=1;
for ($len=floor($sz/2);$len>0;$len--) {
while (array_slice($array, $pos, $len)==array_slice($array, $pos+$len, $len)) {
$pos+=$len;
$nb++;
}
if ($nb>1) break;
}
if (!$len) $len=1;
$rec1=pepito(array_slice($array, $pos+$len));
$rec2=pepito(array_slice($array, 1));
if (count($rec1)<count($rec2)+1) {
return array_merge(Array(Array('number'=>$nb, 'values'=>array_slice($array, $pos, $len))), $rec1);
}
return array_merge(Array(Array('number'=>1, 'values'=>array_slice($array, 0, 1))), $rec2);
}
Definitions:
Pattern base: The sequence of elements that repeat within a pattern. (ie. For [a,b,a,b,c], [a,b] is the pattern base and [a,b,a,b] is the pattern.
We want to start searching for the longest pattern base, followed by the next longest, and so forth. It's important to understand that if we find a pattern, we don't need to check within it for the start of another pattern with a base of the same length.
Here's the proof.
Assume that A is a pattern base, and that we've encountered the pattern AA. Assume that B is a another pattern base, of the same length, that forms a pattern starting within A. Let Y be the overlapping elements. If A=XY, then AA=XYXY. Since B is the same length, it must be the case that B=YX because in order to complete B we must use the remaining elements in A. Moreover, since B forms a pattern we must have BB, which is YXYX. Since A starts before B, we have XYXYX=AAX=XBB. If B repeats again, we would have XBBB=XYXYXYX=AAAX. Therefore, B cannot repeat an additional time without A repeating an additional time. Thus, we do not need to check for longer patterns within those generated by A.
The longest pattern possible consists of half the elements in the whole list because the simplest pattern can occur exactly twice. Thus, we can start checking for patterns of half of the length and work our way down to patterns of size 2.
Assuming that we search the array from left to right, if a pattern is found, we only need to search on either side of it for additional patterns. To the left, there are no patterns with bases of the same length, or they would have been discovered beforehand. Thus, we search the left side for patterns using the next smallest base size. The elements to the right of the pattern haven't been searched so we continue searching for patterns using a base of the same size.
The function to do this is as follows:
function get_patterns($arr, $len = null) {
// The smallest pattern base length for which a pattern can be found
$minlen = 2;
// No pattern base length was specified
if ($len === null) {
// Use the longest pattern base length possible
$maxlen = floor(count($arr) / 2);
return get_patterns($arr, $maxlen);
// Base length is too small to find any patterns
} else if ($len < $minlen) {
// Compile elements into lists consisting of one element
$results = array();
$num = 1;
$elem = $arr[0];
for ($i=1; $i < count($arr); $i++) {
if ($elem === $arr[$i]) {
$num++;
} else {
array_push($results, array(
'number' => $num,
'values' => array( $elem )
));
$num = 1;
$elem = $arr[$i];
}
}
array_push($results, array(
'number' => $num,
'values' => array( $elem )
));
return $results;
}
// Cycle through elements until there aren't enough elements to fit
// another repition.
for ($i=0; $i < count($arr) - $len * 2 + 1; $i++) {
// Number of times pattern base occurred
$num_read = 1; // One means there is no pattern yet
// Current pattern base we are attempting to match against
$base = array_slice($arr, $i, $len);
// Check for matches using segments of the same length for the elements
// following the current pattern base
for ($j = $i + $len; $j < count($arr) - $len + 1; $j += $len) {
// Elements being compared to pattern base
$potential_match = array_slice($arr, $j, $len);
// Match found
if (has_same_elements($base, $potential_match)) {
$num_read++;
// NO match found
} else {
// Do not check again using currently selected elements
break;
}
}
// Patterns were encountered
if ($num_read > 1) {
// The total number of elements that make up the pattern
$pattern_len = $num_read * $len;
// The elements before the pattern
$before = array_slice($arr, 0, $i);
// The elements after the pattern
$after = array_slice(
$arr, $i + $pattern_len, count($arr) - $pattern_len - $i
);
$results = array_merge(
// Patterns of a SMALLER length may exist beforehand
count($before) > 0 ? get_patterns($before, $len-1) : array(),
// Patterns that were found
array(
array(
'number' => $num_read,
'values' => $base
)
),
// Patterns of the SAME length may exist afterward
count($after) > 0 ? get_patterns($after, $len) : array()
);
return $results;
}
}
// No matches were encountered
// Search for SMALLER patterns
return get_patterns($arr, $len-1);
}
The function has_same_elements, which was used to check if arrays with primitive keys are identical, is as follows:
// Returns true if two arrays have the same elements.
//
// Precondition: Elements must be primitive data types (ie. int, string, etc)
function has_same_elements($a1, $a2) {
// There are a different number of elements
if (count($a1) != count($a2)) {
return false;
}
for ($i=0; $i < count($a1); $i++) {
if ($a1[$i] !== $a2[$i]) {
return false;
}
}
return true;
}
In order to speed up the code, there are a few things that you could do. Instead of slicing the array, you can supply the function with indexes to the start and end position that are to be examined, along with the array. Also, using strings may be slow so you can create an array that maps strings to numbers and vice versa. Then you can convert the array of strings into an array of numbers and use it instead. After you get the result, you can convert the arrays of numbers back into strings.
I tested the function using the following code:
$tests = array(
'a,b,c,d',
'a',
'a,a,a,a',
'a,a,a,a,a',
'a,a,a,a,a,a',
'b,a,a,a,a,c',
'b,b,a,a,a,a,c,c',
'b,b,a,a,d,a,a,c,c',
'a,b,c,d,a,b,c,d,c,d',
'x,x,y,x,b,x,b,a'
);
echo '<pre>';
foreach ($tests as $test) {
echo '<div>';
$arr = explode(',',$test);
echo "$test<br /><br />";
pretty_print(get_patterns($arr));
echo '</div><br />';
}
echo '</pre>';
The function that I used to print the output, pretty_print is as follows:
function pretty_print($results) {
foreach ($results as $result) {
$a = "array('" . implode("','", $result['values']) . "')";
echo "array('number' => ${result['number']}, 'values' => $a)<br />";
}
}
The output from the test code is as follows:
a,b,c,d
array('number' => 1, 'values' => array('a'))
array('number' => 1, 'values' => array('b'))
array('number' => 1, 'values' => array('c'))
array('number' => 1, 'values' => array('d'))
a
array('number' => 1, 'values' => array('a'))
a,a,a,a
array('number' => 2, 'values' => array('a','a'))
a,a,a,a,a
array('number' => 2, 'values' => array('a','a'))
array('number' => 1, 'values' => array('a'))
a,a,a,a,a,a
array('number' => 2, 'values' => array('a','a','a'))
b,a,a,a,a,c
array('number' => 1, 'values' => array('b'))
array('number' => 2, 'values' => array('a','a'))
array('number' => 1, 'values' => array('c'))
b,b,a,a,a,a,c,c
array('number' => 2, 'values' => array('b'))
array('number' => 2, 'values' => array('a','a'))
array('number' => 2, 'values' => array('c'))
b,b,a,a,d,a,a,c,c
array('number' => 2, 'values' => array('b'))
array('number' => 2, 'values' => array('a'))
array('number' => 1, 'values' => array('d'))
array('number' => 2, 'values' => array('a'))
array('number' => 2, 'values' => array('c'))
a,b,c,d,a,b,c,d,c,d
array('number' => 2, 'values' => array('a','b','c','d'))
array('number' => 1, 'values' => array('c'))
array('number' => 1, 'values' => array('d'))
x,x,y,x,b,x,b,a
array('number' => 2, 'values' => array('x'))
array('number' => 1, 'values' => array('y'))
array('number' => 2, 'values' => array('x','b'))
array('number' => 1, 'values' => array('a'))
OK, here is my take, the code below splits the whole original array into longest adjacent non-overlapping chunks.
So in the situation like this
'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'c', 'd'
[ ] [ ] [ ] [ ] <-- use 2 long groups
[ ] [ ] [ ] [ ] [ ] [ ] <-- and not 4 short
it will prefer 2 long groups to 4 shorter groups.
Update: also tested with examples from another answer, works for these cases too:
one, two, one, two, one, two, one, two
[one two one two], [one two one two]
'one' 'two' 'one' 'two' 'three' 'four' 'one' 'two' 'three' 'four'
['one'] ['two'] ['one' 'two' 'three' 'four'] ['one' 'two' 'three' 'four']
Here is the code and tests:
<?php
/*
* Splits an $array into chunks of $chunk_size.
* Returns number of repeats, start index and chunk which has
* max number of ajacent repeats.
*/
function getRepeatCount($array, $chunk_size) {
$parts = array_chunk($array, $chunk_size);
$maxRepeats = 1;
$maxIdx = 0;
$repeats = 1;
$len = count($parts);
for ($i = 0; $i < $len-1; $i++) {
if ($parts[$i] === $parts[$i+1]) {
$repeats += 1;
if ($repeats > $maxRepeats) {
$maxRepeats = $repeats;
$maxIdx = $i - ($repeats-2);
}
} else {
$repeats = 1;
}
}
return array($maxRepeats, $maxIdx*$chunk_size, $parts[$maxIdx]);
}
/*
* Finds longest pattern in the $array.
* Returns number of repeats, start index and pattern itself.
*/
function findLongestPattern($array) {
$len = count($array);
for ($window = floor($len/2); $window >= 1; $window--) {
$num_chunks = ceil($len/$window);
for ($i = 0; $i < $num_chunks; $i++) {
list($repeats, $idx, $pattern) = getRepeatCount(
array_slice($array, $i), $window
);
if ($repeats > 1) {
return array($repeats, $idx+$i, $pattern);
}
}
}
return array(1, 0, [$array[0]]);
}
/*
* Splits $array into longest adjacent non-overlapping parts.
*/
function splitToPatterns($array) {
if (count($array) < 1) {
return $array;
}
list($repeats, $start, $pattern) = findLongestPattern($array);
$end = $start + count($pattern) * $repeats;
return array_merge(
splitToPatterns(array_slice($array, 0, $start)),
array(
array('number'=>$repeats, 'values' => $pattern)
),
splitToPatterns(array_slice($array, $end))
);
}
Tests:
function isEquals($expected, $actual) {
$exp_str = json_encode($expected);
$act_str = json_encode($actual);
$equals = $exp_str === $act_str;
if (!$equals) {
echo 'Equals check failed'.PHP_EOL;
echo 'expected: '.$exp_str.PHP_EOL;
echo 'actual : '.$act_str.PHP_EOL;
}
return $equals;
}
assert(isEquals(
array(1, 0, ['a']), getRepeatCount(['a','b','c'], 1)
));
assert(isEquals(
array(1, 0, ['a']), getRepeatCount(['a','b','a','b','c'], 1)
));
assert(isEquals(
array(2, 0, ['a','b']), getRepeatCount(['a','b','a','b','c'], 2)
));
assert(isEquals(
array(1, 0, ['a','b','a']), getRepeatCount(['a','b','a','b','c'], 3)
));
assert(isEquals(
array(3, 0, ['a','b']), getRepeatCount(['a','b','a','b','a','b','a'], 2)
));
assert(isEquals(
array(2, 2, ['a','c']), getRepeatCount(['x','c','a','c','a','c'], 2)
));
assert(isEquals(
array(1, 0, ['x','c','a']), getRepeatCount(['x','c','a','c','a','c'], 3)
));
assert(isEquals(
array(2, 0, ['a','b','c','d']),
getRepeatCount(['a','b','c','d','a','b','c','d','c','d'],4)
));
assert(isEquals(
array(2, 2, ['a','c']), findLongestPattern(['x','c','a','c','a','c'])
));
assert(isEquals(
array(1, 0, ['a']), findLongestPattern(['a','b','c'])
));
assert(isEquals(
array(2, 2, ['c','a']),
findLongestPattern(['a','b','c','a','c','a'])
));
assert(isEquals(
array(2, 0, ['a','b','c','d']),
findLongestPattern(['a','b','c','d','a','b','c','d','c','d'])
));
// Find longest adjacent non-overlapping patterns
assert(isEquals(
array(
array('number'=>1, 'values'=>array('a')),
array('number'=>1, 'values'=>array('b')),
array('number'=>1, 'values'=>array('c')),
),
splitToPatterns(['a','b','c'])
));
assert(isEquals(
array(
array('number'=>1, 'values'=>array('a')),
array('number'=>1, 'values'=>array('b')),
array('number'=>2, 'values'=>array('c','a')),
),
splitToPatterns(['a','b','c','a','c','a'])
));
assert(isEquals(
array(
array('number'=>2, 'values'=>array('a','b','c','d')),
array('number'=>1, 'values'=>array('c')),
array('number'=>1, 'values'=>array('d')),
),
splitToPatterns(['a','b','c','d','a','b','c','d','c','d'])
));
/* 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'c', 'd', */
/* [ ] [ ] [ ] [ ] */
/* NOT [ ] [ ] [ ] [ ] [ ] [ ] */
assert(isEquals(
array(
array('number'=>2, 'values'=>array('a','b','a','b')),
array('number'=>1, 'values'=>array('c')),
array('number'=>1, 'values'=>array('d')),
),
splitToPatterns(['a','b','a','b','a','b','a','b','c','d'])
));
/* 'x', 'x', 'y', 'x', 'b', 'x', 'b', 'a' */
/* // [ ] [ ] [ ] [ ] [ ] [ ] */
assert(isEquals(
array(
array('number'=>2, 'values'=>array('x')),
array('number'=>1, 'values'=>array('y')),
array('number'=>2, 'values'=>array('x','b')),
array('number'=>1, 'values'=>array('a')),
),
splitToPatterns(['x','x','y','x','b','x','b','a'])
));
// one, two, one, two, one, two, one, two
// [ ] [ ]
assert(isEquals(
array(
array('number'=>2, 'values'=>array('one', 'two', 'one', 'two')),
),
splitToPatterns(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])
));
// 'one', 'two', 'one', 'two', 'three', 'four', 'one', 'two', 'three', 'four'
// [ ] [ ] [ ] [ ]
assert(isEquals(
array(
array('number'=>1, 'values'=>array('one')),
array('number'=>1, 'values'=>array('two')),
array('number'=>2, 'values'=>array('one','two','three','four')),
),
splitToPatterns(['one', 'two', 'one', 'two', 'three', 'four', 'one', 'two', 'three','four'])
));
/* 'a', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'c', */
/* [ ] [ ] [ ] [ ] */
assert(isEquals(
array(
array('number'=>1, 'values'=>array('a')),
array('number'=>2, 'values'=>array('a','b','a','b')),
array('number'=>1, 'values'=>array('c')),
),
splitToPatterns(['a','a','b','a','b','a','b','a','b','c'])
));
/* 'a', 'b', 'a', 'b', 'c', 'd', 'a', 'b', 'a', 'b', 'a', 'b' */
// [ ] [ ] [ ] [ ] [ ] [ ] [ ]
assert(isEquals(
array(
array('number'=>2, 'values'=>array('a', 'b')),
array('number'=>1, 'values'=>array('c')),
array('number'=>1, 'values'=>array('d')),
array('number'=>3, 'values'=>array('a','b')),
),
splitToPatterns(['a', 'b', 'a', 'b', 'c', 'd', 'a', 'b', 'a', 'b', 'a', 'b'])
));
/* 'a', 'c', 'd', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'c', */
/* [ ] [ ] [ ] [ ] [ ] [ ] */
assert(isEquals(
array(
array('number'=>1, 'values'=>array('a')),
array('number'=>2, 'values'=>array('a','b','a','b')),
array('number'=>1, 'values'=>array('c')),
),
splitToPatterns(['a','a','b','a','b','a','b','a','b','c'])
));
I started with this now but at the end my brain burn and I don't know where to start to compare the arrays... Enjoy!
$array = array(
'x', 'x', 'y', 'x', 'b', 'x', 'b', 'a'
//[.......] [.] [[......] [......]] [.]
);
$arrayCount = count($array);
$res = array();
for($i = 0; $i < $arrayCount; $i++) {
for($j = 1; $j < $arrayCount; $j++) {
$res[$i][] = array_slice($array, $i, $j);
}
}
//echo '<pre>';
//var_dump($res);
//echo '</pre>';
//
//die;
$resCount = count($res);
$oneResCount = count($res[0]);
First make a function which will find the possible group matches in the array for a given group array, starting from a specific index in the array and will return the number of matches found.
function findGroupMatch($group, $array, $startFrom) {
$match = 0;
while($group == array_slice($array, $startFrom, count($group))) {
$match++;
$startFrom += count($group);
}
return $match;
}
Now, we need to iterate through each item to find possible groups and then send it to findGroupMatch() function to check if any match for the group exists in the next items. The trick to find a possible group is finding an item which matches any of the previous items. If so, we find a possible group taking all the previous items starting from the matched item. Otherwise we just increase the list of unmatched items and at the end we enter all unmatched items as single item groups. (In the given example, we have a, b, c, d, a.... When we find 2nd a in the array, it matches the previous a, So, we consider a, b, c, d a possible group and send it to function findGroupMatch(), to check how many more groups we can find in the next items.)
$array = array(
'a', 'b', 'c', 'd',
'a', 'b', 'c', 'd',
'c', 'd',
);
$unMatchedItems = array();
$totalCount = count($array);
$groupsArray = array();
for($i=0; $i < $totalCount; $i++) {
$item = $array[$i];
if(in_array($item, $unMatchedItems)) {
$matched_keys = array_keys($unMatchedItems, $item);
foreach($matched_keys as $key) {
$possibleGroup = array_slice($unMatchedItems, $key);
$matches = findGroupMatch($possibleGroup, $array, $i);
if ($matches) {
//Insert the items before group as single item group
if ($key > 0) {
for ($j = 0; $j < $key; $j++) {
$groupsArray[] = array('number' => 1, 'values' => array($unMatchedItems[$j]));
}
}
//Insert the group array
$groupsArray[] = array('number' => $matches + 1, 'values' => $possibleGroup); //number includes initial group also so $matches + 1
$i += (count($possibleGroup) * $matches) - 1; //skip the matched items from next iteration
//Empty the unmatched array to start with a new group search
$unMatchedItems = array();
break;
}
}
//If there was no matches, add the item to the unMatched group
if(!$matches) $unMatchedItems[] = $item;
} else {
$unMatchedItems[] = $item;
}
}
//Insert the remaining items as single item group
for($k=0; $k<count($unMatchedItems); $k++) {
$groupsArray[] = array('number' => 1, 'values' => array($unMatchedItems[$k]));
}
print_r($groupsArray);
The Result will be like this: (Check this PHP Fiddle for testing and also https://eval.in/507333 for your another input test.)
Array
(
[0] => Array
(
[number] => 2
[values] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
)
[1] => Array
(
[number] => 1
[values] => Array
(
[0] => c
)
)
[2] => Array
(
[number] => 1
[values] => Array
(
[0] => d
)
)
)
The first example is super easy with recursion. The second example... not so easy.
The example below works for only the first example by assuming no pattern should ever contain two of the same element. This will also handle all individual element patterns at the end of the original array and keep the pattern order (of the first pattern occurrence).
function find_pattern($input, &$result) {
$values = []; // currently processed elements
$pattern = ''; // the current element pattern
$dupe_found = false; // did we find a duplicate element?
// search the values for the first that matches a previous value
while ($next = array_shift($input)) {
// check if the element was already found
if (in_array($next, $values)) {
// re-add the value back into the input, since the next call needs it
array_unshift($input, $next);
// add the resulting pattern
add_pattern($pattern, $values, $result);
// find the next pattern with a recursive call
find_pattern($input, $result);
// a duplicate element was found!
$dupe_found = true;
// the rest of the values are handled by recursion, break the while loop
break;
} else {
// not already found, so store the element and keep going
$values[] = $next;
// use the element to produce a key for the result set
$pattern .= $next;
}
}
// if no duplicate was found, then each value should be an individual pattern
if (!$dupe_found) {
foreach ($values as $value) {
add_pattern($value, [$value], $result);
}
}
}
function add_pattern($pattern, $values, &$result) {
// increment the pattern count
$result[$pattern]['number'] = isset($result[$pattern]['number']) ?
result[$pattern]['number']+1 : 1;
// add the current pattern to the result, if not already done
if (!isset($result[$pattern]['values'])) {
$result[$pattern]['values'] = $values;
}
}
And an example usage:
$input = [
'a', 'b', 'c', 'd',
'a', 'b', 'c', 'd',
'c', 'd'
];
$result = [];
find_pattern($input, $result);
echo "<pre>";
print_r($result);
echo "</pre>";
The example output:
Array
(
[abcd] => Array
(
[number] => 2
[values] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
)
[c] => Array
(
[number] => 1
[values] => Array
(
[0] => c
)
)
[d] => Array
(
[number] => 1
[values] => Array
(
[0] => d
)
)
)
You could do something like this:
<?php
$array = array(
'a', 'b', 'c', 'd',
'a', 'b', 'c', 'd',
'c', 'd'
);
// Call this function to get your patterns
function patternMatching(array $array) {
$patterns = array();
$belongsToPattern = array_fill(0, count($array), false);
// Find biggest patterns first
for ($size = (int) (count($array) / 2); $size > 0; $size--) {
// for each pattern: start at every possible point in the array
for($start=0; $start <= count($array) - $size; $start++) {
$p = findPattern($array, $start, $size);
if($p != null) {
/* Before we can save the pattern we need to check, if we've found a
* pattern that does not collide with patterns we've already found */
$hasConflict = false;
foreach($p["positions"] as $idx => $pos) {
$PatternConflicts = array_slice($belongsToPattern, $pos, $p["size"]);
$hasConflict = $hasConflict || in_array(true, $PatternConflicts);
}
if(!$hasConflict) {
/* Since we have found a pattern, we don't want to find more
* patterns for these positions */
foreach($p["positions"] as $idx => $pos) {
$replace = array_fill($pos, $p["size"], true);
$belongsToPattern = array_replace($belongsToPattern, $replace);
}
$patterns[] = $p;
// or only return number and values:
// $patterns[] = [ "number" => $p["number"], "values" => $p["values"]];
}
}
}
}
return $patterns;
}
function findPattern(array $haystack, $patternStart, $patternSize ) {
$size = count($haystack);
$patternCandidate = array_slice($haystack, $patternStart, $patternSize);
$patternCount = 1;
$patternPositions = [$patternStart];
for($i = $patternStart + $patternSize; $i <= $size - $patternSize; $i++) {
$patternCheck = array_slice($haystack, $i, $patternSize);
$diff = array_diff($patternCandidate, $patternCheck);
if(empty($diff)) {
$patternCount++;
$patternPositions[] = $i;
}
}
if($patternCount > 1 || $patternSize <= 1) {
return [
"number" => $patternCount,
"values" => $patternCandidate,
// Additional information needed for filtering, sorting, etc.
"positions" => $patternPositions,
"size" => $patternSize
];
} else {
return null;
}
}
$patterns = patternMatching($array);
print "<pre>";
print_r($patterns);
print "</pre>";
?>
The code might be far from being optimal in speed but it should do what you want to do for any sequence of strings in an array. patternMatching() returns the patterns ordered descending in size of the pattern and ascending by first occurence (You can use ['positions'][0] as a sorting criteria to achieve a different order).
This should do it:
<?php
$array = array(
'x', 'y', 'x', 'y', 'a',
'ab', 'c', 'd',
'a', 'b', 'c', 'd',
'c', 'd', 'x', 'y', 'b',
'x', 'y', 'b', 'c', 'd'
);
// convert the array to a string
$string = '';
foreach ($array as $a) {
$l = strlen($a)-1;
$string .= ($l) ? str_replace('::',':',$a[0] . ':' . substr($a,1,$l-1) . ':' . $a[$l]) . '-' : $a . '-';
}
// find patterns
preg_match_all('/(?=((.+)(?:.*?\2)+))/s', $string, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$temp = str_replace('--','-',$m[2].'-');
$patterns[] = ($temp[0]==='-') ? substr($temp,1) : $temp;
}
// remove empty values and duplicates
$patterns = array_keys(array_flip(array_filter($patterns)));
// sort patterns
foreach ($patterns as $p) {
$sorted[$p] = strlen($p);
}
arsort($sorted);
// find double or more occurences
$stringClone = $string;
foreach ($sorted as $s=>$n) {
$nA = substr_count($stringClone,':'.$s);
$nZ = substr_count($stringClone,$s.':');
$number = substr_count($stringClone,$s);
$sub = explode('-',substr($stringClone,strpos($stringClone,$s),$n-1));
$values = $sub;
if($nA>0 || $nZ>0){
$numberAdjusted = $number - $nA - $nZ;
if($numberAdjusted > 1) {
$temp = '';
while($n--){
$temp .= '#';
}
$position = strpos(str_replace(':'.$s,':'.$temp,str_replace($s.':',$temp.':',$string)),$s);
$stringClone = str_replace(':'.$s,':'.$temp,$stringClone);
$stringClone = str_replace($s.':',$temp.':',$stringClone);
$result['p'.sprintf('%09d', $position)] = array('number'=>$numberAdjusted,'values'=>$values);
$stringClone = str_replace($s,'',$stringClone);
$stringClone = str_replace($temp,$s,$stringClone);
}
} else if($number>1){
$position = strpos($string,$s);
$result['p'.sprintf('%09d', $position)] = array('number'=>$number,'values'=>$values);
$stringClone = str_replace($s,'',$stringClone);
}
}
// add the remaining items
$remaining = array_flip(explode('-',substr($stringClone,0,-1)));
foreach ($remaining as $r=>$n) {
$position = strpos($string,$r);
$result['p'.sprintf('%09d', $position)] = array('number'=>1,'values'=>str_replace(':','',$r));
}
// sort results
ksort($result);
$result = array_values($result);
print_r($result);
?>
Working example here.

Copying/trimming an array to a certain size

EDIT: making it clear that I need it for multidimensional arrays (at any depth level)
I need to cut down the size of an array to get only a portion of it, but this needs to be done recursively. For example, take the following case:
$a = array(
'a',
'b' => array(
'x' => array(
'aleph',
'bet'
),
'y'),
'c',
'd',
'e'
);
what I need is that after copying only 4 elements I'll get the following resulted array:
$a = array(
'a',
'b' => array('x' => array(
'aleph'
),
),
);
and not...
$a = array(
'a',
'b' => array('x' => array(
'aleph',
'bet'
),
'y'),
'c',
'd',
);
How do I achieve this?
Thanks!
You can try **Note : dual-dimensional
$a = array("a","b" => array('x','y'),"c","d","e");
$new = __cut($a);
function __cut($array, $max = 4) {
$total = 0;
$new = array();
foreach ( $array as $key => $value ) {
if (is_array($value)) {
$total ++;
$diff = $max - $total;
$slice = array_slice($value, 0, $diff);
$total += count($slice);
$new[$key] = $slice;
} else {
$total ++;
$new[$key] = $value;
}
if ($total >= $max)
break;
}
return $new;
}
var_dump($new);
Output
array
0 => string 'a' (length=1)
'b' =>
array
0 => string 'x' (length=1)
1 => string 'y' (length=1)
function arrayTrim($array, $size, $finalArray = null){
global $count;
foreach ($array AS $key => $val){
if($size == $count)
return $finalArray;
$count++;
if(is_array($val)){
$finalArray[$key] = array();
$finalArray[$key] = arrayTrim ($val, $size, $finalArray[$key]);
}
else
$finalArray[$key] = $val;
}
return $finalArray;
}
$a = array( "a"=> array('xa', 'ya'), "b" => array('x', 'y'), "c", "d", "e" );
print_r(arrayTrim($a, 4));
should work fine

Finding patterns in php array

For example:
main array is: array(0 => 'A', 1 => 'A', 2 => 'B', 3 => 'B', 4 => 'B');
pattern is: array('A', 'B');
expected answer: array( array(0, 2), array(1, 3) )
one more example:
main array array(0 => 'F', 5 => 'G', 78 => 'R', 2 => 'D');
pattern array('G', 'R', 'F');
expected answer: array(array(5, 78, 0))
How can I find all occurrences of pattern in array?
Here's an function that uses recursion.
function array_pattern($array, $pattern){
$ret = array(array());
$found = true;
foreach($pattern as $val){
$x = array_search($val, $array);
if($x === FALSE){
$found = FALSE;
break;
}
unset($array[$x]);
$ret[0][] = $x;
}
return $found ? array_merge($ret, array_pattern($array, $pattern)) : array();
}
Call it like this:
$a = array_pattern(array(0 => 'A', 1 => 'A', 2 => 'B', 3 => 'B', 4 => 'B'), array('A', 'B'));
$b = array_pattern(array(0 => 'F', 5 => 'G', 78 => 'R', 2 => 'D'), array('G', 'R', 'F'));
DEMO: http://codepad.org/JCdsAMGk
I didn't test the following code, but it may give you ideas.
$arr = array(0 => 'A', 1 => 'A', 2 => 'B', 3 => 'B', 4 => 'B');
$test = array('G', 'R', 'F');
$count = 0;
$count2 = 0;
for($i=0;$i<count($arr);$++){
$pass= true;
if(count($test)+$count <= count($arr)){
for($k=0;$k<count($test);$k++){
if($arr[k+i]!=$test[k]){
$pass = false;
}
}
}else{
$pass = false;
}
if($pass){
$output[$count2] = $i;
$count2++;
}
}

Keeping even numbered elements of an array?

Say I have an $input array, that contains something like this :
array
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
3 => string 'd' (length=1)
4 => string 'e' (length=1)
5 => string 'f' (length=1)
6 => string 'g' (length=1)
7 => string 'h' (length=1)
8 => string 'i' (length=1)
9 => string 'j' (length=1)
I want to get an $output array, that would contain this :
array
0 => string 'a' (length=1)
1 => string 'c' (length=1)
2 => string 'e' (length=1)
3 => string 'g' (length=1)
4 => string 'i' (length=1)
The $output array contains half the values that were in $input ; those that had even numbered keys in the input; the first one is kept, second one is not, third one is, and so one...
(Note: the keys are not preserved ; only the values are important)
How could I do that ? Keeping only one on two values of the array ?
I have already tried some ideas, and already have a couple different solutions :
First idea: iterate over the input array, and copy the interesting values to the output array:
$input = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', );
$output = array();
$nbr = count($input);
for ($i = 0 ; $i < $nbr ; $i += 2) {
$output[] = $input[$i];
}
var_dump(array_values($output));
Second idea: iterate over the array, and unset what I don't want to keep:
$input = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', );
$output = $input;
$nbr = count($input);
for ($i = 1 ; $i < $nbr ; $i += 2) {
unset($output[$i]);
}
var_dump(array_values($output));
Third idea: use a combinaison of array_flip, range, array_diff_key, ... :
$input = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', );
$output = array();
$keys_to_exclude = array_flip(range(1, count($input)-1, 2));
$output = array_diff_key($input, $keys_to_exclude);
var_dump(array_values($output));
Fourth idea: about the same thing, but with array_intersect_key:
$input = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', );
$output = array();
$keys_to_include = array_flip(range(0, count($input)-1, 2));
$output = array_intersect_key($input, $keys_to_include);
var_dump(array_values($output));
Any other idea ? Even / particularly if it sounds kinda hacky or anything ?
My goal is not to get the most efficient nor simple syntax ; it's just for fun and because I am curious, actually ^^
If the title is not using the right words to describe what I want, don't hesitate to tell ; or edit it :-)
<?php
$x = range('a', 'f');
$x = array_map('array_shift',
array_chunk($x, 2)
);
var_dump($x);
or another one
<?php
class ArrayEvenIterator extends ArrayIterator {
public function next() {
parent::next();
return parent::next();
}
}
$x = range('a', 'f');
$x = iterator_to_array(new ArrayEvenIterator( $x ), false);
var_dump($x);
or with a php 5.3 closure (which isn't better than global in this case ;-) )
<?php
$x = range('a', 'f');
$x = array_filter( $x, function($e) use(&$c) { return 0===$c++%2; });
var_dump($x);
Assuming numeric keys:
foreach ($array as $key => $value) {
if ($key % 2 != 0) {
unset($array[$key]);
}
}
EDIT
Here goes my slightly more insane solution which keeps the index continuous without re-indexing. ;o)
foreach ($array as $key => $value) {
if (!($key%2)) {
$array[$key/2] = $value;
}
}
$array = array_slice($array, 0, ceil(count($array)/2));
If you're using PHP 5.3 or later, or have the SPL extension installed (you will by default on PHP 5), you can use the FilterIterator and ArrayObject classes.
class EvenKeysFilter extends FilterIterator
{
private function iseven($keyval)
{
return (($keyval % 2) == 0);
}
public function accept()
{
$keyval = $this->getInnerIterator()->key();
return ($this->iseven($keyval));
}
}
$input = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', );
$inputobj = new ArrayObject($input);
$evenFilterIter = new EvenKeysFilter($inputobj->getIterator());
$output = iterator_to_array($evenFilterIter, false);
print_r($output);
(Props to VolkerK for pointing out iterator_to_array())
Which correctly outputs this:
Array
(
[0] => a
[1] => c
[2] => e
[3] => g
[4] => i
)
Not necessarily the most efficient method, but since you mentioned that wasn't necessarily a requirement...
flip, filter, then flip back.
<?php
function even($var)
{
return(!($var & 1));
}
$input = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', );
$flipped = array_flip($input);
$filtered = array_filter($flipped, 'even');
$output = array_flip($filtered);
?>
I would invert the bitwise-odd check and either use array_filter() (and reindexing the result with array_values()) or use a classic foreach() loop.
Code: (Demo)
$array = range('a', 'j');
var_export(
array_values(
array_filter(
$array,
function($k) {
return !($k & 1);
},
ARRAY_FILTER_USE_KEY
)
)
);
echo "\n---\n";
$result = [];
foreach ($array as $index => $value) {
if (!($index & 1)) {
$result[] = $value;
}
}
var_export($result);
Output:
array (
0 => 'a',
1 => 'c',
2 => 'e',
3 => 'g',
4 => 'i',
)
---
array (
0 => 'a',
1 => 'c',
2 => 'e',
3 => 'g',
4 => 'i',
)
In my own projects, I prefer to keep global variable declarations to a minimum, so I'd use array_filter().
create a wrapper function
function getInput($i)
{
global $input;
return $input[$i*2];
}
The smallest and most efficient I guess.
function dropHalf($a){
$f=0;
foreach($a as $k=>$v)
if($f = ! $f)
unset($a[$k]);
return $a;
}
That's the smallest version I could think off.

how to export to excel file with multiple sheets wherein data is from database using mysqli

I am a newbie to oops concept of phpexcel.
I am trying to do something like I have data which I am trying to export based on from date, to date and session like breakfast, lunch, and dinner in multiple sheets.
I have almost reached but unable to iterate the rows.
Any help would great to complete it.
Below is the code that I tried.
<?php
error_reporting(E_ALL);
ini_set("display_errors", "ON");
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
include 'db.php';
$xls_filename = 'Data' . date('d-m-Y') . '.xls'; // Define Excel (.xls) file name
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$heading = array(
'id' => 'S.No',
'date' => 'Date',
'item_name' => 'Item',
'qty_in' => 'Quantity (dine in)',
'qty_out' => 'Quantity (parcel)',
'price' => 'Total Quantity',
'total' => 'Total Price',
);
$no_of_cols = count($heading);
$rowNumberH = 1;
$colH = 'A';
$columns = array('0' => 'A', '1' => 'B', '2' => 'C', '3' => 'D', '4' => 'E', '5' => 'F', '6' => 'G', '7' => 'H', '8' => 'I', '9' => 'J', '10' => 'K', '11' => 'L', '12' => 'M', '13' => 'N', '14' => 'O', '15' => 'P', '16' => 'Q', '17' => 'R', '18' => 'S', '19' => 'T', '20' => 'U', '21' => 'V', '22' => 'W', '23' => 'X', '24' => 'Y', '25' => 'Z');
$q = $conn->query("SELECT * FROM main");
if (mysqli_num_rows($q) > 0) {
foreach ($heading as $h) {
$objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
$objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
$colH++;
}
$row = 2;
while ($row_q = mysqli_fetch_assoc($q)) {
$i = 0;
foreach ($row_q as $key => $value) {
if ($key == 'location')
continue;
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
$i++;
}
}
}
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
?>
The code is printing only one row.
Increment $row at the while loop. I hope it will work.
Edited:
I am going to mention your slice of code where I have added $row++.
I have added $row++ at the end of while loop and it is working for me as I tested it at my computer. I implemented your code by creating table like your table. I also tested different type of table in this code and that worked perfect.
$q = $conn->query("SELECT * FROM student");
if (mysqli_num_rows($q) > 0) {
foreach ($heading as $h) {
$objPHPExcel->getActiveSheet()->setCellValue($colH . $rowNumberH, $h);
$objPHPExcel->getActiveSheet()->getColumnDimension($colH)->setWidth(25);
$colH++;
}
$row = 2;
while ($row_q = mysqli_fetch_assoc($q)) {
$i = 0;
foreach ($row_q as $key => $value) {
if ($key == 'location')
continue;
$objPHPExcel->getActiveSheet()->setCellValue($columns[$i] . $row, $row_q[$key]);
$i++;
}
$row++; // $row is added only in your code.
}
}
What you can do is :
Use this function instead :
$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($col,$row,"Name");
both $col and $ row are integers.
if you would use
$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow(0,1,"Name");
Name will be printed on cell A1.
Using this method will remove ambiguity that if you are actually moving to next row or not .

Categories