Skip to main content

Posts

Google like Pagination

In the beginning of my career, every technical interview has this question; "How do you write pagination script in PHP?". I think you too faced the same issue. It was very important to answer this because every application is developed to list set of information and off-course you can't show every thing in a single page. It will irritate the user to scroll to the bottom of page to read. So, the pagination. Divide the list of items in number of pages and navigate to each by clicking on each page number displayed. At the beginning, the pagination was simple was limited to 10-15 pages, but after some time in real time the requirement has grown to 10 multiple pages. Solution was to show first few page numbers, last few page numbers and in middle show the ellipsis(...). As you navigate to any page it would show definite page numbers to both the sides. /** * Pagination file * * This file provides the google like pagination solution * * @category Saral * @packag...

Validation and Submission of form using jQuery :: Easiest Solution

In any application, form validation and submission to server is so common and redundant; there are multiple ways of doing it. And the best and easiest way that I do use or recommend is jQuery solution.  Most popular jQuery plugins are the best solution, they have the best solution ever.  One, jQuery Validator written and maintained by Jorn Zaefferer, a member of the jQuery team, lead developer on the jQuery UI team and maintainer of QUnit. Two, jQuery Form by malsup, it has various plugins and solutions. Another plugin that I like from same developer is jQuery BlockUI . (give a try). Name: Email: Note: To avoid unexpected behavior, maintain same form and fields names and id selector values. Need further explanation, let me know.

Email already exists using jQuery

It is the common requirement for any web application either it is PHP, .net, JSP etc., which has users involved. When creating/adding user from admin or end user registration we should check whether username or email address already exists. It will help to have unique registrations to our application and avoid confusion between various users.  I see many developers struggle with this by submitting the form to server side script and validate their from database, in case username/email address already exists they take the pain to go back to previous page or form and show the error message with other form data filled.  Few use AJAX to send the information onblur or onkeyup or onkeydown but face difficulties to stop form submission. Here is the simple solution that would help in validating username or email address existence without much hassle.  Solution comes from popular jQuery Validator plugin I'm using this from ages.  HTML Code: <html> <head...

Sorting second dimension array

We can sort the second dimension array like table, for example $users = array( array('name' => 'Mr. B', 'age' => 34), array('name' => 'Mr. A', 'age' => 33), array('name' => 'Mr. C', 'age' => 32) ); If you want to sort the array based on the name or age, here is the solution: function arraySortByColumn(&$arr, $col, $dir = SORT_ASC){ $sort_col = array(); foreach ($arr as $key => $row) { $sort_col[$key] = $row[$col]; } array_multisort($sort_col, $dir, $arr); } arraySortByColumn($users, 'name', SORT_DESC); print_r($users);

Connect MySQL Remotely, Amazon EC2 using MySQL Workbench

Login to AWS Management Console. Under the security group, add inbound rule for MySQL. First login to EC2 instance using SSH, then login to mysql  mysql -hlocalhost -uroot -p provide the password. Once you are in mysql prompt CREATE USER 'testuser'@'%' IDENTIFIED BY 'testpwd' GRANT ALL PRIVILEGES ON *.* TO 'testuser'@'%' WITH GRANT OPTION FLUSH PRIVILEGES Note: Setting host to '%' may be harmful, you can set your IP address to access the MySQL from your IP address only Now open the my.cnf file from /etc/mysql and search for bind-address, the default value will be bind-address = 127.0.0.1 change it to  bind-address = 0.0.0.0 That's it now go to MySQL workbench, create connection with Host: Username: testuser  

MySQL Simple JOINS

Simple Example : Lets say you have a Students table, and a Lockers table. Each student can be assigned to a locker, so there is a " Locker Number " column in the student table. More than one student could potentially be in a single locker, but especially at the  beginning  of the school year, you may have some incoming students without lockers and some lockers that have no students assigned. For the sake of this example, lets say you have  100 students , 70 of which have lockers. You have a total of  50 lockers , 40 of which have at least 1 student and 10 lockers have no student. INNER JOIN  is equivalent to " show me all students with lockers ". Any students without lockers, or any lockers without students are missing. Returns 70 rows LEFT OUTER JOIN  would be " show me all students, with their corresponding locker if they have one ". This might be a general student list, or could be used to identify students with no locker. Returns 100 r...

Find relative duration of an event using PHP for given date time (seconds ago, minutes ago, hours ago, days ago, weeks ago, months ago, years ago)

You might have seen that many of the live applications shows the time relative to when it was posted as seconds ago, minutes ago, hours ago, days ago, weeks ago, month ago, year ago and so on. Instead of showing the whole date and time of any possible action on the application it is shown with smaller units, more simpler ones like a minute ago, 15 minutes and 10 seconds ago etc. Here is how we achieve it using PHP, below are two methods that can be used, it requires a Date Time string in YYYY-MM-DD HH:II:SS format. Method One: /** * returns the time ago in string * @param string $date_time * * @return string */ function timeAgo($date_time) { $time_ago = strtotime($date_time); $cur_time = time(); $time_elapsed = $cur_time - $time_ago; $seconds = $time_elapsed; $minutes = round($time_elapsed / 60); $hours = round($time_elapsed / 3600); $days = round($time_elapsed / 86400); $weeks = r...