Posts tagged ‘php’

SEO Correction in PEAR::Pager

Pagination is a common practice to show big volume of data. It is quite important if you are showing dynamically populated data.

For myself I mostly use PEAR::Pager to display paginated data. If you are familiar with this package then you know that you can use 2 types of pagination display, as – sliding and jumping. In case of sliding display with a common practice is to use it with constructor option append:true and urlVar:<variablename>.

Let me show an example of this kind of usage -

  1. $Params = array(
  2.           ‘itemData’ => $dataDetails,
  3.           ‘perPage’ => $ViewPerPage,
  4.           ‘append’ => true,
  5.           ‘separator’ => ‘|’,
  6.           ‘spacesBeforeSeparator’ => 1,
  7.           ‘spacesAfterSeparator’ => 1,
  8.           ‘clearIfVoid’ => false,
  9.           ‘urlVar’ => ‘page’,
  10.           ‘useSessions’ => true,
  11.           ‘closeSession’ => true,
  12.           ‘mode’  => ‘Sliding’,
  13.           ‘importQuery’ => true,
  14.           ‘linkClass’ => ‘LinkStyle’,
  15.           );
  16. $Pager = Pager::factory($Params);
  17. $DataDetailsInArray = $Pager->getPageData();
  18. $PaginationLinks = $Pager->getLinks();

Now if you print/echo the $PaginationLinks, then you will get pagination like 1 2 3 4 etc.

Now let me describe you the problem:
Say your page url is mydata.php. So mydata.php page will show the first page of paginated data. while using the pagination links you will get a page with url mydata.php?page=1. This page shows the first page of paginated data. Now you are having a page with 2 different url like mydata.php and mydata.php?page=1. In SEO this is known as duplicate content. This is not good if you are seriously deal with SEO.

Here is the correction for this. This is simple but effective.

  1. if(isset($_GET[‘page’]) && $_GET[‘page’] > 1)
  2. {
  3.    $PaginationLinks[‘all’] = str_replace(array(‘?page=1"’, ‘&amp;page=1"’), ‘"’, $PaginationLinks[‘all’]);
  4. }

Use this code section just after generating $PaginationLinks. Hope this will help you. As always comments/suggestions are welcome. But spammers please don’t waste your time. :)

Freetag PEAR DB Version

While working with tagging application my favourite is freetag by Gordon Luk. It is a comprehensive open source tagging and folksonomy code in php. The source code is also hosted in Google Code. The best feature of it is you can use this code with little modification to fit your requirement. First time I used it in advaitaashrama.org for their book store application. Currently I am using it in another website which is under development.

I am having an issue of compatibility with this code and I am writting this post for it. If you look at the code you will find that it uses the ADODB Library for database operations. In my case I am happy with PEAR::DB. Now for me to use this code I need to use 2 different database component as PEAR::DB and ADODB Library. It seems useless to me. So I decided to make the code compatiable with PEAR::DB. While working upon the compatibility modification I also made 3 changes as

  1. I have written a function as show_debug_text() which is a replacement of debug_text().
  2. I have removed the silly_list() function from my code as it was declared as deprecated.
  3. I have renamed the main class file from freetag.class.php to freetag.db.class.php

Here is the sample code for use this

  1. <php?
  2. require_once("/path-to/freetag.db.class.php");
  3. $DbObj = DB::connect($dsn, $options); //this is the database connection object using PEAR::DB
  4. $OptionArray = array(
  5.                ‘table_prefix’ => ‘mytags’,
  6.                );
  7. $TagObj = new freetag($DbObj, $OptionArray);
  8. // Use this object to call tag related functions .
  9. ?>

Here is the code modified by me.
If you use this code please let me know if you have any problem.

PHP serialization or Json

Information storage and retrieval is an important job while designing or developing an application. We all know how to store data with normal data types in database or file systems. Here I am going to discuss about storage and retrieval of data having complex type. Complex type means they are not like normal string or number or boolean values. Complex type means data structure or objects.

Think of a situation where you are working with some array. Now there may be some situations where you need to store the array and retrieve it back. If you are familiar with OOPS concept then objects are another type which you may need to store and retrieve.

