Category Archives: PHP

Get HREF Atribute of anchor tag by class name in PHP

I was looking for extracting href value from html link(a) tags in PHP. Came up with the following solution. It’s useful to extract links from a website


function get_href_attribute("Google","a","my_tag")

function get_href_attribute($html,$tag,$class)
{

   // Load the HTML in DOM
   $doc = new DOMDocument();
   $doc->loadHTML($html);

   // Then select all anchor tags
   $all_anchor_tags = $doc->getElementsByTagName($tag);
   $href_array = array();
   foreach ($all_anchor_tags as $_tag) {
      if ($_tag->getAttribute("class") == $class) {
         // If match class get the href value
         $href_array[] = $link->getAttribute("href");
      }
   }
  return $href_array;
}

Call JavaScript functions from Flash AS3

For one of my project clients want to use Lightbox in there website which is fully build of Flash AS3. I was wondering how to do that?
It will take time to make a gallery in Flash like Light Box. I was looking online if is there any way to call JavaScript functions from Flash.
Following is the simplest way to call JavaScript from Flash AS3.

The code which is required to call the function in AS3 is

import flash.external.ExternalInterface;
ExternalInterface.call("myFunction()");

Here myFunction() is the JavaScript function which is already there in my HTML page.
Very simple to call JS fuctions. By this I made my life easier by giving the almost same
feelings to my client of Flash and JQuery animations. Which might take more effort if
I try to make the same animation in Flash.

Download sample Example

test.zip

My notes on “Getting Real” Book by 37signals

Getting Real is one of the great book by 37signals for developer, designer or entrepreneur. I love the book. A must read book. I wrote some notes from the book for myself.
So I can review it anytime. I thought it might be helpful for others so I shared my notes down. You can read the book free online Getting Real. All the credits goes to 37signals.
Thanks for writing this book.

My Notes for review

The Starting Line Chapter 2-3

  1. Deal with the actual problems you’re trying to solve instead of your ideas about those problems.
  2. Do less than your competitors to beat them.
  3. Solve your own problems. You’ll be the target audience and you’ll know what’s important and what’s not.
  4. Focus on building a quality tool that you and your customers can live with for a long time.
  5. Scope down. It’s better to make half a product than a half-assed product.
  6. Best way to know what your app should be is to know what it shouldn’t be.
  7. Keep your app small and manageable so you can actually enjoy the process.
  8. Let’s focus on what we need to focus on right now.
  9. The leaner you are, the easier it is to change.
  10. Start with a developer, a designer, and a sweeper (someone who can roam between both worlds).
  11. Metcalfe’s Law, that “the value of a communication system grows at approximately the square of the number of users of the system,”
  12. Let limitations guide you to creative solutions.
  13. Smaller companies are closer to the customer by default.
  14. Differentiate yourself from bigger companies by being personal and friendly.

    Priorities Chapter 4

  15. What does your app stand for? What’s it really all about?
  16. Work from large to small. Always.
  17. Don’t waste time on problems you don’t have yet
  18. Find the core market for your application and focus solely on them
  19. If you try to please everyone, you won’t please anyone
  20. Don’t think about scaling problem until you have one.

Feature Selection Chapter 5

  1. Build half a product, not a half-ass product
  2. Cut out the work and thinking that just don’t matter, you’ll achieve productivity you’ve never imagined.
  3. Innovation is not about saying yes to everything. It’s about saying NO to all but the most crucial features.
  4. Build something you can manage. It’s easy to make promises. It’s much harder to keep them.
  5. Build software for general concepts and encourage people to create their own solutions.
  6. Let your customers remind you what’s important. You can’t forget what’s important when you are reminded of it every day.
  7. Ask people what they don’t want. If you could remove one feature, what would it be?

Process Chapter 6

  1. Get something real up and running quickly. It’s ok to do less, skip details and take shortcuts in your process if it’ll lead to running software faster.
  2. Work in iteraions. Don’t expect to get it right the first time. Let the app grow and speak to you.
  3. Come up with ideas (Brainstorm), Sketch it in paper, Make mock up html and then go for coding.
  4. Avoid Preferences like chosing 25,50 or 100 results per page. Don’t put the burden on users while it’s your responsibility.
  5. Value the importance of moving on and moving forward. Take Decisions quickly as accepting that mistakes will happen and realize it’s no big deal as long as you can correct them quickly.
  6. Test your app via real world usage. Get real data, Get real feedback then improve based on that info.
  7. Break the Project in smaller chunks. Break 30 hours into smaller 6-7 hours.

