PHPExcel Values get remove on merge row - php

In my application, I have a json file which holds products.
In a loop I loop through every item and put them in cells.
$this->row = 4;
foreach ($this->json['products'] as $product) {
$this->template->getActiveSheet()->setCellValueByColumnAndRow(6, $this->row, $product);
$this->row++;
}
Afterwards, I am setting the style of the excel file
$this->template->getActiveSheet()->mergeCells('J6:K6');
Basically merging row J and K with each other but because of this the value of K gets removed. How can I set the value of K to the next row value dynamically after the merge of an inaccessible row?

#jahmic gives answer here
//There is a specific method to do this:
$objPHPExcel->getActiveSheet()->mergeCells('J6:K6');
//You can also use:
$objPHPExcel->setActiveSheetIndex(0)->mergeCells('J6:K6');
//That should do the trick.
Link is
Merge Cell values with PHPExcel - PHP

Related

how to echo cod39 value inside $pdf->cell() or store it in variable and print the result inside a table?

I am trying to print a table in FPDF that has 4 columns, and many rows retrieved from MySQL.
Two of my columns shall print the value in barcode using code39 in FPDF. The problem is that it's not allowing me to print code39 results within the cells. I can do that only by adding the location to the $i and adding it to the location of the code39. Even though one page prints correctly, it then gets out of sync.
If you check on the output, you will see the problem I'm having.
foreach ($result as $row) {
$pdf->Code39(12,47+$i,'%'.$row['activity'].' START',1,10);
$pdf->Code39(209,47+$i,'%'.$row['activity'].' END',1,10);
$pdf->Cell($width_cell[0],20,'',1,0,'c',false); // First column of row 1
$pdf->Cell($width_cell[1],20,$row['activity'],1,0,'c',false); // Second column of row 1
$pdf->Cell($width_cell[2],20,$row['a_description'],1,0,'c',false); // Third column of row 1
$pdf->Cell($width_cell[3],20,'',1,1,'c',false); // Fourth column of row 1
$i=$i+20;
}
The output:
Before writing a cell that should contain a barcode, use the current cursor position in the document as the coordinates to draw the barcode
foreach ($result as $row) {
$pdf->Code39($pdf->GetX(), $pdf->GetY(), '%'.$row['activity'].' START',1,10);
$pdf->Cell($width_cell[0],20,'',1,0,'c',false); // First column of row 1
$pdf->Cell($width_cell[1],20,$row['activity'],1,0,'c',false); // Second column of row 1
$pdf->Cell($width_cell[2],20,$row['a_description'],1,0,'c',false); // Third column of row 1
$pdf->Code39($pdf->GetX(), $pdf->GetY(), '%'.$row['activity'].' END',1,10);
$pdf->Cell($width_cell[3],20,'',1,1,'c',false); // Fourth column of row 1
}
I haven't tested this. You might have to add or subtract an offset on GetX and GetY so that it looks good.

PHPExcel data checks

I'm trying to use PHPExcel to create a website that checks consistency of data in excel files.
I manage to load the excels but now I need to pass some checks on the data, and I can't figure out how. For example some of the checks would be:
Count number of empty cells in a specific column
Count number of non integer values in a specific column
Check that a column only has values that are contained in another one of the excels.
Are there some functions that do a kind of count where on the columns?
Or some way to iterate through the values to achieve this goals?
Thank you
Like Mark said, once I used PHPExcel to put the data into arrays I managed to do the checks myself. Here is an example of logging the empty cells of a column:
$n_row=0;
$divisasNull=0;
foreach ($sheetdata as $row) {
$n_column=0;
foreach ($row as $cell) {
if($n_column==6)
{
if($cell==null)
{
echo 'Error. Empty currency at row: '.$n_row.'<br>';
$divisasNull++;
}
}
$n_column++;
}
$n_row++;
}
echo 'Total errors: '.$divisasNull;

PHP spontaneously creates associative array where indexed array is required - using PHP arrays with Highcharts

