PHP Spreadsheet color not working - php

I am trying to apply collors to my cells using a function which is this:
function color($cell, $color, $find = true){
if ($find){
$color = $this->color_helper($color);
}
$this->sheet->getStyle($cell)->getFill()->applyFromArray(array(
'type' => PHPExcel_Style_Fill::FILL_SOLID,
'startcolor' => array(
'rgb' => $color
)
));
}
colors are defined in the color_picker function and they return something like ''FF0000'for red.
Now the problem is, after I download the file, nothing is changed, its like the original file.

Related

Dynamically apply a key/value pair to an array in one go (within first definition)

Imagine this situation:
$component = array(
'type' => 'chimney',
'material' => 'stone'
);
What i would like to do is to add a key/value pair to this array, if a certain condition is met.
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => ($hasMetrics ? array('width' => 60, 'height' => 2000) : false)
);
While this could be used, it will always cause a key called 'metrics' in my array.
Of course, if i don't want that, i could use array_merge() to merge a second array with the first (the second being either an empty array or the desired key/value pair, depending on the condition).
But what i am longing to find out is if there is any way to define this array like above, while taking care of $hasMetrics, without the use of any other means (such as array_merge()) but purely in the actual (first and only) definition of this array.
Like this: (non-applicable, demonstrative example)
$component = array(
'type' => 'chimney',
'material' => 'stone',
($hasMetrics ? array('metrics' => array(
'width' => 60,
'height' => 2000
)) : false)
);
(This, as i understand it, would generate two keys (type and material and then create one keyless value that is, itself, an array containing a key (metrics) and another array as value.)
Can anyone show me some proper approach? Perhaps there is some kind of PHP function available, with special properties (such as list() which is capable of cross-assignment).
EDIT
Perhaps some more clarification is needed, as many answers point out ways to go such as:
Using a followup assignment to a certain key
Filtering the generated array after defining it
While these are perfectly valid ways to extend the array, but i am explicitly looking for a way to do this in one go within the one array definition.
Not with the array defenition itself. I would add it to the array if necessary:
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
$hasMetrics = true;
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics){
$component['metrics'] = array('width' => 60, 'height' => 2000);
}
Try
$component = array(
'type' => 'chimney',
'material' => 'stone',
'metrics' => $hasMetrics ? array('width' => 60, 'height' => 2000) : ''
);
And after that
$component = array_filter( $component ); // remove if it has '' value
OR
$component = array(
'type' => 'chimney',
'material' => 'stone',
);
if($hasMetrics) {
$component['metrics'] = array('width' => 60, 'height' => 2000);
}

More efficient way to set PHP variables over and over again

