Thursday, March 26, 2009

Pagination in PHP

Pagination in PHP




Step One

  1. Connect to MySQL database.
For those who are new to PHP & MySQL and do not know how to establish connection with MySQL, may use the following config.php.file code.

//Host name

$host='localhost';

//User name

$user='root';

//Password

$pass='';

//Database name

$db='mydb';

//Establishing database connection

mysql_connect($host,$user,$pass);

//Select database

mysql_select_db($db) or die ("Database Connection Failed") ;

?>


Step Two
  1. Set Variables:
  2. Set table name you want to use (Table must contain an auto increment field named “Id”).
  3. Set the column name you want to show.
  4. Set number of rows per page (default is 5 rows/page).
After configure variables as defined above, simply run the script and enjoy {:-Þ

//Include config file

includes('config.php');

//Table name

$myTable='Mytable';

//Column Name

$columnName='Columnname';

//How many rows to show per pg

$rowsperpg=5;

//by default we show first pg

$pgnum=1;

//if $_GET['page'] defined, use it as first page number

if(isset($_GET['page']))

{

$pgnum=$_GET['page'];

}

//counting the offset

$offset=($pgnum-1)*$rowsperpg;

$test="SELECT * FROM '".$myTable."' LIMIT $offset,$rowsperpg";

$run=mysql_query($test) or die('selection fail');

//print

$count=mysql_num_rows($run);

if($count > 0)

{

while($res=mysql_fetch_assoc($run))

{

echo '

';

echo '

';

echo '

Event: '.$res[$columnName].'
';

echo '
'
;

}

//how many rows we have in db

$query="SELECT COUNT(Id) as numrows FROM '".$myTable."' ";

$res=mysql_query($query) or die('Error Query failed');

$row=mysql_fetch_assoc($res);

$numrows=$row['numrows'];

//how many pg we have when using paging?

$maxpg=ceil($numrows/$rowsperpg);

// print the link

$self=$_SERVER['PHP_SELF'];

$nav='';

for($page=1;$page<=$maxpg;$page++)

{

if($page==$pgnum)

{

$nav.="$page";

}

else

{

$nav.="$self?page=$page\">$page";

}

}

if($pgnum>1)

{

$page=$pgnum-1;

$prev="$self?page=$page\">[Prev]";

$first="$self?page=1\">[First Page]";

}

else

{

$prev=' ';

$first=' ';

}

if($pgnum<$maxpg)

{

$page=$pgnum+1;

$next="$self?page=$page\">[Next]";

$last="$self?page=$maxpg\">[Last Page]";

}

else

{

$next=' ';

$last=' ';

}

echo '

'.$first.' '.$prev.' '.'page'. $pgnum.' '.' of'.' '.$maxpg.' '.$next.' '.$last.'
';

}

if($count <= 0)

{

?>

<div style="color: #FF3300" align="center">

<p><h5>No Record Availableh5>p>

div>

}

?>

Wednesday, March 25, 2009

Google Docs Security Problem Solved

I am a fan of Google Docs(beta). When I was trying to find out templates on Google Docs, I felt that I got the private spreadsheet of someone. I searched for this issue on the internet but did not find a satisfied answer. Yesterday, I found the following link. Google had admitted this security problem but I think the problem has been solved.


http://www.pcworld.com/article/160927/google_docs_glitch_exposes_private_files.html

on Wikipedia

http://en.wikipedia.org/wiki/Google_docs#Data_safety_and_privacy

Friday, March 20, 2009

Search Your Network Game Using PHP

Search games on your network, simply using PHP. This script uses a cool module of PEAR, Net_GameServerQuery- a common API to query Half Life server. PEAR () is a framework and distribution system for reusable PHP components. You need to install PEAR module Net_GameServerQuery on your PHP installation.
First change configuration to match your game server. Set IP address ( $ip ) and game type ( $protocol ) variables.



Note: Click on image for clear view.

Thursday, March 19, 2009

JAVA connection with MySQL using JDBC in Eclipse

We will discuss JAVA-MySQL connection using JDBC (Java Database Connectivity)

JDBC:

JDBC is a standard API that allows JAVA programs to access databases. The JDBC API is a collection of interfaces and classes written in the JAVA Programming Language. We can write applications that connect to databases, send SQL queries, and get the results using these standard interfaces and classes.

A JDBC driver implements these interfaces and classes for a particular Database Management System. A Java Code loads the specified driver for a particular DBMS before it establish a connection with a database.


Download JDBC Driver:


First of all you have to download JDBC driver (usually named mysql-connector-java-version).
For Example: http://ftp.fju.edu.tw/Database/MySQL/Downloads/Connector-J/

Extract downloaded zip file and copy extracted folder to your plugins folder in Eclipse Directory.
For Example: C:\Eclipse\plugins\mysql-connector-java-version


Import JDBC Driver in Eclipse.


1. Right Click on your Project in Eclipse -> Select Properties.
2. Select Java Build Path->selcect Libraries tab->press Add External JARs button.
3. Select mysql-connector-java-version-bin file from downloaded mysql-connector-java-version folder.

Done. Now you are ready to establish JAVA-MySQL connection.


JAVA Code Example:

For Example we have a database named “riksof” with a table employees(id,name,email,designation) where “id” is primary key and auto_increment.



/*Define a connection url*/
String host = "localhost";
String db = "riksof";
String tb = "employees";
String db_user = "dbuser";
String db_pw = "dbpassword";

String db_url = "jdbc:mysql://"+host+":3306/"+db+"?user="+db_user+"&password="+db_pw;

Connection conn = null;
Statement stmt = null;
String query = "";
ResultSet result = null;
PreparedStatement ps = null;



/* Load driver and Establish a connection */
try{
conn = DriverManager.getConnection (db_url , db_user , db_pw);
stmt = conn.createStatement();
}catch(Exception connectionExp){
System.out.println("SQL Connection not Established: " + connectionExp.getMessage());
}

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception driverExp){
System.out.println("Driver not Found : " + driverExp.getMessage());
}



