Wednesday, September 22, 2010

Css Code to be rendered by IE6 only

If we want our CSS code to be read by IE6 only(in most cases) we use a common IE6 hack

* html #propertyName
{
/* CSS Code Here */
}

using *html in css will allow us to over come some CSS pseudo-classes problems in IE6.

How to render html codes in blogspot

We need to replace these appearances(characters with notations) in our post. we can do this with help of notepad or any other editor of choice.

Characters Notation
(Name Code)
Notation
(Number Code)
Remarks
< &lt; &#60;    less-than sign
> &gt; &#62; greater-than sign
& &amp; &#38; and
" &quot; &#34; Double quote
± &plusmn; &#177; Plus minus sign
Space &nbsp; &#160; nonbreaking space
© &copy; &#169; copyright
® &reg; &#174;     Registered

Update multiple rows in mysql using checkbox

--
-- Table structure for table `test_mysql`
--

CREATE TABLE IF NOT EXISTS `tbl_sql` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(65) NOT NULL DEFAULT '',
`lastname` varchar(65) NOT NULL DEFAULT '',
`email` varchar(65) NOT NULL DEFAULT '',
`status` varchar(1) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

--
-- Dumping data for table `test_mysql`
--

INSERT INTO `tbl_sql` (`id`, `name`, `lastname`, `email`, `status`) VALUES
(1, 'Tarik', 'Hussain', 'th@example.com', 'Y'),
(2, 'Shariq', 'Hussain', 'sh@example.com', 'Y'),
(3, 'Umair', 'Khan', 'uk@example.com', 'Y'),
(4, 'Majid', 'Hussain', 'mh@example.com', 'N'),
(5, 'Riaz', 'Shah', 'rs@example.com', 'N'),
(6, 'Ahsan', 'Siddiqui', 'as@example.com', 'N');

<?php
// Connection to the database
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="database"; // Database name
$tbl_name="tbl_sql"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"])
$id = "('" . implode( "','", $checkbox ) . "');" ;
$sql="UPDATE test_mysql SET status = '".(isset($activate)?'Y':'N')."' WHERE id IN $id" ;
$result = mysql_query($sql) or die(mysql_error());
}

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>

<!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="utf-8"" />
<title>Update multiple rows in mysql with checkbox</title>

<script type="text/javascript">
<!--
function un_check(){
for (var i = 0; i < document.frmactive.elements.length; i++) {
var e = document.frmactive.elements[i];
if ((e.name != 'allbox') && (e.type == 'checkbox')) {
e.checked = document.frmactive.allbox.checked;
}}}
//-->
</script>

</head>
<body>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="frmactive" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1">
<tr>
<td colspan="5"><input name="activate" type="submit" id="activate" value="Activate" />
<input name="deactivate" type="submit" id="deactivate" value="Deactivate" /></td>
</tr>
<tr>
<td> </td>
<td colspan="4"><strong>Update multiple rows in mysql with checkbox</strong> </td>
</tr><tr>
<td align="center"><input type="checkbox" name="allbox" title="Select or Deselct ALL" style="background-color:#ccc;"/></td>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Firstname</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Status</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['status']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center"> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

Saturday, September 18, 2010

Send Email using PHP


Its very easy to send an email using php we just need to use integrated function of php 'mail'


mail($to, $subject, $body, $headers);


$to = variable that holds the address where emails has to be sent.

$subject = variable to hold the subject of email

$body = this is the body of the email, we can send html email too by defining headers in our email function.

$headers = this variable is responsible to define metadata of our email. i.e(from, reply-to, auto-responder)

-> example of simple email:


<?php
mail("receiver@emailadd.com", "this is the body of Subject", "this is body of email", "From: sender@emailadd.com);
?>

->here is another example to send HTML Email by dfining headers and using variables



<?php
$to = "receiver@emailadd.com"

$sub = "This is the Subject of Body";

$body = This is First Line<br/>This is Second Line <b>some thing bold</b>";

$from = "sender@emailadd.com";

$headers = "From: $from" . "\r\n";

$headers .= 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to, $sub, $body, $headers);
?>

in above functions we define variables to be used in mail function and also define headers that tells the mail function about the property of email body and some other meta information.

Using explode in php for beginners

how to explode (split, break apart) string in php using explode function 

<?php

function split($stringtosplit)
    {
    $abc = explode(' ', $stringtosplit);
    foreach($abc as $xyz){
    echo  $stringtosplit;
    }
    return $abc;
    }
   
?>

for example

<?php

    split ('the quick brown fox jumps over the lazy dogs');
    ?>

output : thequickbrownfoxjumpsoverthelazydogs
in this output all the space were removed via explode function, to format this we need to style our string

i.e. echo  $stringtosplit .'<br />';

now output will be

the
quick
brown
fox
jumps
over
the
lazy
dogs
 

Friday, September 17, 2010

Create a Simple Image in PHP

Below is the function to manipulate a simple image in true color

this function will create a simple image set its header return image and free the memory by destroying it.

function createImage($imgw, $imgh)
{
// create a image
$image = imagecreatetruecolor($imgw, $imgh);
# Prints out all the figures and picture and frees memory
header('Content-type: image/png');
ImagePNG($image);
imagedestroy($image);
}

Executing scripts while posting Form to different URL

Technique to execute certain function or a file on a server
(i.e, posting form to some URL and want to track the hits on the search result)

function createlog(param1, param2) {
var u = "createlog.php?param1=" + param1 + "&param2=" + param2;
(new Image).src = u;
return true;
}

we can also use this approach to execute some functions i.e, to email a paypal form custom data while using paypal buttons.