Dream House

Courage to do something…

Archive for the 'PHP' Category


Paging Class using PHP and MySQL

Posted by Mohammad Sajjad Hossain on 24th April 2008

Based on my last post on paging using PHP and MySQL, I have coded this class. It is very easy to implement and it will save your time. Here is a code snippet to show how simple to use it.

//include the class file
include('Pager.php');
 
//making connection to the database
mysql_connect('localhost', 'root', '');
mysql_select_db('test');
 
//prepare SQL
$sql = 'SELECT * FROM books';
 
//create an object of Pager passing the SQL
$pager = new Pager($sql);
 
//set the url. this is the current page
$pager->url = 'index.php';
 
//set number of rows. by default it is 10
$pager->rowPerPage = 5;
 
//build the pager
$pager->build();
 
//get paged data
$rows = $pager->getPagedData();

Click the download link given below for a copy of the class. It also includes an working example.

Download

Share/Save/Bookmark

Posted in My Works, PHP | 3 Comments »

Paging using PHP and MySQL

Posted by Mohammad Sajjad Hossain on 9th March 2008

I have coded a class for paging, you may find it here.

### Code for Paging Starts ###
//index no of the page
$pageIndex = ((int)$_REQUEST['pageIndex'] == 0) ? 1 : (int)$_REQUEST['pageIndex'];
 
//number of rows
$rowPerPage = 10;
 
//counting total number of rows
$sql = 'SELECT COUNT(*)
        FROM {your_table_name}
        WHERE {IF ANY CONDITION}';
 
$query = mysql_query($sql, $link_identifier);
$row = mysql_fetch_row($query);
 
// total records
$totalRecords = $row[0];
 
// number of pages
$totalPages = ceil($totalRecords/$rowPerPage);
 
if($pageIndex > $totalPages)
{
    $pageIndex = $totalPages;
}
 
$startIndex = ($pageIndex - 1) * $rowPerPage;
 
if($totalRecords > $rowPerPage)
{
    $endIndex = $startIndex + $rowPerPage;
}
else
{
    $endIndex = $totalRecords;
}
 
if($pageIndex == $totalPages)
{
    if($totalRecords > $totalPages * $rowPerPage)
    {
        $endIndex = $totalRecords;
    }
}
 
if($totalRecords > 0)
{
    $rangeStart = $startIndex+1;
 
    if(($totalRecords > $rowPerPage) || ($pageIndex == $totalPages))
    {
        $rangeEnd = $totalRecords;
    }
    else
    {
        $rangeEnd = $endIndex;
    }
 
    $range = 'Showing '  . $rangeStart.' - '.$rangeEnd.' of '.$totalRecords;
}
 
// link for next page
$nextLink = 'page_name.php?pageIndex='.($pageIndex - 1);
 
// link for previous page
$previousLink = 'page_name.php?pageIndex='.($pageIndex - 1);
 
// fetching data using pagination
$sql = "SELECT *
            FROM {your_table_name}
            WHERE {IF ANY CONDITION}
            LIMIT $startIndex, $rowPerPage";
 
$query = mysql_query($sql, $link_identifier);
 
### Code for Paging Ends ###

Now use the resource ‘$query’ and do what you want to do.

Share/Save/Bookmark

Posted in PHP | 1 Comment »

Enabling cURL in XAMPP

Posted by Mohammad Sajjad Hossain on 25th February 2008

By default cURL is disable in XAMPP installation. If you want to enable cURL of your XAMPP environment then uncomment the following line:

;extension=php_curl.dll

to

extension=php_curl.dll

in the C:\Program Files\xampp\apache\bin\php.ini file (assuming you have installed XAMPP in “C:\Program Files” folder).

If you plan to switch version of PHP in XAMPP you may made the change in following files (assuming you have installed XAMPP in “C:\Program Files” folder):

  • C:\Program Files\xampp\php\php5.ini
  • C:\Program Files\xampp\php\php.ini
  • C:\Program Files\xampp\php\php4\php.ini
  • C:\Program Files\xampp\php\php4\php4.ini

Share/Save/Bookmark

Posted in PHP | 2 Comments »

XAMPP - Sending test mails with MercuryMail

Posted by Mohammad Sajjad Hossain on 28th January 2008

XAMPP comes with MercuryMail. To use it for sending test mails, you can do the followings:

1. Start Mercury Mail from XAMPP control panel.
2. Navigate to Mercury Mail folder in XAMPP installation (Most probably C:\Program Files\xampp\MercuryMail)
3. Double click on mercury.exe and Mercury window will be opened.
4. Go to Configuration -> Local Users
5. Add a user with password (Suppose sajjad)
6. Close this window.

That’s it.

Now you can send mail to sajjad@localhost or sajjad@localhost.com (Mecury Mail should be started).

To receive mail configure outlook express with localhost as pop3 and SMTP server.

Hope these information will be helpful.

Share/Save/Bookmark

Posted in PHP | No Comments »

XAMPP - My favorite PHP environment

Posted by Mohammad Sajjad Hossain on 28th January 2008

XAMPP

When I started coding in PHP, the main hassle was preparing the development environment. Installing Apache, PHP, MySQL was not so easy. Then I came across PHPTried and after that WAMPP. But finally I got what I wanted - XAMPP. To me XAMPP is a total PHP development environment.

XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl. XAMPP is really very easy to install and to use - just download, extract and start.

To get a copy of XAMPP, visit http://www.apachefriends.org/.

Share/Save/Bookmark

Posted in PHP | No Comments »