I am curious of a better way of doing the code I have below, I find it repetitive and would like to cut down on it, any suggestions?
I was trying to do something with Variable Variables but I failed at getting that to work.
So basically I have a bunch of color names that I get with $_GET[color-name-here']
The goal is to set a color's code to a new color's code. So using the URL I am able to set the code for the color red to the color code of green so red's value would become 00FF00
// Get color and color replacement values from URL
// get_value_or is ran through this code...
// isset($_GET[$key]) && !empty($_GET[$key]) ? $_GET[$key] : $default;
$red = get_value_or('red', null);
$orange = get_value_or('orange', null);
$yellow = get_value_or('yellow', null);
$green = get_value_or('green', null);
$turquoise = get_value_or('turquise', null);
$blue = get_value_or('blue', null);
$purple = get_value_or('purple', null);
$pink = get_value_or('pink', null);
$white = get_value_or('white', null);
// Define Default Color Name and Hexcode values
$colorsArray = array(
'red' => 'FF0000',
'orange' => 'FF5000',
'yellow' => 'FFF200',
'green' => '00FF00',
'turquoise' => '00F0C8',
'blue' => '0064FF',
'purple' => '9F00FF',
'pink' => 'FF0082',
'white' => 'FFFFFF'
);
// Iterate Color Array and Set New Color Values if they exist
foreach($colorsArray as $colorName => $colorCode){
// Do something to set each color Name with a New color code, if that color name has a value set
}
// Right now I am doing it manually for each color name, all 9+ like this...
//Set Reds NEW color value
if(isset($red)){
$colorsArray['red'] = $colorsArray[$red];
}
//Set oranges NEW color value
if(isset($orange)){
$colorsArray['orange'] = $colorsArray[$orange];
}
//Set yellows NEW color value
if(isset($yellow)){
$colorsArray['yellow'] = $colorsArray[$yellow];
}
So any ideas how to set all the colors with less code?
A color's code should ONLY be updated if that color has a NEW value set in the URL using $_GET variables
PS) I wasn't sure of a good title for this question, feel free to change it if you have a better one, thanks
If I were you, I would put the assignments in the loop:
$colorsArray = array(
'red' => 'FF0000',
'orange' => 'FF5000',
'yellow' => 'FFF200',
'green' => '00FF00',
'turquoise' => '00F0C8',
'blue' => '0064FF',
'purple' => '9F00FF',
'pink' => 'FF0082',
'white' => 'FFFFFF'
);
foreach ($colorsArray as $colorName => $colorCode) {
$colorsArray[$colorName] = get_value_or($colorName, $colorCode);
}
It's quite neat, but I'm not sure whether it works with your real code or not.
Edit
I updated the code because I realized that the array $color_names was unnecessary, you have them in the keys of $colorsArray.
Edit
Updated the code again because the if in the loop was unnecessary too.
You can access all global variables by using the super global $GLOBALS. So you could do this:
foreach ($colorsArray as $colorName => $colorCode) {
if (isset($GLOBALS[$colorName]) {
$colorsArray[$colorName] = $GLOBALS[$colorName];
}
}
More info about $GLOBALS.

Regarding footer issue in pdf file creation using php and fpdf library

I have created and successfully created my pdf file in php with fpdf
library support.
But the problem is my footer is showing more space.
I want to reduce the space underneath my text. My output is like
this:
Here my code goes:
<?php
require('fpdf/fpdf.php');
class PDF extends FPDF {
function Header() {
$this->SetY(0.208333);
}
function Footer() {
if ($this->footer <> 1)
{
$this->SetY(-15);
}
else
{
echo "bye";
}
}
}
//class instantiation
$pdf=new PDF("l","in",array(8.5,4.17));
$pdf->SetFont('Arial','',8);
$pdf->footer = -15;
//Array2
$datas = array
(
'Address1' => array
(
'Name' => 'Vijaya',
'Area' => 'Valasaravakkam',
'City' => 'Chennai',
),
'Address2' => array
(
'Companyname' => 'Vy Systems',
'Area' => 'Valasaravakkam',
'City' => 'Chennai',
),
'Address3' => array
(
'Companyname' => 'Vy Systems1',
'Area' => 'Valasaravakkam1',
'City' => 'Chennai1',
),
);
//Array2
$datas1 = array
(
'Address4' => array
(
'Name' => 'Jaya',
'Area' => 'Valasaravakkam',
'City' => 'Chennai',
),
);
foreach($datas1 as $address1 => $details1)
{
//pdf_set_text_pos($pdf, 1240, 490);
//$pdf->ln(1);
foreach($datas as $address => $details)
{
$pdf->SetMargins(0,0,0.3);
$pdf->AddPage();
if((is_array($details)) and (is_array($details1)))
{
foreach($details1 as $rows1 => $value1)
{
$pdf->SetX(0.520833);
$pdf->MultiCell(0, 0.2, $value1, 0, "L");
}
$pdf->ln(1.96);
foreach($details as $rows => $value)
{
$pdf->SetX(5);
$pdf->MultiCell(5, 0.2, $value, 0, "L");
}
}
}//end of sub foreach
}//end of main foreach
$pdf->Output();
?>
I didn't follow the code completely, but it seems you're using the Header and Footer methods to set Y and nothing more, expecting that to be enough to correctly position the MultiCells being output outside of the Header and Footer. Maybe so, but the interaction of positioning inside and outside the Header/Footer isn't well defined.
For example, the process may be something like this: Y is calculated for the MultiCell, that trips the footer, the footer changes Y, the MultiCell is output. Is this the original Y, the revised (by the footer Y), or some other value? Absent a precise definition of what happens, you've set up a complex sequence of things that would be very difficult to sort out.
I would suggest vastly simplifying the code. You may find that the automatic header/footer tripping isn't helpful at all. In that case, turn off the auto page break, get rid of the Footer/Header functions, and totally control each page yourself. That way at least you have a clear, reliable model of what's going on.

Get value from custom category attribute

I am trying to get the value from a custom category attribute in Magento. The attribute is a select field and is been made with the install script below:
$this->startSetup();
$this->addAttribute('catalog_category', 'category_categorycolor', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'varchar',
'label' => 'Categorie kleur',
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'option' => array (
'value' => array('yellow' => array('Geel'),
'purple' => array('Paars'),
'blue' => array('Blauw'),
'red' => array('Rood'),
'orange' => array('Oranje'),
'green' => array('Groen'),
'darkblue' => array('Donkerblauw'),
'lightgreen' => array('Lichtgroen'),
)
),
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Unfortunately only getting numbers and not text value. I use this line to retrieve the value:
<?php $_category_categorycolor = $_category->getData('category_categorycolor'); if($_category_categorycolor): ?> <?php echo $_category_categorycolor; ?> <?php endif; ?>
Can someone help me?
Something like this:
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
The sollution is pretty messy (the only one I know of).
$opt = array(); // will contain all options in a $key => $value manner
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_category', 'category_categorycolor');
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
foreach ($options as $o) {
$opt[$o['value']] = $o['label'];
}
}
$categoryColorId = $_category->getData('category_categorycolor');
$categoryColorLabel = $opt[$categoryColorId];
// if you have problems, do a Zend_Debug::dump($opt);
// - it should contain an array of all the options you added
Didn't test it out, let me know if it works or not.
PS: can't reply to your comment, not sure why. What does $opt contain ?
The numbers you are getting back are the id's of each value in the dropdown. You have to load the dropdown values too.
See the following page. It helped me understand this.
http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/

PHPExcel specific cell formatting from style object

I'm using PHPExcel in a project and need to style the cells of the excel sheets.
What I've done is create a PHPExcel style object like this:
$style['red_text'] = new PHPExcel_Style();
I then use the set functions on this style to fill up the object like this:
$style['red_text']->getFont()
->applyFromArray(
array('name'=>'Arial')
)
Now I am trying to use this style object in a cell. I tried to use the applyFromArray function like this:
$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($x, $y)->applyFromArray( $style['red_text'] );
This isn't the way to do it I don't think. To me, this is the most readable and consistent way of assigning the styles but if someone who is more fluent with PHPExcel could direct me towards the proper method I'd be much obliged!
P.S. Excuse the formatting; this is my first post :)
EDIT: just found the error in this: "Invalid style array passed"
Does this mean I'm creating the style object wrongly?
Applying from array is literally applying from an array, not from a style object
$style['red_text'] = array(
'font' => array(
'name' => 'Arial',
'color' => array(
'rgb' => 'FF0000'
)
),
);
$objPHPExcel->getActiveSheet()
->getStyleByColumnAndRow($x, $y)
->applyFromArray($style['red_text']);
or alternatively:
$style['red_text'] = array(
'name' => 'Arial',
'color' => array(
'rgb' => 'FF0000'
)
);
$objPHPExcel->getActiveSheet()
->getStyleByColumnAndRow($x, $y)
->getFont()
->applyFromArray($style['red_text']);
You could also do this:
$objPHPExcel->getActiveSheet()
->getStyle('A1:B30')
->getFont()
->applyFromArray(
array(
'name' => 'Arial',
'color' => array(
'rgb' => 'FF0000'
)
)
);

Categories