I'm trying to read a merged cell in phpexcel, my problem is that the cell is merged on A:B:C:D:E:F ( can't argue with what they want )
$i= 0;
foreach ($worksheet->getRowIterator() as $row) {
if ($i > 10) break ;
$i ++;
$cellIterator = $row->getCellIterator();
foreach ($cellIterator as $cell) {
if($cell->getColumn()== 'A:B:C:D:E:F'){
$specification=$cell->getCalculatedValue();
var_dump($specification);die();
// some other code
always dumps null.
I've tried $cell->getColumn()== 'A' since that the cell starts at A, but dumps null as well.
I would appreciate any help.
I don't understand exactly what you're trying to do here, because getColumn() will only ever return a single column address like A or B and never anything like 'A:B:C:D:E:F'
It may be sensible to iterate only existing cells using
$cellIterator->setIterateOnlyExistingCells(true);
but there's a couple of functions that may help you with merged cells:
$cell->isInMergeRange()
will return a Boolean true/false, indicating if the cell is part of a merge range
and
$cell->isMergeRangeValueCell()
can be used to test whether a cell within a merge range is the top-left (primary) cell of that range, and will return a Boolean true/false indicating whether it holds the actual data value/type etc for that range... Note that it will return a false if the cell isn't part of any merge range
I have a CSV file that I am outputting to a table via PHP. The CSV data appears like this:
0072534,800:fixed:9.9900:0072534|6500:fixed:9.9900:0072538|2100:fixed:9.9900:0072537
0072538,800:fixed:9.9900:0072534|6500:fixed:9.9900:0072538|2100:fixed:9.9900:0072537
0072537,800:fixed:9.9900:0072534|6500:fixed:9.9900:0072538|2100:fixed:9.9900:0072537
I am struggling to work out how I can get the second CSV column to only display the data relevant to the ID in the first CSV column. The desired result would be:
0072534, 800
0072538, 6500
0072537, 2100
(The first part of the second column is the data I am after, i.e. the the data before the first ':' )
Does that make sense and can someone suggest a solution? Thank you!!
You could iterate over each row (however you're parsing your CSV file), and further iterate over each CSV column - exploding the second column.
This is assuming that you want to get the beginning value after each | character, dependent on the row number. If I am misunderstanding, let me know - your question could be far clearer.
For example, something like this:
$count = 0;
$final = array();
foreach ($rows as $row) {
$columns = explode(",", $row);
$column2 = explode("|", $columns[1]);
$length = strpos($column2[$count], ':');
$column2 = substr($column2[$count], 0, $length);
$final[] = array($columns[0], $column2);
$count++;
}
Here is the snapshoot of my form. The input values under LPO (1st column) are hidden, I just showed them here to show complete form.
if color_id (first left most inputbox) is 37 its DB value is BB552 as displayed. And if its 3, the value of that color is BB110, but when its 0, it means a user has selected Custom and written a value by him self in the third row it is FFBBBAA.
I need to submit the form to PHP
The field names are as follows
color_id[] <-- Hidden input
order_id[] <-- Hidden input
subcolor_id[] <-- Select
subcolor_string[] <-- Input
material_id[] <-- Select
weight[] <-- input
When I post the form, in PHP
<?PHP
$count = count($_POST['weight']);
for($i = 0; $i < $count; $i++){
$color_id = $_POST['color_id'][$i];
$order_id = $_POST['order_id'][$i];
$material_id = $_POST['material_id'][$i];
$weight = $_POST['weight'][$i];
// i can count one post item and can iterate over all.
if($color_id == 0){
$color_id = $_POST['subcolor_id'][$i]; // this give me wrong result;
}
}
So when 0 is there, admin approving this form, can leave it to custom or change the color back to some value from DB
I want to know what are the possibilities for getting the proper sub color values if first hidden input color_id has 0.
So far the one which I thought of is to add two more hidden fields in Sub Color columns to match their index, but this will require me to completely re-write the del sub color code
Is there any other way which can save me from doing lots of alterations to this form?
as eds mentioned it totally makes sense.. u shud do this way
$j=0;
$count = count($_POST['weight']);
for($i = 0; $i < $count; $i++){
$color_id = $_POST['color_id'][$i];
$order_id = $_POST['order_id'][$i];
$material_id = $_POST['material_id'][$i];
$weight = $_POST['weight'][$i];
// i can count one post item and can iterate over all.
if($color_id == 0){
$color_id = $_POST['subcolor_id'][$j]; // this give me wrong result;
$j++;
}
}
this shud solve your problem..
Your other option is to declare another iterator $j outside of the loop, starting at 0, and manually incrementing that iterator after you read a subcolor each time. Then it should always be the index of the next subcolor_id you need to read.
This may sound like a silly question and I'm not thinking hard enough,
Or its harder than i think...
Say i have a array of numbers like:
$table = array(
'5','2','1','4','4','4',
'1','2','4','2','1','1',
'3','4','3','1','4','4',
'1','4','2','S','4','4',
'1','2','4','2','1','1',
'5','2','6','4','8','1'
);
S = where I want to get either the row or column based on where "S" is.
I know i can get where S is.
by doing:
$start = array_search('S', $table);
The ''grid'' this array is based on,The S can can be anywhere.
And the grid itself can be different sizes length and width.
How would i go about getting the whole row or column.?
(IE:S is in column 3 : 4,2,1,S,2,4)
(IE:S is in row 3 : 1,4,2,S,4,4)
Just a hint in the right direction would be helpful, Don't need to go all out for me.
Or a idea on how to approach this.
Okay, I guess you have the width you want to use stored somewhere! (And you don't want to use 2-dimensional arrays, check the other answer if you don't care, a 2-dimensional array makes a lot more sense in this case).
If you want to get the line, use $start which is the position of S in the array and divide it by the width. Then you need to round down or up (depends on if you are starting to count at 0 or 1)
For column you need to do something similar, use $start % $width here. (add 1 if you start counting at 1).
Here is a hint:
$table[] = array();
$table[][0] = array(5,2,1,4,5);
$table[][1] = array(1,5,2,3,3);
$table[][2] = array(1,2,'s',4,5);
You will need an array of arrays to have an x/y search.
Your x is the nth element in the outer array.
Your y is the nth element in the inner array.
You can search your string like 's' in array and find column and row of that :
$cols=5; // your columns count
$row=1;
$i=1;
$array = array('1','2','3','4','5','6','7','s','9');
foreach($array as $value)
{
if($value=='s')
{
echo 'Column : '.$i .' || Row : ' .$row ;
}
if($i==$cols)
{
$i=0;
$row++;
}
$i++;
}
I have the following array:
$masterlist=[$companies][$fieldsofcompany][0][$number]
The third dimension only exists if the field selected from $fieldsofcompany = position 2 which contains the numbers array. Other positions contain regular variables. The 3rd dimension is always 0 (the numbers array) or Null. Position 4 contains numbers.
I want to cycle through all companies and remove from the $masterlist all companies which contain duplicate numbers.
My current implementation is this code:
for($i=0;$i<count($masterlist);$i++)
{
if($masterlist[$i][2][0][0] != null)
$id = $masterlist[$i][0];
for($j=0;$j<count($masterlist[$i][2][0]);$j++)
{
$number = $masterlist[$i][2][0][$j];
$query = "INSERT INTO numbers VALUES('$id','$number')";
mysql_query($query);
}
}
Which inserts numbers and associated IDs into a table. I then select unique numbers like so:
SELECT ID,number
FROM numbers
GROUP BY number
HAVING (COUNT(number)=1)
This strikes me as incredibly brain-dead. My question is what is the best way to do this? I'm not looking for code per se, but approaches to the problem. For those of you who have read this far, thank you.
For starters, you should prune the data before sticking it into the database.
Keep a look up table that keeps track of the 'number'.
If the number is not in the look up table then use it and mark it, otherwise if its in the look up table you can ignore it.
Using an array for the look up table and with keys being the 'number' you can use the isset function to test if the number has appeared before or not.
Example pseudo code:
if(!isset($lookupTable[$number])){
$lookupTable[$number]=1;
//...Insert into database...
}
Now that I think I understand what you really want, you might want to stick with your two-pass approach but skip the MySQL detour.
In the first pass, gather numbers and duplicate companies:
$duplicate_companies = array();
$number_map = array();
foreach ($masterlist as $index => $company)
{
if ($company[2][0][0] === null)
continue;
foreach ($company[2][0] as $number)
{
if (!isset($number_map[$number])
{
// We have not seen this number before, associate it
// with the first company index.
$number_map[$number] = $index;
}
else
{
// Both the current company and the one with the index stored
// in $number_map[$number] are duplicates.
$duplicate_companies[] = $index;
$duplicate_companies[] = $number_map[$number];
}
}
}
In the second pass, remove the duplicates we have found from the master list:
foreach (array_unique($duplicate_companies) as $index)
{
unset($masterlist[$index]);
}