Monday, November 10, 2014

PHP 1st, 2nd, 3rd, 4th, 5th, 6th and so on PHP Add Ordinal Number Suffix

<?php

function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1:  return $num.'st';
case 2:  return $num.'nd';
case 3:  return $num.'rd';
}
}
return $num.'th';
}

for ($i = 1; $i <= 100; $i++){
echo addOrdinalNumberSuffix($i) . "\t";
if ($i % 10 == 0) {
echo "<br/>";
}
}

?>


Output :)


Friday, October 10, 2014

MySql query to select count of only max value from table in PHP

CREATE TABLE tableName  // Give any name as u like
(
     id int auto_increment primary key,
     party varchar(20),
     constituency varchar(30),
     votes int(11)
);

Then insert some details.
I inserted these details :)

INSERT INTO tableName
          (party, constituency,votes)
VALUES
          ('BJP', 'Surat',1200),
          ('Congress', 'Surat',1100),
          ('BJP', 'Baroda',800),
          ('Congress', 'Baroda',900);

Query :)

mysql_query("select max(votes) from tableName group by constituency");

Output :)


Wednesday, October 8, 2014

PDO search database using LIKE.

First create database and name it as u like. I create database named with `fdb`. And then create table with 4 columns.

DEMO :)

Then insert some details


Example :)

/* here project is the database table name */

<?php
try{
$bdd=new PDO("mysql:host=localhost;dbname=fdb","root","");
}
catch(exception $e){
die("ERROR : ".$e->getMessage());
}

$keyword='ma';  /* You can use any characters as keywords */

$sql="SELECT * FROM project WHERE fullname LIKE :keyword;";
$q=$bdd->prepare($sql);
$q->bindValue(':keyword','%'.$keyword.'%');
$q->execute();

while ($r=$q->fetch(PDO::FETCH_ASSOC)) {
   echo"<pre>".print_r($r,true)."</pre>";
}
?>

Output :)


Friday, September 19, 2014

Display data in the table from database.

First create the database and name it fdb.
Then create table with 2 columns in the database and name it simp.
Insert data in the table.

Demo :)

Now php file :)

<?php
$dbserver="localhost";
$dbusername="root";
$dbpassword="";
$dbname="fdb";

$link=mysql_connect($dbserver,$dbusername,$dbpassword);
mysql_select_db($dbname,$link);

function displaytable($tablename,$link)
{
$query = "SELECT city FROM $tablename";
$res = mysql_query($query,$link);
$ccount = mysql_num_fields($res);

echo "<table border=1>";
echo ("<tr align=center>");
echo "<td><b>City List</b></td>";
echo "</tr>";

while($row=mysql_fetch_array($res))
{
echo ("<tr>");
for($col=0;$col<$ccount;$col++)
{
echo"<td> $row[$col]</td>\n";
echo"</tr>\n";
}
}
echo "</table>\n";
}
?>
<html>
<head>
<title>Display Table Data</title>
</head>
<body>
<h4>Table Data</h4>
<hr/>
<table>
<tr>
<td>
<?php
displaytable("simp",$link);
?>
</td>
</tr>
</table>
</body>
</html>

Output :)

Wednesday, September 17, 2014

How to know total no of rows in Database table in PHP?

First create a database and a table in it.
Here database name = first_db and table name = students
Add some data in the table

<html>
<head>
<title>No of Rows</title>
</head>
<body>

<?php
$dbserver="localhost";

$dbusername="root";

$dbpassword="";

$dbname="first_db";

$link=mysql_connect($dbserver,$dbusername,$dbpassword);
mysql_select_db($dbname,$link);

if($_REQUEST['btn'])
{
$sql="select * from students"; // Here students is a database table name that we created in the database
$result=mysql_query($sql,$link);
$row=mysql_num_rows($result);

echo "Total no of rows : " . $row ;

mysql_close($link);
}
?>

<form action="totalnorows.php" method="POST">
<table>
<tr>
<td>
<input type="submit" name="btn" value="Get No" />
</td>
</tr>
</table>
</form>

</body>
</html>

Output :)

Screen 1 :)



Screen 2 :)

Screen 3 :)

Table of 25

<?php
$n=1;
factorial($n);
function factorial($n){
if($n<=10)
{
$ans= $n * 25;
echo "25 * $n"." = "."$ans"."<br/>";
factorial($n+1);
}
}
?>


Output :)

Sunday, April 20, 2014

How to create Registration form in PHP.

First create a database table in MySql :-



Than create a Sign Up form using html.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>

<form method="post" action="database.php">
<fieldset style="width:30%">
<legend>Registration Form</legend>
<table align="center">
<tr>
<td>First Name :</td>
<td><input type="text" name="fname"/></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input type="text" name="lname"/></td>
</tr>
<tr>
<td>Select UserName :</td>
<td><input type="text" name="uname"/></td>
</tr>
<tr>
<td>Email ID :</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="pw"/></td>
</tr>
<tr>
<td></td>
<td><input id="button" name="submit" type="submit" value="Sign Up"/></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>

write the following code in the Database.php :-


<?php
$cn=mysql_connect('localhost','root','');
$cd=mysql_select_db('Mahi',$cn); //Mahi is a Database Name

if(isset($_POST['submit']))
{
SignUp();
}

function SignUp()
{
if(!empty($_POST['uname'])) //checking the 'uname' is empty or have some text
{
$query = mysql_query("SELECT * FROM cf1 WHERE UserName = '$_POST[uname]'
OR UserName='$_POST[uname]' and Email='$_POST[email]' OR
Email='$_POST[email]'"); //cf1 is a Table name of a Database

if(!$row = mysql_fetch_array($query))
{
newuser();
}

else
{
echo "SORRY...This is Aready Registered User.";
}
}
}

function NewUser()
{
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$uname = $_POST['uname'];
$email = $_POST['email'];
$pw = $_POST['pw'];

$query = "INSERT INTO CF1(First_Name,Last_Name,UserName,Email,Password) VALUES ('$fname','$lname','$uname','$email','$pw')";

$data = mysql_query ($query);

if($data)
{
echo "YOUR REGISTRATION IS COMPLETED...";
}
}
?>

Output :-