Monday, July 28, 2008

How to get a random alpha numeric value php - uniqid()

If you want to get a random alpha numeric value (Numbers and letters) in php you can use uniqid() (Only works in php 5 or later)


<?php

$random = md5(uniqid());

echo $random;

?>





Get custom programming done at GetAFreelancer.com!

Wednesday, July 23, 2008

How to find duplicate rows in mysql using php

There are times when you need to find duplicate rows or records in a mysql table.
This mostly happens when you import data sources from out side(CSV,EXCEL,SQL)
Following code will show you how to find duplicates,



select count(*) as num, ID, Name from table
group by ID,Name
having count(*) > 1



This code will group the given column and see if the grouped columns have more than one record

+-----+------+------------+
| num |ID | Name |
+-----+------+------------+
| 3 | 1 | Kuppiya |
+-----+------+------------+
| 2 | 3 | Apple |
+-----+------+------------+



Get custom programming done at GetAFreelancer.com!

Friday, July 18, 2008

How to get distinct(unique) rows from mysql table using php

The simple way to get the unique values from a mysql table is to use DISTINCT keyword in your mysql query.


SELECT DISTINCT col_name FROM tbl_name

OR

SELECT DISTINCT col_name, col_name2 FROM tbl_name




Get custom programming done at GetAFreelancer.com!

Friday, July 11, 2008

How to find mysql query returned row count using php

You can find affected or returned row(tuple) count of a mysql query using php built in function mysql_num_rows()


$result = mysql_query("SELECT * FROM MyTable");
$rows = mysql_num_rows($result);

echo $rows;





Get custom programming done at GetAFreelancer.com!

Thursday, July 10, 2008

How to get the size of an array in php - Find Array Size

Following code will show you how to get the size(Element count) of a array in php using count() and sizeof() (sizeof is same as count() )


<?php

$myA[0] = 12;
$myA[1] = 8;
$myA[2] = 20;

$arr_size = count($myA);
echo $arr_size; // 3

//you can also use sizeof()
//sizeof() is a alias for count()

$arr_size2 = sizeof($myA);
echo $arr_size2; // 3

?>




Get custom programming done at GetAFreelancer.com!

Tuesday, July 8, 2008

How to turn off error reporting in php Using php code - error_reporting()

If you want to turn off any errors and warning from displaying in the web page, The easiest way is to modify the php.ini file.
But what if you want to turn off error reporting in php using your php code? you can accomplish this easily using php built in function error_reporting()

<?php

error_reporting(0); // Turn off all error reporting

?>



More info : http://www.php.net/error_reporting

Freelance Jobs

Sunday, July 6, 2008

Redirect a page OnSubmit using radio buttons and php

Do you need a simple form which redirect to a new page when you select a relevant Radio button and submit? Take a look at the following code, pretty simple :)


<?php

//check if submit button is pressed
if(isset($_POST['sub']))
{
$path_id = $_POST['test'];//get selected radio button value

if($path_id == 1)
{
header("Location: page1.php");//redirect to the new page
}
else if($path_id == 2)
{
header("Location: page2.php");
}
else if($path_id == 3)
{
header("Location: page3.php");
}

}

?>

<html>
<head>
<title>test</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="test.php">
Form1:
<input type="radio" name="test" id="test" value="1" />

Form2:
<input type="radio" name="test" id="test" value="2" />

Form3:
<input type="radio" name="test" id="test" value="3" />
<br />
<br />

<input type="submit" name="sub" value="submit" />
</form>
</body>
</html>




Get custom programming done at GetAFreelancer.com!

Thursday, July 3, 2008

How to check if a php variable is empty or not using PHP

You wanna check if a given string or php variable is empty?
You can easily do it using the php empty() function


<?php

$my_var = ''; //empty() also return true for 0

if (empty($my_var))
{
echo 'Its empty';
}
else
{
echo 'not empty';
}
?>




Get custom programming done at GetAFreelancer.com!

Tuesday, July 1, 2008

HTML MARQUEE UP - Simple News Bar Using Marquee

If you want to add a simple News bar (News reel) to your website, you can do it easily using a HTML MARQUEE tag, you only need to change the direction property to UP
Try this code out


<div width="35">
<marquee bgcolor="skyblue" scrollamount="2"
direction="up" width="35%">
<strong>
THIS IS A SIMPLE NEWS REEL<br>
You can add many lines<br>
THIS IS ANOTHER LINE<br>
<strong>
</marquee>
</div>




Get custom programming done at GetAFreelancer.com!