Friday 18 January 2013

CheckBox Check/Uncheck All Javascript

Hi Every One,
I struggled a little to check and uncheck all on checkbox click today. At last i found a bit of code to do it by my friend.

Here is the script i used,

<script type="text/javascript">
function checkedAll (obj)
     {
   
            var aa= document.getElementsByName(obj.id);
          if (obj.checked){
                               var i =0;
                               while(i<aa.length)
                                   {
                                       aa[i].checked=true;
                                    i++;
                                   }
                          }
                     else {
                               var i =0;
                                   while(i<aa.length)
                                   {
                                       aa[i].checked=false;
                                    i++;
                                   }
                          }
       
    }

</script>





Form Coding:

<form method="post">
<div id="c">
<div id="a" >
<input type="checkbox" name="a" id="agroup" onclick="checkedAll(this)"/>Header1<br />
<input type="checkbox" name="agroup" />List A1
<input type="checkbox" name="agroup" />List A2
</div>
<br />
<br />

<div id="b">
<input type="checkbox" name="b" id="bgroup" onclick="checkedAll(this)"/>Header2<br />
<input type="checkbox" name="bgroup" />List A1
<input type="checkbox" name="bgroup" />List A2
</div>
</div>

</form>


But i am trying to make it by Id. Let me think of it.
~umar

Thursday 17 January 2013

JueryUI Tabs Cookies




Hi,
I am glad that I suceeded in Tabs cookies today after a long study. here is the default code.
Kindly add library file called "jquery.cookie.js" from Development Bundle -> External  folder of jqueryUI.

Note: Dont forget to delete

                               $( "#tabs" ).tabs();


Then add the following in that place and play.

$( "#tabs" ).tabs({
cookie: {
// store cookie for a day, without, it would be a session cookie
expires: 1
}
});





It wont expire till 24 hours so u can come and check for the next day too. Hope it helps. 

~Umar

Saturday 12 January 2013

Simple Web2.0

Small Note about Web2.0:
"Web 1.0":
- Posting articles.
- Tagging content.
- Put products online for sale.

Web 2.0:
- Allow Users to post comments and rate articles.
- Allow Users to tag the content themselves and create a tag cloud to let Users know which tags are more popular.
- Allow Users to post reviews and submit additional photos/videos of them using your company's products.

~Let me go in Deeper soon.
Umar

Wednesday 9 January 2013

Ten PHP Best Practices Tips that will get you a job


The last couple of weeks have been quite the experience for me. I was part of a big layoff at my former company, which was interesting. I've never been in that position before, and it's hard not to take it personally. I started watching the job boards, and a nice-looking full-time PHP position caught my eye, so I sent out a resume and landed an interview. Before the face-to-face portion, I chatted with the owner and head programmer on a conference call, and they ended up sending me a technical assessment quiz. One particular question caught my eye on this quiz… it looked something like this:

Find the errors in the following code:

<?
function baz($y $z) {
$x = new Array();
$x[sales]  = 60;
$x[profit] = 20:

foreach($x as $key = $value) {
echo $key+" "+$value+"<BR>";
}
}

?>

So, give it a shot. How many can you find?

If you got the missing comma in the parameter list, the "new Array()" error, the colon instead of a semi-colon, the '=' instead of '=>' in the foreach statement, and the erroneous use of '+' on the echo line, then congratulations, you found all the errors! You have the basic PHP technical skills to pay the bills.

That's not how I answered the question though. I noted the errors, obviously, but I went further than that. For instance, did you notice that there were no single quotes around the array indexes ($x[sales] and $x[profit])? That won't cause a fatal PHP error, but it is a coding error! Did you also notice the use of double-quoted strings instead of single-quoted strings on the echo line? Or the usage of the opening PHP short tag? Or the usage of "<BR>" instead of "<br/>"?

After pointing out the actual errors, I made a point of adding comments about those things I just mentioned. It was enough to push the answer from "correct" to "impressive", and it scored me a lot of points with the programmers who were reviewing my application. Enough so that they offered me the job! (I eventually turned it down, as I have been seduced by the siren call of the contracting life, and I intend to flex my PHP skills to the benefit of my clients, and not a faceless corporate overlord who dabbles in telemarketing. I need a shower).

So, read on for my Ten PHP Best Practices Tips that will get you a job:

1. Single-quoted strings are your friend. When you surround a PHP string in double quotes, it is subsequently parsed by the PHP interpreter for variables and special characters, such as "\n". If you just want to output a basic string, use single quotes! There is a marginal performance benefit, since the string does not get parsed. If you have variables or special characters, then by all means use double-quotes, but pick single quotes when possible.

2. String output. Which line of code do you think runs faster?