I'm using PHP to retrieve data from an SQL database to produce a stacked column chart in Highcharts. The idea is that I'm taking the following piece of code to retrieve values from my database. This code should generate an array which then gets encoded to JSON and passed to Highcharts; this code produces a single 'part' of a stacked column, and the index determines which vertical bar that part is in. (So in http://www.highcharts.com/demo/column-stacked, the index would represent which fruit, and the data in this series would represent one person/color.)
The issue is that when I run this code, instead of ending up with an indexed array of data grouped by category, such as
[12,13,14,15] where each item is a category, I end up with an associative array where the indexes I specified in the code are turned into a string key.
{"1":13,"0":12,"3":14, "2":13, "5":15}
Because my indexes are being interpreted as associative keys and not as the indexed locations of the data inside the array, the data is now being added to locations in the order that I retrieved the data, and not assigned to a location in the array based on the index I give. Highcharts assigns categories based on location in the array, and not on key, so all my data ends up in the wrong categories.
Is there a way to get PHP to treat my carefully collected indexes as indexes and not as keys, and add my data points in the location in the array indicated by the indexes? I'm kind of new to PHP, and Java and C++ - the languages I've worked with before - don't have associative arrays, so any help you can give me in explaining and fixing this undesired behavior would be much appreciated.
Code below.
$variable indicates what the data is being sorted into categories by, and $r is the variable representing the array of the SQL query, so $r['variable'] is the category of this data point, and $r['amount'] is the data point itself.
$found = -1;
//if this is the first set of data being collected
if (count($category['data']) == 0){
$category['data'][0] = $r[$variable];
$series1['data'][0] = floatval($r['amount']);
$count++;
$times1[0]++;
}
//if it's not the first set of data, find out if this category has been used before
else {
for ($x = 0; $x < count($category['data']); $x++){
if ($r[$variable] == $category['data'][$x]){
$found = $x;
break;
}
}
// if that category does not already exist, add it, and add the data
if ($found == -1) {
$times1[$count]++;
$category['data'][$count] = $r[$variable];
$series1['data'][$count] = floatval($r['amount']);
$count++;
}
else { //otherwise, add its data to the data already in the current category. This will eventually yield an average, with $times1[] as the divisor
$times1[$found]++;
$series3['data'][$found] = floatval((floatval($series3['data'][$found]) + floatval($r['amount'])));
}}
Go through with below code hope it will give some idea to resolve your problem --
<?php
$jsonstring = '{"1":13,"0":12,"3":14, "2":13, "5":15}';
$tempArr = json_decode($jsonstring, true);
asort(tempArr); // for sorting the array --
//run another foreach to get created an array --
$finArr = array();
foreach(tempArr as $key=>$val){
$finArr[] = $val;
}
$requiredjsonString = json_encode(finArr); // it will return your required json Array [12,13,14,15]
?>
Edit: I advice also set JSON_NUMERIC_CHECK flag in json_encode();

Editing the object returned by $wpdb->get_results(SQL)

We have a nasty database call using the Wordpress function $wpdb->get_results(SQL).
After receiving the result in PHP, we need to make a few changes to the result.
So can anyone tell me how I can:
1) Remove specific rows from the get_results() returned object.
2) Change the values of the specific columns in specific rows in the returned object.
I.e. if the object returned is $nastyData, we need to:
1) Remove specific rows from $nastyData
2) Change the value of specific columns in specific rows in $nastyData, for example $nastyData->name for a specific row.
Any ideas?
I have thought about makeing get_results() return the data as an array, but that will create problems in other places in our code (where the code expects to receive an object).
Thanks,
Mads
To start with, your "Nasty database call" should be optimized to be less nasty. More specifically, only query the results you want so that you don't have to remove stuff afterwords. This is the best solution.
If you insist on trying to modify the objects, this is a workaround. According to the documentation, when returning objects, they are returned in one of two ways:
OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
So, knowing that the result is an array of objects, we can get to each individual instance using a foreach construct:
$results = $wpdb->get_results( $nastySQL );
foreach($results as $index => $result)
{
// Change name column to FirstName using copy and delete
$tmp = $result->name;
unset($result->name);
$result->FirstName = $tmp;
// Remove specific row
if( $result->name == "Tom")
{
unset($results[$index]);
}
}
Below code will replace the value coming from specific field of database table, if name field has value like Reo and you want to replace it with other name like John then you can to do this via below code
$results = $wpdb->get_results( $nastySQL );
foreach($results as $key => $value){
$results[$key]->name= 'John';
}
return $results;

php, get values from matrix (array)

I have Matrix and how get value from there?
The data obtained from a file, so that the matrix can be of different sizes
Thanks
Hypothetically (because the question is vague), if you read the contents in and have the results stored in a two-dimensional array, then you would use brackets to find the cell value. For example, here's reading in the contents into a multidimensional array called $matrix:
$contents = file_get_contents("myfile.csv");
$splitlines = explode("\n",$contents);//split the contents into rows
$matrix = array();
foreach($splitlines as $line){
$row = explode(",",$line);//split each line into columns
$matrix[] = $row;//add the array of columns as a new row in the matrix
}
now address any of the values in the matrix:
$samplevalue = $matrix[2][1]; //addresses the third row, second column.

Categories