Serialization is the process by which you can convert an object or data structure into a sequence of bits which can stored and retrieve back. Serialization is also known as deflating or marshalling. The opposite process (converting the serialized sequence of bits to object or data structure) is known as Deserialization or inflating or unmarshalling. Most of the important languages have their own implementation of these processes. In Java provides automatic serialization by implementing the java.io.Serializable interface. In perl there are modules like Storable or FreezeThaw. Python implements serialization through the standard library module pickle. In PHP there are two built-in functions as serialize() and unserialize() for this purpose. However as I will continue the discussion with PHP then you should be aware of the fact that – there is a difference in the implementation of serialization in PHP 4.x and PHP 5.x.

In last couple of months I need to work with JSON (JavaScript Object Notation). JSON is an ideal data interchange format which is language independent. I had used the JSON to design some Ajax driven functionality in one of my projects. Over there I had encoded into JSON some normal and associative PHP array and passed them to access by JavaScript.

After successful completion of the project I started working upon the possibilities of storing data structure or object in PHP using JSON. IN case of PHP there are two function as json_encode() and json_decode(). Interesting thing with json_decode() is that it takes a boolean argument as second parameter[$assoc] which is by default false. But when TRUE json_decode() returns objects converted in associative array. After doing a lot R&D I find out certain facts which favors JSON rather than using serialize() /unserialize(). In general if you consider the size of the stream produced by serialize() with json_encode(), later produces small sized stream. While working with data structure like arrays JSON produces the same structure after encode and decode. So in my opinion usage of JSON is better than PHP serialize()/unserialize().

Now while working with objects, JSON produces the same result as PHP serialization functions. However if the object is complex or if you like to take the benefit of magic methods object oriented PHP5 then its better to use serialize()/unserialize(). As serialize() will attempt to call _sleep() [if exists] which can be used to do some last minute clean up of the object prior to serialization. Same way unserialize() will call _wakeup()[if exists] which can be used to do some reconstruction in the object just after unserialization. So if your objects are simple then use JSON otherwise go for serialize()/unserialize().

In the conclusion I will prefer to use JSON rather than PHP serialization methods. I won’t be surprised if I get good nos of comments for/against my preference.

QuickForm and AutoSuggest

The best example of auttosuggest filed I can instantly think of is Google. While designing dynamic forms I felt the requirement of such field. As I mostly use PEAR class and HTML Quickform to design dynamic forms in php. However in HTML Quickform there is autocomplete type but still there is no support for autosuggest. I googled a lot to get such support, but found it out in RFC of HTML Quickform. So I started working upon it. My first job was to found out a autosuggest Javascript code suitable for integration. I searched out an autocomplete javascript code by Beau D. Scott

The reasons why I selected Beau’s code is :
The code is simple to use but robust and has good documentation.
It uses standard JS library like prototype and scriptaculous.

Now having previous knowledge of integration of QuickForm and Jscalender I created the code for autosuggest class. This autosuggest class file contains constructor function, toHtml function which generates the HTML code for the field, getFrozenHtml function to return the autosuggest content in HTML and finally the registering process of the autosuggest type with HTML QuickForm. I put this autosuggest class name as autosuggestwce. View this wceautosuggest.php file.

To use this autosuggest field in HTML Quickform you need to have 3 javascript files as prototype.js, scriptaculous.js and autocomplete.js. Prototype Javascript is used for javascript framework, Scriptaculous for show and hide effect and AutoComplete is the main javascript functionality file. Among them scriptaculous.js can be removed if you don’t like to show effects, but you need to modify certain section in the wceautosuggest.php file. You also need to declare $GLOBALS['_HTML_QUICKFORM_AUTOSUGGEST_BASEPATH'] the global path defining variable for the javascript files path in the page where you will generate the form.

This autosuggest uses php script to populate values. The name of the php script has to be mentioned in the $attribute array for the key ‘fileref’.

If you are using DB_Table here is snippet of code

  1. ‘user_name’ => array(
  2.                ‘type’ => ‘varchar’,
  3.                ‘size’ => 255,
  4.                ‘qf_type’ => ‘autosuggestwce’,
  5.                ‘qf_label’=> ‘UserName:’,
  6.                ‘require’ => false,
  7.                ‘qf_attrs’ => array(
  8.                            ‘id’ => ‘user_name_autosuggest’,
  9.                            ‘autocomplete’ => ‘off’,
  10.                            ‘fileref’ => ‘getusersname.php’,
  11.                                  ),
  12.                   ),

