Category: PHP


It took me a couple of searches before finally got to solve the issue with having a validation for select box and not being able to display the error message. Tried a couple of solutions over the net then finally got the simple and efficient one. :)

Add this below your form select.

<?php echo $form->error('nameofselectbox');?>

That’s it! :)

Six things I know now. :)

It’s been awhile :) Happy New Year, by the way. I’ve been busy with work. But glad to have the time tonight. On this particular project that I did, I’ve come to realize that with every project, there will always be something new. The most fun part is the learning experience you have along while working on it. Tonight, I will share new (but might not be new to you) stuffs I’ve learned.

1. Creating transparency on existing image.

Not until today that I’ve realized the importance of GD Library.There was a particular task that I need to modify and add transparency to an existing image. GD library worked like charm on this. To be able to do it effectively, the existing image should have a dark color that could easily be distinguish and separated from the entire image. Here is the snippet:

<?php

$imagefile = "image/sample.png";
	
$im = imagecreatefrompng($imagefile);
$black = imagecolorallocate($im, 0, 0, 0); // search for the color, which is black
imagecolortransparent($im, $black); // replace all black pixel values to be transparent
imagepng($im, $imagefile);
imagedestroy($im);

?>

2. Determine if the value is odd or even.

This becomes handy when you need to display alternate table row colors.

<?php
function is_odd($num)
{
  return( $num& 1 );
} 
?>

3. Return two or more variables from a function.

Shame on me that I’m not aware of this. Oh well… good for me now. :)

<?php 

function basicMath($a, $b){

$sum = $a + $b;
$difference = $a - $b;

return array($sum, $difference); //return as array values.

}

$a = 5;
$b = 3;
list($sum, $difference) = basicMath( $a, $b); //returns two variables via list()

echo $a." + ".$b." = ".$sum;
echo $a." - ".$b." = ".$difference;

?>

4. Sort arrays and retain its original keys.

There was a particular query that I need to add sorting features. But the hard part was, the query was so complicated that sorting couldn’t be possible from there. What I did was to get all the necessary fields and rows, put them on arrays. On the specific array that will be the lead and sorted out. I’ve used the asort and arsort function. Retaining the keys helped in organizing and order the other fields that are associated to it.

Here is a sample:

<?php

$firstname = array('Ana','Billy','Carol');
$lastname=array('Dela Cruz','Garcia','Rodriguez');
$average=array('85','96','90');

asort($firstname); //sort in ascending order

foreach($firstname as $key => $value)
{
echo  $value." ".$lastname[$key]." average: ".$average[$key];

}

//sample output  Ana Dela Cruz average: 85

?>

5. Get image size.

getimagesize() is handy too. I used this to determine the amount of space needed to adjust the page layout.

<?php

list($width, $height, $type, $attr) = getimagesize("image/sample.png");

?>

6. Adjusting symbol display on fpdf output

I had trouble displaying the registered trademark symbol and apostrophe on generated pdf using fpdf library. Using str_replace() and iconv() solved the issue.


<?php

str_replace("®", iconv("UTF-8", "ISO-8859-1", "¨"), str_replace("™", iconv("UTF-8", "ISO-8859-1", "ª"), $row['title'])); //outputs the title with registered trademark symbol

str_replace('’', iconv('UTF-8', 'ISO-8859-1', " ' "),$title); //outputs the title with apostrophe

?>

So that’s about it. Hope this helps! :)

These are the applications I’m busy with as of the moment

A) Aptana Studio 2.0

It’s a robust web development IDE that is highly configurable. The most amazing part is it’s totally FREE! For those PHP Developers who would like to try it, I would like to share two things that you need to do when installing the software.

1) After you have downloaded and successfully installed the software, go to My Studio window -> Plugins (extensions for studio). Select all PHP Development Tools (PDT) necessary plugins from there.

2) I initially thought after installing the plugin, I would now be able to view PHP files on it. But after several attempts, it throws me errors. So what I did was to go to Help->Install Available software. There will be a list of updates you could install, just select “–All Available Sites–” in Work with field . I then selected all PDTs and PHP related plugin. This time, I was able to open the file and view the script.

B) Navicat Lite

All this time, I was working hand in hand with PHPmyAdmin for all database related stuff. But with the project I’m working on right now, It doesn’t have anything like it installed. I was instructed to look for a MySQL gui application. So I thought for a moment, and remembered my former boss used before. I googled “Navicat” and found the link of the website. I decided to download the free version, Navicat Lite. So far I’m liking the simplicity and ease of use it offers.

C) Swiff Chart Generator on Linux

Swiff Chart Generator is a server-side solution that creates graphs. Dynamically created graphs can be exported in jpeg, swf, pdf, and many more. It works with ASP.NET (C# and Visual Basic), ASP, PHP and JSP. I’ve recently installed it on a Linux remote server via SSH. Here are the commands:

1. Login as root.
2. On root go to, cd /usr/local/
3. wget http://
(where the tar.gz is located. you can download an evaluation version )
4. then, tar -xvzf swiffchart.tar.gz
(to extract the file and install)

By the way, You can view the demos and get the sample script from there to try it out.

So that’s it. :) Thanks guru tonio for item C.

Follow

Get every new post delivered to your Inbox.