You are currently browsing the archives for the Programming category.
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Sep | ||||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |
08-14-2008 by Tons.
Day 1
Today is the first day to study java. I started on the basic steps like know how to compile, where to get the compiler and how to run it via ms-dos prompt and via web.
So far i was able to get a compiler from java.sun.com website — j2sdk-1_4_2_18-windows-i586-p.exe. I was able to compile my basic java script using javac.
Before i was able to use the command javac, I went to the MS-DOS PROMPT and type PATH=c:\ j2sdk-1_4_2_18\bin.
Now I can use javac FILENAME.java to compile the file. Once the file is compiled, a new file will be created. FILENAME.class is the new file that was created.
To view the output of the file in MS DOS, use java FILENAME.
Posted in Programming | Print | 3 Comments »
07-08-2008 by Tons.
Article: When I surf the Net, I often see web sites filled with beautiful graphics that strive to capture my attention. Well, they do so for an instant, however I click away when I don’t immediately find relevant content. The content you add to your web site needs to attract both visitors and search engines.
Part I - How to add content to your web site to attract visitors
Part 11 - How to add content to your web site to attract the search engines
14 ways to add content to your web site and attract visitors
1. Get into the mind of your visitor. Brainstorm all the ways your visitor would think of your product. Write your content as if you are sitting next to him/her explaining your product. Don’t write in the 3rd person ie we, they, but use I, you, or your. This makes it more personal.
2. Keep it simple - write as if explaining your web site material to a 7th grader (12-13 year old). Don’t use complicated words that people would have to look up in the dictionary. You want them to understand clearly what your site is about.
3. Convey emotion - people on the Web are often in a great hurry, so you need to appeal to their emotions to stop them clicking away to the next site. Use stories, convey your experiences or include testimonies from others. This adds to your credibility and trust, 2 essential factors for doing business online.
4. Communicate quickly and efficiently - people online tend to scan rather than read everything on the page. Therefore use single lines of text for your headings and sub headings. Catch your visitor’s eye with lists, bullet points and use short, snappy, active (not passive) words in your sentences. Your paragraphs should only consist of 2-5 sentences. Long paragraphs make it hard to read (scan) your page quickly.
5. Create white space - the layout of your web page should include plenty of white space. Don’t lean text hard up against your graphics. Include white space between headings, sub headings and paragraphs.
6. Use graphics sparingly - you have heard it said, “a picture is worth a 1000 words.” That’s true but only if the picture supports your content. Don’t overdo the graphics. You may impress your visitors initially, however to keep them interested in your site, you need high quality content.
7. Create high quality content - make clear points with each paragraph you write. Each paragraph should build on the previous one, so that you are pulling your visitor through your page naturally. You are trying to pre sell the product or service to your visitor. This puts them in a natural frame of mind to buy (unlike many sites which may just have pictures of the products and a shopping cart).
8. Web page background - a colorful or busy background can make your text hard to read and may give the impression of an inexperienced webmaster. If you do use a background image make sure it complements your site’s theme, fits with your visitors experience and will increase your credibility.
9. Use the correct fonts - the offline world primarily uses “Times New Roman”. This works well in print but not online. Sans Serif fonts, such as Arial, Verdana and Helvetica are the best fonts for easy online scanning.
10. Font colors - the best colors for reading online are black text on a white or off-white background. If you want to use multiple colors only use a maximum of 3. Too many text colors on a web page make it hard on the eyes and spell inexperience. To emphasize text you can use the bold tag (<B>this text will appear bold</B>) or italic tag (<I>this text will appear in italics</I>).
11. Check spelling and grammar - run your page through spell check in your word processor. It won’t pick up all the mistakes, so make sure you read it through yourself to find other errors. Spelling and grammar mistakes convey an unprofessional impression.
12. Simple navigation - the main purpose of the navigation bar is to make it easy for your visitor to find his way around your site. Place your navigation bar on the left side or top of your page (or both). Repeat the bar at the bottom of the page so your visitor does not have to scroll back up to move on to another section. ( Read my article “How to Create an Effective Web Site Navigation Structure” http://www.isitebuild.com/navigation).
13. Get a critique - don’t fall in love with your writing and leave it there. Yes, it’s hard to listen to someone criticizing your beautiful piece of work, but swallow your pride and get your friends or family members to do a review of your web page. This will help you to refine what you have written and make it appeal to a wider audience.
14. Use specific keywords - weave targeted keywords into your web page as you write your web page content. I’ll cover “How to Write for the Search Engines” in Part II of this article.
Posted in Programming | Print | No Comments »
07-08-2008 by Tons.
First, I’d like to give credit to my co-author, Mundi King. I’ve mentioned him before — he’s the PHP guru who can wite code with both hands tied behind his back (well, almost!!) Thanks, Mundi, for great work on this! And now to it……
There are entire shelves of books dedicated to tackling the question “how should one write a program?”. It is a difficult question and you will invariably get a different answer depending on the programmer you ask.
One of the harder tasks to writing a PHP application (or any program) is to know how to break down the processes into smaller, more manageable pieces.
Many PHP files start out as simple little scripts, but after time (and many feature requests) they tend to grow into very long, complicated programs. It may even become difficult to determine exactly what the program is doing at any given point. It is at this point where breaking it down will add a bit of clarity to the program flow.
Fortunately, PHP provides a few simple, yet elegant methods for breaking out code into separate pieces. The first one we will concentrate on is PHP’s very powerful include() statement.
Let us start with an often requested web application that will allow an end user to upload an image file to the web server and then give them a list of the images they have uploaded in the past.
Starting with first things first, we know that we will need a web form that has a file upload field and a submit button. And we know we will need some php code to actually handle the upload. Here is an example.
<?
//print_r($_POST);
if($_POST[”action”] == “Upload Image”)
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES[’image_file’]))
$error[”image_file”] = “An image was not found.”;
$imagename = basename($_FILES[’image_file’][’name’]);
//echo $imagename;
if(empty($imagename))
$error[”imagename”] = “The name of the image was not found.”;
if(empty($error))
{
$newimage = “images/” . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES[’image_file’][’tmp_name’], $newimage);
if(empty($result))
$error[”result”] = “There was an error moving the uploaded file.”;
}
}
?>
<form method=”POST” enctype=”multipart/form-data” name=”image_upload_form” action=”<?$_SERVER[”PHP_SELF”];?>”>
<p><input type=”file” name=”image_file” size=”20″></p>
<p><input type=”submit” value=”Upload Image” name=”action”></p>
</form>
<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo “<br>\n”;
}
}
?>
Without going into great detail, here is the basic low-down of the up-load.
The bulk of the php code only get activated if $_POST[”submit”] is “Upload Image” and this only occurs if someone has pressed the submit button. The form actually submits back to itself by using the <?$_SERVER[”PHP_SELF”];?>. Inside the HTML of the form, we will print out any error messages that may have occurred. This script also assumes that there exists a directory named “images” that is writable by the web server. You can usually accomplish this by ftping into your website and making a folder named “images” and changing the permissions of the folder to 777 or xxx or execute execute execute.
You may want to take a look at this tutorial about uploading files.
http://www.htmlgoodies.com/beyond/php/article.php/3472551
Now that we have our uploading script, it is time to take a step back and see if there is a way that we can make it more modular. Imagine a scenario where you are working with a team, and a designer who only knows HTML wants to modify the form. This could lead to a dangerous situation where he inadvertently changes some php code when he just wants to change some HTML.
Here is where php’s include() comes in handy. include() lets you grab other files and php will automatically insert everything from that file at the exact place you invoke the include() statement. To accomplish this, one only has to type the file name in-between the parentheses of the statement. For example, include(”myfile.txt”);
If we separate out our one giant php script so that the html part is in another file and leave most of the php in the original script, the HTML designer can go into that file and not have to worry about changing code.
After putting in our include, the main file would appear like this…
<?
//print_r($_POST);
if($_POST[”action”] == “Upload Image”)
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES[’image_file’]))
$error[”image_file”] = “An image was not found.”;
$imagename = basename($_FILES[’image_file’][’name’]);
//echo $imagename;
if(empty($imagename))
$error[”imagename”] = “The name of the image was not found.”;
if(empty($error))
{
$newimage = “images/” . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES[’image_file’][’tmp_name’], $newimage);
if(empty($result))
$error[”result”] = “There was an error moving the uploaded file.”;
}
}
include(”upload_form.php”);
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo “<br>\n”;
}
}
?>
and our mostly HTML file would be this…
<form method=”POST” enctype=”multipart/form-data” name=”image_upload_form” action=”<?$_SERVER[”PHP_SELF”];?>”>
<p><input type=”file” name=”image_file” size=”20″></p>
<p><input type=”submit” value=”Upload Image” name=”action”></p>
</form>
The next step is to have something that actually shows what images are in the directory. We can write a quick script that will iteratively go through images directory and echo out what is there like this…
<?
$handle = @opendir(”images”);
if(!empty($handle))
{
while(false !== ($file = readdir($handle)))
{
if(is_file(”images/” . $file))
echo ‘<img src=”images/’ . $file . ‘”><br><br>’;
}
}
closedir($handle);
?>
Again, here is where the power of include comes in handy. We can provide a direct link to our list_images.php, but we can also just include it in our original upload.php This saves us from having to write it twice!
So at the bottom of our upload.php we can just include the include like this…
<?
//print_r($_POST);
if($_POST[”action”] == “Upload Image”)
{
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES[’image_file’]))
$error[”image_file”] = “An image was not found.”;
$imagename = basename($_FILES[’image_file’][’name’]);
//echo $imagename;
if(empty($imagename))
$error[”imagename”] = “The name of the image was not found.”;
if(empty($error))
{
$newimage = “images/” . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES[’image_file’][’tmp_name’], $newimage);
if(empty($result))
$error[”result”] = “There was an error moving the uploaded file.”;
}
}
include(”upload_form.php”);
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo “<br>\n”;
}
}
include(”list_images.php”);
?>
So far, our little application has been kept basic to emphasize the program flow and not get bogged down in the details. But our next step is to enhance the functionality of our gallery.
Posted in Programming | Print | No Comments »
07-08-2008 by Tons.
The single most important thing I tell people who use PHP is to turn error reporting to its maximum level. Why would I want to do this? Generally the error reporting is set at a level that will hide many little things like:
These factors might not seem like that big a deal — until you develop structured or object oriented programs with functions and classes. Too often, writing code with the error reporting turned up high would cost your hours as you scoured long functions that didn’t work because a variable was misspelled or not accessible.
PHP won’t tell you anything in that case – it’ll just create the new variable for you and initialize it to zero. The remedy is to put the following line at the top of every PHP document as you develop:
error_reporting(E_ALL);
It simply forces the error reporting to be at its highest level. Try putting this line in other PHP programs, and more often than not you’ll receive a barrage of warning messages that identify all the potentially wrong elements of the code.
I never recommend using ” (double quotes) when programming with PHP. Always use ‘ (single quotes) unless you need the features of ” (double quotes). You might think it’s much easier to write code as:
echo "Today is the $day of $month";
However, using single quotes forces variables to be outside the quotes; instead, you must use the period (.) to combine strings. It makes for faster coding but can be more difficult for other programmers to read. Let’s look at what would happen if we put an associative array value in the previous code:
echo "Today is the $date[‘day’] of $date[‘month’]";
You would receive a parse error and it would be harder for another team member to read. Two correct ways to write that line of code would be:
echo 'Today is the ' . $date[‘day’] . ' of ' . $date['month'];
and
echo "Today is the {$date['day']} of {$date['month']}";
These might not look as pretty as the original code, but syntactically they are both correct. Additionally, I believe the first method, with single quotes, is easier to read.
The use of single and double quotes also applies to associative arrays. Consider this code:
$SESSION[team] = $SESSION["old_team"];
One main problem exists in that line of code. The associative entry team on the left side needs to have single quotes around it; otherwise, PHP will think it’s a define and give you a warning message (only if error reporting is at maximum). I would recommend that the code should look like this:
$SESSION['team'] = $SESSION['old_team'];
I wish I’d known the difference between single and double quotes as they pertain to strings when I first learned PHP.
Getting Syntax Highlighting for PHP with Dreamweaver 4
As I was programming in PHP using Dreamweaver 4 as my text editor, I came across a handy little trick. You can fool Dreamweaver into thinking that code on the page is JavaScript, so that it applies the same syntax highlighting rules to your PHP as it would to JavaScript.
JavaScript and PHP both draw from the same languages, including C and Java, and therefore have many aspects in common. Try it out and see for yourself. Add this code to the top of a PHP document, open it in Dreamweaver 4, and go to the text editor (you can click Ctrl-Tab on your keyboard to switch views):
/* >The code to get PHP syntax highlighting with Dreamweaver
What the code does is close out the most recent left bracket on the page (typically from
<script language=”JavaScript”> (courtesy of david@superupdate.com) */<?php) and sets the script language to JavaScript for the remainder of the page. The code itself doesn’t affect PHP because it is commented out using the /*…*/ commenting system.
I hope these little PHP coding tips make your development time more efficient, and your resulting code more effective!
Posted in Programming | Print | No Comments »