The Organization chapter 7

  1. Unite the team. Hire people with multiple talents who can wear different hats during development.
  2. Make or manage some time at least half of your working time alone without any interruption like email response, phone call, making coffee etc. Make this time completely alone and concentrate in your work.
  3. Don’t have meetings. Reduce meetings as much as you can.
  4. Seek and celebrate small victories. “What can you do and release in 4 hours?” . A simple feature or update and release it.

Staffing Chapter 8

  1. Don’t hire unless you feel extreme necessity.
  2. Give a test project to your new employee.
  3. Hire based on open source contribution.
  4. Hire someone who can wear different hats. Designer who can also write and programmers who understand design.
  5. Hire someone who’s enthusiastic. Someone you can trust to get things done.
  6. If there is few people to fill a position hire good writers.

Interface Design Chapter 9

  1. Design the interface before start programming. Sketch it or making html is cheap and easy to change.
  2. Start designing from core of the project(epicenter). The most important piece of content first then go for 2nd important, 3rd…etc.
  3. Design for regular(The screen people see always), Blank(The screen people see first time) & Error(The screen people see when something goes wrong).
  4. Don’t focus only regular state focus also on blank slate. Because this is the first impression of your project for the users.
  5. Design for when thing goes wrong. Defensive design is like defensive driving.
  6. Consistency is not necessary. Give people what they need, when they need it and get rid of what they don’t. It’s better to be right than to be consistent.
  7. Every letter matters. You need to speak the same language as your audience too.
  8. One Interface for admin and public interface. Just build add, edit and delete function to regular application interface.

Code chapter 10

  1. Less software, less code. Keep your code as simple as possible.
  2. Choose tools that keep your team excited and motivated.
  3. Listen to your code. It will offer suggestions, it will push back and it will tell you where the pitfalls reside.
  4. Regularly put aside some time to pay off your code and design debt.
  5. Open the door for your app. Get data out into the world via RSS, APIs…etc..

Words chapter 11

  1. Don’t write a functional specifications document.
  2. Eliminate unnecessary paperwork.
  3. Write a quick story instead of details.
  4. Insert actual text instead of lorem ipsum.
  5. Think of your product as a person and give it a personality(strict, funny, serious etc..). Use them to guide copywriting, interface and the feature set.

Pricing and Signup chapter 12

  1. Give something away for free.
  2. Make it as easy as possible to get in and get out of your app for the user. Make signup and cancellation a painless process.
  3. No one likes long term contracts, early termination fees, or one time set-up fees. So avoid them. Try to make products bill on a month to-month basis.
  4. Soften the blow of bad news with advance notice and grandfather clauses.

Promotion chapter 13

  1. Go from teaser to preview o launch.
  2. Promo Site with overview, tour, screen captures and videos, manifesto , case studies, buzz, forum, pricing & sign-up and weblog.
  3. Blogging is be more effective than advertising and it’s cheaper.
  4. Share your knowledge with the world and promote through education.
  5. New or interesting features are a great way to generate buzz for your application.
  6. Check who’s is talking about you? Who’s bitching about you? Leave comments on those blogs and thanks people for posting your link.
  7. Promote upgrade opportunities inside the app.
  8. Give your app a name that’s easy to remember instead of a big name. Don’t sweat it if you can’t get the exact domain name

Support chapter 14

  1. Developers/Designers should act as also support with customer. They should hear and answer the customer.
  2. Use inline help and FAQ’s so you wont’ need a manual or training for your customers.
  3. Answer quick to your to your customer email. Don’t use more that 1 hour to answer them.
  4. Don’t consider all the request from the customer. Try to say no.
  5. User forums or chat to let customers help each other. You’ll be surprised how much want to help one another.
  6. Get bad news out to customer as soon as possible. Be honest to your customer. Good news should be trickled out slowly.

English To Arabic Number Converter

Some of the middle east project which is Arabic Site our clients want Arabic Numbers Like “٩٢٠٠١٥٠١٠” which is “920015010”. Even though it’s not Arabic number but they want to use it in their sites. So in our web projects we need to convert English “920015010” to Arabic “٩٢٠٠١٥٠١٠”.

I couldn’t find good solution for that. So I made my own class

