Thursday, November 27, 2014

simple pagination in php





Below is the code to do a simple pagination in php:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 5;

$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydatabase');
/* Get total number of records */
$sql = "SELECT count(prd_code) as count_prd FROM Product_tab";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval );
$rec_count = $row['count_prd'];

if( isset($_GET{'page'} ) )
{
   $page = $_GET{'page'} + 1;
   $offset = $rec_limit * $page ;
}
else
{
   $page = 0;
   $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);

$sql = "SELECT prd_code,prd_name,prd_price from product LIMIT $offset, $rec_limit";

$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval))
{
    echo "Product id :{$row['prd_code']}  <br> ".
      "Product name :{$row['prd_name']}  <br> ".
         "Price : {$row['prd_price']} <br> ".         "--------------------------------<br>";
} 

if( $page > 0 && !($left_rec < $rec_limit) )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\">Previous 5 Records</a> |";
   echo "<a href=\"$_PHP_SELF?page=$page\">Next 5 Records</a>";
}
else if( $page == 0 )
{
   echo "<a href=\"$_PHP_SELF?page=$page\">Previous 5 Records</a>";
}
else if( $left_rec < $rec_limit )
{
   $last = $page - 2;
   echo "<a href=\"$_PHP_SELF?page=$last\">Previous 5 Records</a>";
}
mysql_close($conn);
?>


See the effect here:

First page:

Middle page:

Last page:


About me




That's me Avnish Alok, the guy behind this blog, a Software Engineer, Blogger, Freelancer and a true technology geek indeed. I believe in the ideology of BENJAMIN FRANKLIN great quote "He that can have patience can have what he will". I retain all such qualities to be a technology geek i:e; spend my much more time with computer, treating my pc as a family member, awake up at every morning with more passion to know about new tools & technologies, dig deeply to know more about each stuff.
                               Apart from my profession i love to write poems and reading novels as well. I write poems of genres like inspirational and romantic.

I hold my PG degree(MCA) in Information Technology and planning to do P.H.D in the same. As I’m a blogger, it is obvious to do blogging so, CS HOT TOPICS is the outcome. With the help of this blog I share my knowledge to my friends, my colleagues and to the entire world.  


ABOUT CS HOT TOPICS: 
 CS HOT TOPICS is a technical blog for open source enthusiasts that always updates with latest tools & technologies. As the name suggest it covers hot topics of computer programming in various languages(java, jsp, servlet, php, mysql, oracle) to name a few with latest tools(ajax, jquery). You can get more to visit here http://cshotopics.blogspot.com/

You can follow me on Google+, Facebook and Twitter, If you’d like to dig more about me.

 If you’ve any Queries about Add. and content Posting, or want to provide me your feedback so that i can improve this blog further, feel free to email me at avnishalok.se@gmail.com

Wednesday, November 26, 2014

how to configure xampp to send mail in php?




In order to send mail, you have to make some changes in your files if you are using XAMPP. Configure two files, first one is C:\xampp\php\php.ini and the second one is c:\xampp\sendmail\sendmail.ini

In C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for mailing sites like gmail for your localhost.

In php.ini file find [mail function] and change

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"


Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=gmail-id@gmail.com
auth_password=gmail-password
force_sender=gmail-id@gmail.com

Note: Replace gmail-id and gmail-password in above code with yours.

Note: If you are unable to send mail using your gmail accout then you need to make some changes in your gmail account setting like change Access for less secure apps must be enable. Login into your gmail accout and follow the link Gmail setting for less secure apps to configure your gmail account setting.

Click here to get code to send mail using php

how to send mail in php from localhost?




In previous chapter you got idea to configure your XAMPP. If you've configured then move to code to send mail and if not then configure at first. click here to configure.
Now it's time to write code in order to send mail. Below is the code to send mail using php:

<?php

$to = "your-gmail-id@gmail.com";
$subject = "HTML email using PHP";

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This is an HTML Email send using php mail function.</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Avnish</td>
<td>alok</td>
</tr>
</table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: <your-gmail-id@gmail.com>' . "\r\n";

$chk=mail($to,$subject,$message,$headers);
if($chk)
echo "success";
else
echo "fail";
?>
 

Monday, November 24, 2014

Write For Us






CS HOT TOPICS is open for guest posts and blog advertisement now. If you want to write for us on computer programming and its related technologies, kindly contact us at avnishalok.se@gmail.com.
After getting your post we'll review it and if it looks good then we'll publish your post within 2 working days. Below are the list of topics for which you can write for us:

  • Computer languages and its related technologies
  • Mobile technology
  • Technical support