Saturday, 24 September 2011

Making dyanamic variable in CSS


Using PHP to generate a CSS file.
One advantage of CSS is that your styles can be kept in a separate file from the pages that use them. This is simply a text file that ends with the .css suffix such as style.css. The suffix tells the server to treat the contents of the file as CSS. Unfortunately, the CSS standard doesn’t allow for variables within a .css file. The solution is to use a PHP file, such as style.php and include code to let the server know that the contents of the file should be treated as CSS. This can be accomplished by including the following line of PHP at the top of the style sheet;
<?php header(“Content-type: text/css”); ?>
Your style definitions can then follow so that the top of your style.php document looks something like this:
<?php header(“Content-type: text/css”); ?>
a:link { text-decoration : none; color : #FF0000; }
a:active { text-decoration : underline; color : #FF0000; }
a:visited { text-decoration : none; color : #FF0000; }
a:hover { text-decoration : underline; color : #660000; }
Your .php style sheet will now function exactly like a .css one. We’ll get to the variables in a minute but first…
Linking to your .php style sheet
You can link to your .php style sheet exactly as you would a regular style sheet. Here’s the code I placed in the <head> section of my webpage:
<link href=”style.php” rel=”stylesheet” type=”text/css” />
Using variables in your style.php file
The advantage of a PHP style sheet is that it allows you to define variables and then use those variables throughout your style sheet. So, for the example I gave above where the a:link, a:active and a:visited styles all use the same color (#3366cc), we could define a “$linkColor” variable (The “$” before linkColor simply indicates it is a variable.) and use it in all three styles. The variable definition would look like this:
<?php header(“Content-type: text/css”);
$linkColor = ‘# #FF0000;
?>
Remember all PHP code must be contained within a php tag which opens with “<?php” or simply “<?” and closes with “?>”
We can now insert the variable into our css code like this:
<?=$linkColor?>
Notice the opening and closing php tags.
So the beginning of our CSS file now looks like this:
<?php header(“Content-type: text/css”);
$ linkColor = ‘#FF0000;
?>
a:link { text-decoration : none; color : <?=$ linkColor?>; }
a:active { text-decoration : underline; color : <?=$ linkColor?>; }
a:visited { text-decoration : none; color : <?=$ linkColor?>; }
a:hover { text-decoration : underline; color : #000021; }
Now to change the color of the a:link, a:active and a:visited links from red (#FF0000) to blue (#0000FF) we need only change the definition of $linkColor:
<?php header(“Content-type: text/css”);
$ linkColor = ‘#0000FF;
?>
A word about variables and variable names
It should be easy to see how this trick can make editing your style sheets a lot easier. What’s less obvious (at least initially) is that, if you create a variable for every possible style, your variable list will soon become nearly as complicated and confusing as your original style sheet. Here are a couple tips to keep things simple:
  1. Build your color palette carefully so that you can color as many pieces of the design as possible using the same variable. For instance, if your header, footer and sidebar will be the same color, you can use the same variable (perhaps $secondColor) to define them.
  2. Don’t be too specific with your variable names. You don’t want to use the variable name $red, if you might be changing it to blue later on. Similarly, you probably don’t want to use the variable name $headerColor if the variable is also used for the footer and sidebar. I prefer names such as $themeColor, $hiliteColor, etc.
  3. Plan carefully so that you don’t force yourself into a situation where your type and background have insufficient contrast. In spite of #1 above, you might sometimes need to define more than one variable with the same color to provide future flexibility.
  4. Use PNG graphics for images that are partially transparent. Since PNGs offer true transparency, you can create a drop shadow that will work over any background color. That’s important if you’ll be changing those background colors. Just remember, you need to use a png javascript fix for PNGs to display properly in Explorer 6.


NOTE:
header(’Content-type: text/css’); seems to cause problems for Safari. Try this instead:
header(”Content-Type: text/css; charset: utf-8?);


Thursday, 22 September 2011

PHP Mailer Tips

Adding CC in phpmailer

 $mail->AddCC('test@test.com, 'Admin');


Friday, 16 September 2011

disabling auto logoff on phpmyadmin

http://www.phpmyadmin.net/documentation/

 In the file libraries/config.default.php

change the value for LoginCookieValidity
/*
* Prevent timeout for a week at a time.
* (seconds * minutes * hours * days)
*/
$cfg['LoginCookieValidity'] = 60*60*24*7;




Thursday, 15 September 2011

Displaying a Multidimensional Array using For each

<?php
  $emp_det = array (array (Name=>"a", Code=> "8", Hobby=> "A"),
                    array (Name=>"b", Code=> "8", Hobby=> "B"),
                    array (Name=>"c", Code=> "1", Hobby=> "C"),
                    array (Name=>"d", Code=> "3", Hobby=> "D"),
  );
  foreach ($emp_det as $tempone) {
    foreach ($tempone as $key=>$temptwo) {
      echo "$key: $temptwo""\n";
    }
    echo "\n";
  }
?>

Tuesday, 6 September 2011

Colorbox Tips

Opening colorbox popup using function:

<script>
function lightbox(){    
  $.fn.colorbox({width:"80%", height:"80%", iframe:true, href:"/pagetoopen.html"});
}
</script>
<input type="button" value="open the box" onClick="lightbox()"/>

Modify above function to refresh the parent page when popup closed:

$.fn.colorbox({width:"95%", height:"95%", iframe:true, href:"allocate-contact-number.php?id="+checked_num, onClosed:function(){window.location.reload();}});


Close popup 


<script type="text/javascript" >

self.parent.$.fn.colorbox.close();
</script>





Thursday, 1 September 2011

Displaying javascript running clock time for selected timezone


<?php
$timeFormat = $_SESSION["display"]["php_time_format"];

$currentTime_client = parse_date_for_clock($_SESSION["timezone"], $connA);
$currentTime_system = date("F d, Y ".$timeFormat);

if($timeFormat=="h:i:s A"){
    $clockFormat = "12";
}else{
    $clockFormat = "24";
}
?>
<script type="text/javascript">

var serverDate_client;
var serverDate_system;

function getServerDate_client(){
serverDate_client=new Date("<?php echo $currentTime_client;?>");
}
function getServerDate_system(){
serverDate_system=new Date("<?php echo $currentTime_system;?>");
}

function tick(timeFormat,showTimeFor,showTimeDiv){
if(showTimeFor=="client"){
    serverDate=serverDate_client;
}else{
    serverDate=serverDate_system;
}


serverDate.setSeconds(serverDate.getSeconds()+1);
var currentHours = serverDate.getHours();
var ampm="";
if(timeFormat=='12'){
if(currentHours >=12){
    ampm="PM";
}else{
    ampm="AM";
}
currentHours = (currentHours > 12 ) ? currentHours - 12 : currentHours;
currentHours = (currentHours == 0 ) ? 12 : currentHours;

}
if (currentHours<10) currentHours="0"+currentHours;

var min = serverDate.getMinutes();
if (min<10) min="0"+min;
var sec = serverDate.getSeconds();
if (sec<10) sec="0"+sec;
document.getElementById(showTimeDiv).innerHTML = currentHours + ":" + min + ":" + sec+" "+ampm;
}

window.onload=function(){
getServerDate_client();
setInterval("tick('<?php echo $clockFormat;?>','client','client_clock')", 1000);
getServerDate_system();
setInterval("tick('<?php echo $clockFormat;?>','system','system_clock')", 1000);
}
</script>



======== php time function definition used in above script==========
function parse_date($gmtoffset, $connA) {// parse date and time both

        $currdate = date("Y-m-d H:i:s");
        return mysql_fetch_object(mysql_query("SELECT DATE_FORMAT(CONVERT_TZ('$currdate', '".date("P")."', '$gmtoffset'), '".$_SESSION["display"]["mysql_date_format"]." ".$_SESSION["display"]["mysql_time_format"]."') AS display_date", $connA))->display_date;
    }

    function parse_date_only($gmtoffset, $connA) {

        $currdate = date("Y-m-d H:i:s");
        return mysql_fetch_object(mysql_query("SELECT DATE_FORMAT(CONVERT_TZ('$currdate', '".date("P")."', '$gmtoffset'), '".$_SESSION["display"]["mysql_date_format"]."') AS display_date", $connA))->display_date;
    }
    function parse_time_only($gmtoffset, $connA) {

        $currdate = date("Y-m-d H:i:s");
        return mysql_fetch_object(mysql_query("SELECT DATE_FORMAT(CONVERT_TZ('$currdate', '".date("P")."', '$gmtoffset'), '".$_SESSION["display"]["mysql_time_format"]."') AS display_date", $connA))->display_date;
    }

    function parse_date_for_clock($gmtoffset, $connA) {

        $currdate = date("Y-m-d H:i:s");
        $format = "%M %d %Y ".$_SESSION[display][mysql_time_format];
        return mysql_fetch_object(mysql_query("SELECT DATE_FORMAT(CONVERT_TZ('$currdate', '".date("P")."', '$gmtoffset'), '$format') AS display_date", $connA))->display_date;
    }

Bulk data insertion in mysql and PHP


$conn = mysql_connect("localhost", "root", '111111', false, 65536)or die(mysql_error());

$studentName = array("Rahul", "Ravi", "Sachin", "Vikas","Sunidhi");

foreach($studentName as $value){

$query_arr[] = "(NOW(),NOW(),$value)";
}


$bulk_ins_qry = "INSERT INTO tbl_student (creationdate,lastupdate,name)
                    VALUES";

$bulk_ins_qry .= implode(",",$query_arr);
mysql_query($bulk_ins_qry, $conn);