<?php
class English2ArabicNumber {

	function English2ArabicNumber()
	{

	}

	function convert($digit)
	{
		if(empty($digit))
			return '٠';

		$ar_digit = array('٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩','-'=>'','.'=>'.');

		$arabic_digit = '';

		$length = strlen($digit);

		for($i=0;$i<$length;$i++)
		{

			if(isset($ar_digit[$digit[$i]]))

				$arabic_digit .= $ar_digit[$digit[$i]];

		}
		return $arabic_digit;
	}
}

$ekram = new English2ArabicNumber();
echo $ekram->convert('12345');
?>

OUTPUT:

١٢٣٤٥

you can download the class from phpclass.org site

[Download]

Flash(AS2) + PHP + MySql

Last couple of month was working on Flash. Became familiar with Flash and started loving it… 🙂 Then a project came they
asked for making CMS for their flash site. You can see the site http://www.zfp.com

And the game started… 🙂

Started learning AS2 and loadvars. Faced lots of difficulties and finally manged to communicate with Flash(AS2) + PHP + Mysql.
In this post I will try to give basic idea to communicate Flash(AS2) and PHP.

1. Flash(AS2)

lv_th = new LoadVars();

lv_th.onData = function(varText:String)

{

 temp = varText.split('@@');

 photo= temp[0].split('|');

 photo_title = temp[1].split('|');

 photo_description = temp[2].split('|');

};

lv_th.load("photo_gallery.php?gallery_id=5");


1. PHP+MySql


$gal_id = $_GET['gallery_id'];
$sql = "SELECT * FROM gallery WHERE gal_id=$gal_id ORDER BY gal_id";
$result = mysql_query($sql) or die(mysql_error());
$photo = array();
$photo_title = array();
$photo_description = array();

while($rows = mysql_fetch_object($result))
{
    $photo[]             = $rows->photo;
    $photo_title[]       = $rows->title;
    $photo_description[] = $rows->description;
}

print implode('|',$photo) . '@@' . implode('|',$photo_title) . '@@' .
implode('|',$photo_description);


First one is flash AS2 Code to get the data from php. It first create a new instance LoadVars();

then onload called the photo_gallery.php?gallery_id=5 and wait for the response to come.

In php we made the query based on the gallery_id and get the data and put it in three separate

array. then we implode each array values with ‘|’ and separate each array with ‘@@’.

In flash after getting the data as a string in varText variable we split it by ‘@@’ to separate the

different array values and after that separte it in ‘|’ to get each value. Now we have all the data

in photo, photo_title and photo_description array. Use the array values as you want…. 🙂

Note: In the data there should not be ‘@@’ or ‘|’. If it contains then you have to choose the separator carefully. Choose a separator which is not come as data.

Have fun with flash + php + MySql….. 😉

Became ZCE

I am very happy to see my name listed under Bangladesh In Zend Yellow page of ZCE. I passed the exam in first chance. Thanks to Allah and to all previous ZCE of my country. I get inspired mostly from them either by talking with them or by reading their blogs. And of course thanks goes to my class mates and friends. One of my friend is also ZCE (Mahmud Ahsan).

I bought the Zend Exam Voucher on 3rd February 2009. But for so many reasons I was unable to attend the exam and the most big reason is my laziness…:) I rescheduled the exam may be for 50 times but as my laziness comes up so I couldn’t give the exam. 3rd February 2010 is the last date of the exam and if I unable to attend the exam it will expire. So I was in a pressure to give the exam and I attend the exam on 2nd February before one day of expiry Alhamdulillah passed the exam. Now let me share with you what I did to prepare myself. But it may work with you or may not. The way of thinking of the entire human is not the same. So follow what you feel better way to prepare yourself.

First of all as all the ZCE’s will suggest you the same thing to read the
Zend Certification Guide and Use the test exam.

1.  Finish one topic from the Zend Certification Guide and search it in php.net
for details. Quote or write the things which you can use as just quick review. Do it
for all the topics of 1 Chapter and pin it. After that go to the Second Chapter follow
the same thing and review the first chapter at the same time so you won’t forget
the thing. After finishing 12th Chapter you will have 12 clips in your hand for quick review.

2.  Now you will see some of the sub topics of the 12 topics listed in Zend Site which is not
there in Zend Certification Guide. So now you have to go for Google search or php.net to learn those
things and write quotes in your note.

3. After finishing all those things now check out if you missed any subtopics. If you saw that everything is done then fix your mind to give a mock test. But 5 mock tests from zend site and give one test. After that you will realize the type of questions and lake of your knowledge in a chapter. Go now study those chapters again if you got grade pass or fail. After finishing give a second mock test. If you got excellent in all the chapter now you are ready to face the real exam. Schedule the real exam and go for it.

Rather than this you can check the following resource which I followed parallelly.

Some sample Questions Link ( I don’t know this is legal or not. If it’s illigal plz let me know so i will remove the link)
Link 1
Link 2

Review of 12 Chapter Other ZCE’s Blog
Read The Web

The Paul Paul Reinheimer Review Guide is also very useful

Download Link

Check out Frank ZCE Preparation. It also helped me lot.

Frank Blog

Download Frank Hand Note

I cant share anything else as I agreed with zend not to share. But believe me if you
are a PHP programmer for 6 month or 1 years it wont take you more then 12 days
for 12 chapter I mean for zend preparation. Thank you. Best of luck….:)

JeddahGeeks First meetup(Nov,09)

Some of my Jeddah friends Arranged a group called JeddahGeeks. Where Web developer, designer and entrepreneurs can share their knowledge, thoughts and experience. So we can learn something and gain some knowledge from some real geeks. In Our first meet up(13th November) I give a small speech on “How to Become a PHP Geek?”. I am inspired by the event of PHPExperts Seminar of Bangladesh. The presentation of Emran Hasan Brother on How Becoming a kick-ass PHP ninja gave me more inspiration to make this presentation. You can see the presentation below. As this is my first presentation any criticize and suggestion will be appreciated.

FirePHP with FireBug

Last Couple of months I was using Firebugs ad-dons of Fire Fox.  It helped me lot to debug my css. It’s simply great help for me. Today I came to know about new ad-dons FirePHP which can be used with FireBug. You can debug your php code in your browser. It sounds cool. You don’t have to use print_r or var_dump to debug your php code and massed up your site by dumping your array.

It’s great to use it like same as firebug. Hope it will be useful for php programmers.

To use FirePHP you need the following thigs

After installing those three you should download the FirePHPCore from http://www.firephp.org/HQ/Install.htm.

Setup

Let’s assume your document root is /www/ and it is in your PHP include path.

Step 1 Locate the server library

Locate the /lib/FirePHPCore folder in the FirePHPCore archive and move it into your document root.

/www/FirePHPCore/fb.php
/www/FirePHPCore/FirePHP.class.php
/www/FirePHPCore/LICENSE

You can skip this step if you are using PEAR. Make sure the PEAR repository is in your include path.

Step 2 Include the server library

Create a file at /www/Index.php and open it in your editor.

require_once(‘FirePHPCore/FirePHP.class.php’);

Step 3 Start output buffering

ob_start();

Now That’s it. It’s now time to play. Your first Log Message

$firephp = FirePHP::getInstance(true);

$var = array('i'=>10, 'j'=>20);

$firephp->log($var, 'Iterators');

Console_HelloWorld_Screenshot
FirePHP Official Site:  http://www.firephp.org

The Site I got the information: http://sixrevisions.com/web-development/how-to-debug-php-using-firefox-with-firephp/

get image width and height using PHP

Sometime we have to get the uploaded image name from database. But we don’t know the image width and height. Width and height may be variable. But we want to know the actual width and height of an image. We can use getimagesize() PHP function to get the width and height of an image.

array getimagesize ( string $filename [, array &$imageinfo ] )

This function returns an array of 7 elements. Index 0 and 1 contains respectively the width and the height of the image.

Example:

<?php

$image_Name = ‘ekram.jpg’;

list($width, $height) = getimagesize($image_Name);

echo “<img src=’$image_Name’ width=’$width’ height=’$height’ />”;

?>

How to organize PHP Project files?

I am organizing PHP Project files in following way. I don’t know this is standard or not. But it makes my life easier. If anyone knows any standard for organizing PHP files please share with us.


/webroot
/image (all images)
/dev (scripts and images under development – this is a mess)
/doc (document and pdf files that is uploaded or my work description)
/etc (configuration, i.e. database un&pw, etc – restricted perms)
/home (this is where most of the website lives)
/inc (included files)
/func (included functions)
/class (included class)
index.php