If you are using QuickForm directly then here is the code snippet

  1. $form = new HTML_QuickForm(‘someTest’, ‘get’);
  2. $form->addElement(‘autosuggestwce’, ‘username’, ‘UserName:’, "array(‘id’ => ‘user_name_autosuggest’, ‘autocomplete’ => ‘off’, ‘fileref’ => ‘getusersname.php’)");
  3. $form->display();

Here is the code segment of php script

  1. <?php
  2. $UsrObj = new CLUsrChk($DBObj, ‘hllqz_tblusers’);
  3. $SqlStatement = "SLECT user_name FROM ".$UsrObj->table." WHERE user_name LIKE (‘".$_GET[‘s’]."%’)";
  4. $UsrNameArray = $UsrObj->bd->query($SqlStatement);
  5. echo json_encode($UsrNameArray);
  6. ?>

Here is the screenshot

autosuggest_field
autosuggest_filed_populated

However if you require the files for your reference please let me about that.

I was planning to do this post almost a month ago but I was busy with learning another Javascript framework jQuery. I am started loving it. Hope I will be able to post something about jQuery in near future.

QuickForm and Jscalendar

Most of us are familiar with calendars in web pages. Small calendar showing date and year are really good to see and useful too. While in case of forms we have seen calendars attached with date fields. In some of my earlier projects I have designed date fields using simple drop down menu containing the data, month and year menu. It’s good but not very user friendly as user need to select 3 menus to choose a certain date. I had to stick with that format as I mostly use PEAR HTML_QuickForm to design forms in the web pages. Among many reasons the most unavoidable one is that it automatically generates client side validation JavaScript code. With time I realized that I should find some options to integrate calendar object with the QuickForm.

After giving a thorough search I selected jscalendar to incorporate with my form elements. The reasons behind selecting jscalendar are:

  • It has a nice interface and wide variety of themes.
  • Multiple integration options along with selectable date format to put the date in the text field.
  • Support for integrating the calendar object with HTML_QuickForm.

Prior to integrate the jscalendar I need to upgrade HTML_QuickFom version to 3.2.10 as I found some code added in this version to support jscalendar. I also need to upgrade my PEAR DB package to version 1.7.13 and DB_Table to version 1.5.5. In my projects most of the cases I generate forms using DB_Table instead of using QuickForm directly. This gives an extra benefit for data validation and storing them in database.

After the up-gradating the required pear packages I worked upon integrating the jscalendar code. After a spending a moderate amount of time I was able to work jscalender with the QuickForm. But unfortunately when I was trying generate forms using DB_Table jscalendar component was not working. I was getting javascript errors.
I was following the method described by Philippe Jausions of 11abacus in jscalendar.php file. It was working fine with the QuickForm. In that case I was using addElements function to add jscalendar with proper options using the $option array like this –

  1. $options = array(
  2.            ‘baseURL’ => ‘../js/jscalendar/’,
  3.            ‘styleCss’ => ‘calendar-win2k-1.css’,
  4.            ‘language’ => ‘en’,
  5.            ‘button’   => ,
  6.            ‘setup’ => array(
  7.                       ‘inputField’ => ‘datefield1′,
  8.                                       ‘ifFormat’ => ‘%d.%m.%Y %H:%M’,
  9.                                       ‘showsTime’ => true,
  10.                                       ‘time24′ => true,
  11.                                       ‘weekNumbers’ => false,
  12.                                       ‘showOthers’ => true
  13.                                       ),
  14.            );
  15. $form->addElement(‘jscalendar’, ‘ datefield1′, ‘My Date’, $options);