/* Now Display all records */
query = "SELECT * FROM " + tb;
try{
result = stmt.executeQuery(query);

while(result.next()){
System.out.println(result.getString("id"));
System.out.println(result.getString("name"));
System.out.println(result.getString("email"));
System.out.println(result.getString("designation"));
System.out.println("-------------------------------");
}
}
catch(Exception sqlExp){
System.out.println("SQL Exception: " + sqlExp);
}



/*Insert a single record*/
query = "INSERT INTO employees(name,email,designation) VALUES('Name','Email','Designation')";

try{
conn.createStatement().executeUpdate(query);
System.out.println("Record Stored.");
}
catch (SQLException insertExp) {
System.out.println("SQLException Caught: " + insertExp.getMessage());
}

Google Special Syntaxes

In the era when people go online before the morning tea, Google search engine is an essential part in our everyday life.
Being a full-text search engine Google indexed entire website instead of title, links and description. Google search engine support Boolean operators, basic AND, OR, and quoted strings, as well as it offers special syntax for quick and precise query. Here are some of the common syntax which are very useful for professional peoples;

intitle:
intitle: search is restricted to page titles
intitle:"pakistan"
allintitle:"kashmir dispute" pakistan

inurl:
inurl: search is restricted to URLs.
inurl:view.shtml
allinurl:search help

intext:
intext: searches only body text
intext:"riksof.com"

inanchor:
inanchor: searches for text in a page's link anchors.
inanchor:"university"

site:
site: search by either a web site or a top-level domain.
site:gov
site:pk
site:edu.pk

link:
link: gives a list of pages linking to the particular URL.
link:www.riksof.com

cache:
cache: find copy from Goolge indexed cache even it is no longer available at its original location.
cache:www.amazon.com

daterange:
daterange: limits your search to a particular date or range of dates that a page was
indexed.
Note that daterange: works with Julian, not Gregorian dates (the
calendar we use every day).
"Osama Bin Laden" daterange:2452389-2452389
Aerospace daterange:2452389-2452389

filetype:
filetype: searches the suffixes or file extensions.
java filetype:pdf
"marketing" filetype:ppt

related:
related: finds pages that are related to the specified page.
related:www.ebay.com
related:www.cricinfo.com

info:
info: provides a page of links to more information about a specified URL.
info:www.wikipedia.com

phonebook: //not very useful for Pakistan
phonebook: phone number looks up.
phonebook: Karachi

There are still more syntax for easy, precise and rapid search and Google is adding more day by day.