print "Hi my name is $a. I am $b";
echo "Hi my name is $a. I am $b";
echo "Hi my name is ".$a.". I am ".$b;
echo "Hi my name is ",$a,". I am ",$b;

This might seem weird to you, but the last one is actually the fastest operation. print is slower than echo, putting variables inline in a string is slower than concatenating them, and concatenating strings is slower than using comma-separated echo values! Not only does not-inlining your variables give you a performance boost, but it also makes your code easier to read in any editor that has syntax highlighting (your variables will show up in nice colors). The little-known use of echo as a function that takes a comma-separated list of values is the fastest of them all, since no string operations are performed, it just outputs each parameter. If you combine all this with Tip #1 and use single quotes, you're on your way to some finely-tuned strings.

3. Use single-quotes around array indexes. As you saw in the quiz question above, I pointed out that $x[sales] is technically incorrect! You should quote associative array indexes, like so: $x['sales']. This is because PHP considers the unquoted index as a "bare" string, and considers it a defined constant. When it can't find a matching symbol for this constant in the symbol table however, it converts it to a real string, which is why your code will work. Quoting the index prevents this constant-checking stuff, and makes it safer in case someone defines a future constant with the same name. I've also heard that it is up to seven times faster than referencing an unquoted index, although I haven't tested this. For more on this, see the section called "Array do's and don'ts" in the Array section of the PHP manual.

4. Don't use short open tags. Eww… are you really using these? <? is just bad form. It can cause conflicts with XML parsers, and if you ever distribute code, it's going to annoy the heck out of people who have to start modifying their PHP ini directives to get it to work. There's just no good reasons to use short open tags. Use the full <?php.

5. Don't use regular expressions if you don't need to. If you're doing basic string operations, stay away from the preg and ereg function groups whenever possible. str_replace is much faster than preg_replace, and strtr is even faster than str_replace! Save those crunch cycles… your enterprise applications will thank you.

6. Don't use functions inside a loop declaration. This isn't a PHP-specific tip, but you'll see it in a lot of code.

Bad:

for ($i = 0; $i < count($array); $i++) {
//stuff
}

Good:

$count = count($array);
for($i = 0; $i < $count; $i++) {
//stuff
}

That should be pretty self-explanatory, but it's amazing how many people would rather save a line of code at the expense of performance. If you use a function like count() inside a loop declaration, it's going to get executed at every iteration! If your loop is large, you're using a lot of extra execution time.

7. Never rely on register_globals or magic quotes. register_globals and magic quotes are both old features of PHP that seemed like a good idea at the time (ten years ago), but in reality turned out to be not that great. Older installations of PHP would have these features on by default, and they cause security holes, programming errors, and all sorts of bad practices, such as relying on user input to create variables. Both these features are now deprecated, and everyone needs to stop using them. If you are ever working on code that relies on these features, get it out of there as soon as you can!

8. Always initialize your variables. PHP will automatically create a variable if it hasn't been initialized, but it's not good practice to rely on this feature. It makes for sloppy code, and in large functions or projects can become quite confusing if you have to track down where it's being created. In addition, incrementing an uninitialized variable is much slower than if it was initialized. It's just a good idea.

9. Document your code. You've heard it many times, but this can't be said enough. I know places that won't hire people who don't document code. I even got my previous job after an interview where the VP sat-in with the interviewer and I, where I had brought my laptop in and I was just scrolling through some code I had written for one of my sites. He saw my documented functions and was impressed enough to ask me about my documenting habits. A day later I had the job.

I know a lot of self-declared PHP gurus out there like to pretend that their code is so good that they don't have to spend time documenting it, and these people are full of $largeAnimal poop. Learn docblock syntax, familiarize yourself with some PHP Documentation packages like phpDocumentor or Doxygen, and take the extra time to do it. It's worth it.

10. Code to a standard. This is something that you should ask potential employers about during interviews. Ask them what kind of coding standards they use… PEAR? Zend? In-house? Mention that you code to a specific standard, whether it be your own, or one of the more prevalent ones out there. The problem with loosely-typed languages like PHP is that without a proper coding standard, code tends to start looking like huge piles of garbage. Stinky, disgusting garbage. A basic set of rules that includes whitespace standards, brace matching, naming conventions, etc. is a must-have, must-follow for anyone who prides themselves on their code quality.

That being said, I hate all you space-indenters. I mean, what the hell? 4 space characters as an indent? That's exactly four times as much whitespace to parse as a tab. More importantly, you can set your tab-stops to any value you want if you're using any text-editor more advanced than Notepad, so every developer can having something that they like the looks of. Set it to 4 if you want, or 0 if you're a masochist. I don't care, but you can't do that with spaces! You're stuck with exactly the amount that Mr. Monkey Pants in cubicle 17 decided to put in. So why are spaces so popular? This "4-space indent" standard everyone uses is stupid! Stop it! Stop doing it!

Monday 7 January 2013

Add month or year for a date using PHP


Hi,
Below is the code to add month or year for a date using PHP.
<?php
$date = "2012-01-30";

list($year,$month,$day) = explode("-",$date);

// add month here
$month=$month+1;
// to avoid a month-wrap, set the day to the number of days of the new month if it's too high
$day = min($day,date("t",strtotime($year."-".$month."-01")));
$date = $year."-".$month."-".$day;

$date1_d=date("d",strtotime("$date"));
$date1_D=date("D",strtotime("$date"));
$date1_Y=date("Y",strtotime("$date"));
$date1_F=date("F",strtotime("$date"));

// 2012-02-29
echo $date;
echo "<br>".$date1_d;//29
echo "<br>".$date1_D;//Wed
echo "<br>".$date1_Y;//2012
echo "<br>".$date1_F;//February
?>

Hope it helps.
~ Enjoy
Umar

Saturday 5 January 2013

Retaining Form Values On PHP Postback

Here is one text box inside a PHP code area. We will learn here how to retain the text box value after the form is submitted. In this text box we will keep one value option which will store the value entered by the user. This form is submitting to another page where the PHP code will check the value and if it is correct then it will redirect ( or back ) to main page with the value of data in query string. We will use PHP header redirecting the second page ( page to which form data will be submitted ) to post back the data to the main page having the form.

Assigning php variable to at text box

Before this we will learn how to assign data stored in a php variable to value attribute of a text box. When the page is loaded the data stored in PHP variable will be stored and displayed inside text box as default value. Here is one example

<?Php
$t1="My Data here "
echo "<input type=text name='t1' value='$t1'>";
?>

Here to take care of some characters like & we have used urlencode and urldecode to first encode the date before sending through the address bar and then after receiving at main page again we will decode it to get the original data entered by the visitor. 

Here is the code of the form with decode script.

<?Php
$t1v=$_GET['t1v'];
$t1v=urldecode($t1v);

echo "
<form method=post action=pb-t.php>
<input type=text name=t1 value='$t1v'>
<input type=submit value=Submit>
"; ?>

Now we will go to the second page were the form date is collected and posted back to main page. Here is the code of pb-text.php

<?Php
$t1=$_POST['t1'];

$t1=urlencode($t1);
header ("Location: pb-text.php?t1v=$t1");
?>

Date will be posted back and you will see your name again here after redirection..

Hope it helps.
Thanks.

Friday 7 December 2012

Auto complete text box with PHP, jQuery and MySql

In this tutorial I want to show you how easy to do a auto complete text box with PHP, jQuery and MySql.

Firstly download the autocomplete jQuery plugin and extract it to your hard drive.
And then create a folder in your localhost. Copy jquery.autocomplete.css and jquery.autocomplete.js file to your project folder. And you will also need jquery.js. Now we can start our project. 

Creating database table


We need to create a table in our database before writing our script. I also add some data for this table. Import following SQL statement via phpMyAdmin or any other MySQL tool.

CREATE TABLE `tag` (
  `id` int(20) NOT NULL auto_increment,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

INSERT INTO `tag` (`id`, `name`) VALUES
(1, 'php'),
(2, 'php frameword'),
(3, 'php tutorial'),
(4, 'jquery'),
(5, 'ajax'),
(6, 'mysql'),
(7, 'wordpress'),
(8, 'wordpress theme'),
(9, 'xml');

index.php

<!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>Auto Complete Input box</title>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<script>
 $(document).ready(function(){
  $("#tag").autocomplete("autocomplete.php", {
        selectFirst: true
  });
 });
</script>
</head>
<body>
<label>Tag:</label>
<input name="tag" type="text" id="tag" size="20"/>
</body>
</html>

autocomplete.php

This file is called from our jquery script. This php script is easy to follow.
<?php
 $q=$_GET['q'];
 $my_data=mysql_real_escape_string($q);
 $mysqli=mysqli_connect('localhost','username','password','databasename') or die("Database Error");
 $sql="SELECT name FROM tag WHERE name LIKE '%$my_data%' ORDER BY name";
 $result = mysqli_query($mysqli,$sql) or die(mysqli_error());

 if($result)
 {
  while($row=mysqli_fetch_array($result))
  {
   echo $row['name']."\n";
  }
 }
?>
We have done. Run your project and type in your text box you will see following screenshot. 

By the way, I use the old jquery plug in this project. If you want to learn new thing, you can learn hereand here. I create this tutorial because it is still useful and easy, although it is too old. I will also write about new jquery ui later.
Download Source Code

Thanks To :  Wai Lynn Oo