I used this same option array in the column definition for the same field, as DB_Table uses the column definition for the attributes of the form elements of the form. I written like

  1. ‘datefield1′ => array(
  2.                 ‘type’    => ‘varchar’,
  3.                 ‘size’    => 20,
  4.                 ‘qf_type’ => ‘jscalendar’,
  5.                 ‘qf_label’=> ‘<span class="caption">Date:</span>’,
  6.                 ‘require’ => false,
  7.                 ‘qf_attrs’ => array(‘class’=> ‘formfield’,‘id’ => ‘datefield1′),
  8.                 ‘qf_opts’ => array(
  9.                           ‘baseURL’ => ‘../js/jscalendar/’,
  10.                           ‘styleCss’ => ‘calendar-win2k-1.css’,
  11.                           ‘language’ => ‘en’,
  12.                           ‘button’   => ,
  13.                           ‘setup’ => array(
  14.                                      ‘inputField’ => ‘listadata’,
  15.                                      ‘ifFormat’ => ‘%d.%m.%Y %H:%M’,
  16.                                      ‘showsTime’ => true,
  17.                                      ‘time24′ => true,
  18.                                      ‘weekNumbers’ => false,
  19.                                      ‘showOthers’ => true
  20.                                      ),
  21.                                   ),
  22.                 ‘qf_client’=> true,
  23.                                ‘qf_rules’ => array(
  24.                                           ‘required’ => ‘Date is required.’,
  25.                                                 ),
  26.                 ),

It didn’t worked and also gave the javascript error as calendar.Setup – no such method found….
Now I started working on how to fix this problem. I started checking the all the codes thoroughly and found that using QuickForm from DB_Table setting all these options can’t be done. After spending some upon those codes I rewrite the column definition like this

  1. ‘datefield1′ => array(
  2.                 ‘type’    => ‘varchar’,
  3.                 ‘size’    => 20,
  4.                 ‘qf_type’ => ‘jscalendar’,
  5.                 ‘qf_label’=> ‘<span class="caption">Date:</span>’,
  6.                 ‘require’ => false,
  7.                 ‘qf_attrs’ => array(‘class’=> ‘formfield’,‘id’ => ‘datefield1′),
  8.                 ‘qf_opts’ => array(
  9.                              ‘ifFormat’ => ‘%d.%m.%Y’,
  10.                              ‘showsTime’ => false,
  11.                              ‘time24′ => false,
  12.                              ‘weekNumbers’ => false,
  13.                              ‘showOthers’ => false,
  14.                              ‘button’ => ,
  15.                              ),
  16.                 ‘qf_client’=> true,
  17.                 ‘qf_rules’ => array(
  18.                               ‘required’ => ‘Date is required.’,
  19.                               ),
  20.                 ),

Also I declared the global path defining variable in the page where I was generating the form. I defined

  1. $GLOBALS[‘_HTML_QUICKFORM_JSCALENDAR_BASEPATH’] = "/js/jscalendar/";

Oh that time it worked. It showed the small calendar while the clicking the small button beside the text field for data. But I wasn’t still happy with it. What I found that the calendar was coming but it wasn’t using any style and dates are raveling upon a transparent layer, making it too ugly. I tried to define the style using style class/ stylesheet file name in different places as different parameter. But nothing worked. Now I had two options then

Define the stylesheet for the calendar within the head tags of the page containing the form, means hardcode the name of the stylesheet.
Change the coding to accommodate to options for setting the style class for the calendar.

Selecting the 1st option means loosing some dynamicity and selecting the 2nd option means I need to change or modify certain code in pear class package files. So it was difficult to select any option. I was investigating the benefits and the hitches which can come out as a result of using any one of those options. Also side by side I was looking for third other option which can be better then these two. I spent almost two days and came up with a decision of selecting the second option. I decided to add two lines of code in the QuicfForm.php file in the DB package as

  1. if(isset($col[‘qf_theme’])) {
  2.               $element->theme = $col[‘qf_theme’];
  3. }

Also I have added one more element in column definitions for the date field as

  1. ‘datefield1′ => array(
  2.                 ……    …….
  3.                 ……    …….
  4.                 ‘qf_theme’ => ‘calendar-win2k-2′,
  5.                 ),

Now my problem is solved and the jscalendar is working seamlessly with the QuickForm from the DB_Table package too.

My opinion is if you like to add the two lines of code in QuickForm.php file you can add it but you need to remember it at the time when you upgrade the pear package. Also you need to check if it works properly or not. The other way is to hardcode the stylesheet file within the head tag part of the page where you will go to generate the form. In this case you will surely loose some dynamicity but this is easier to work with. Choice is yours.

Currently I am working upon another important issue in php. I need some time to accomplish this job. I will discuss this issue too cause I find it very interesting. However if you find this above discussion useful then I might expect your comment here. Suggestion and queries are also welcome.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes