PHP Programming 101

December 18th, 2014

PHP Scripts on the Web

Posted by Conrad in Basic Programming, Sample Code

PHP Scripts on the Web

There are a lot of PHP scripts you can check out on the web. Some PHP scripts can even be downloaded and used on your own websites. Most of these scripts can be customized accordingly, but just make sure you save it as another file since in the event you have errors, you may find yourself having to revert to the original file.

You will be surprised at the number of scripts you can find on the web. A lot of them are good and others really basic. But the thing to get out of them is really the script design structure to which will help you learn. After seeing all of these, you will surely get yourself to write you very first PHP script to display on the Internet.

December 11th, 2014

PHP BASIC CODING

Here are some of the basic things to be in mind while coding your dynamic web pages using PHP.

I. PHP basic syntax

PHP is a script that is place inside a HTML document that’s why you need to put the basic codes of HTML such as and .
PHP is open with the (?)question mark followed by “PHP” and closed with a (?) question mark.

II. Displaying in PHP

In order to display a text in PHP you need to use the keyword ECHO or PRINT. all code line ends with a semicolon.

To be continued…

firefoxss23hqcom.jpg

image source: www.23hq.com

December 4th, 2014

PHP Basics

Posted by Conrad in Basic Programming, Sample Code

Now that you have installed the necessary web server software and tested that it works (which is included in the manual) we can now get to know the basics of PHP. For our guide we will be using HTTP combined with PHP, this allows PHP code to be embedded into regular html pages and thus simplifying the execution by simply requesting the page. PHP uses start and stop tags in the form of “?php” and “?” and below is a sample:

".. PHP Code".
?>

The following sample has PHP code embedded within HTTP:



Fan: Who are You?

//print output
Echo "My name is MacGyver";?>

Upon execution or opening the page this would give you text in the browser stating the following words. “Who are You?” “My name is MacGyver“. This would be the equivalent hello world program many books use in teaching the basics of programming.

November 27th, 2014

How to Export Data From MySql to CSV

Posted by Edi in Codes, PHP

This is a simple code that exports mysql database data to csv (delimated) data.

//a small class for database connectivity
class database {
private $db_handle;
private $user_name;
private $password;
private $data_base;
private $host_name;
private $sql;
private $results;
function __construct($host = “localhost”, $user, $passwd) {
$this->db_handle = mysql_connect($host, $user, $passwd);
}
function dbSelect($db) {
$this->data_base = $db;
if (!mysql_select_db($this->data_base, $this->db_handle)) {
error_log(mysql_error(), 3, “/phplog.err”);
die(”Error connecting to Database”);
}
}
function executeSql($sql_stmt) {
$this->sql = $sql_stmt;
$this->result = mysql_query($this->sql);
}
function returnResults() {
return $this->result;
}
}
//database variables
$host = “localhost”;
$user = “root”;
$passwd = “”;
$db = “bg_db”;
$sql = “SELECT * FROM table ORDER BY id”; // a query to fetch records from database
$dbObject = new database($host, $user, $passwd);
$dbObject->dbSelect($db);
$dbObject->executeSql($sql);
$res = $dbObject->returnResults(); // result reasource
$newFileName = “emp_names.csv”; //file name that you want to create
$fpWrite = fopen($newFileName, “w”); // open file as writable
$nameStr = “”;
$rows = mysql_fetch_assoc($res); // fetching associate records
$sStr = “”;
//first store the fields name as header of csv in $sStr
foreach($rows as $key=>$val) {
$sStr .= $key.”,”;
}
//then store all records
do {
$sStr .= “– —–\n”; //to seprate every record
foreach($rows as $key=>$row) {
$sStr .= $row.”,”;
}
}while($rows = mysql_fetch_assoc($res));
$sStrExp = explode(”– —–”, $sStr);//separate every record
foreach($sStrExp as $val) {
$sStr2 .= rtrim($val, “,”);
}
echo $sStr2;
fwrite($fpWrite, $sStr2); //now write to csv file
fclose($fpWrite);//close file
?>

November 20th, 2014

The Convenience of Ready-Made Scripts

Posted by Hannah in Codes

So you know the basics of PHP coding, and you’re really keen on trying out your new-found knowledge and skills; that’s all and good, but who said that you always have to build everything from scratch.

One of the best things about PHP is that its popularity means that there’s a plethora of ready-made scripts and programs that you can get on the Internet (both free and paid), which you can use for your particular need. Take the smart route when programming with PHP and save yourself the trouble of coding everything so that you can focus on problems and/or apps that really need to be coded from scratch.

Just make sure that the PHP scripts you get online have a license that will allow you to make modifications or can be embedded as part of your bigger code if you need to use it that way. A lot of PHP scripts like financial tools scripts are copyrighted so make sure you don’t get in trouble just for overlooking legalities.

Image via Eric Mueller

November 12th, 2014

PHP BASIC CODING (cont…)

(cont…)
III. PHP Variables

$variablename=value;

Variables are used for storing values such as text string or arrays. And all variables in PHP starts with a ($) dollar sign. There are various rules to be follow before naming a variable. 1) A variable name must start with a letter or an underscore. 2) A variable name can only contain alpha-numeric characters and underscores. Special characters are not allowed. 3) A variable name should not contain spaces.

IV.PHP Strings

$wrd=”Hello World”;
print $wrd;

Strings variables are used for values that contain character strings. The only operator that can be used on string is concatenation denoted by the sign (.) period.

$wrd1=”Hello World”;
$wrd2=”Good day”;
print $wrd1.”".$wrd2

You can count the nos. of string with the function strlen().

Print strlen(“HelloWorld and GoodDay”);

to be continued…

php-designer3php-editorscom.jpg

image source: www.php-editors.com

November 5th, 2014

6 Tips For Better Form Design (3/3)

Posted by binary in PHP


INFORM THE USERS ABOUT INPUT LIMITS

When your database has a field limit, situate a size limit on a text box – this stops people from entering too much text only to find out that their data has been trimmed down by your database.

KEEP FORMS SHORT
Never make forms too long and verbose- this may cause confusion to people and may intimidate them.

TELL THEM WHERE THEY ARE
In case of splitting forms by pages, it is a good idea to let your visitors know how far they are through the process of form submission. This lets people know where they are at the process all times.

October 29th, 2014

PHP BASIC CODING (cont…)

(cont….)

V. Operators in PHP

*Arithmetic Operators*

+ addition
- subtraction
* multiplication
/ division
% modulus (division remainder)
++ increment
– decrement

*Comparison Operators*
== equal to
!= not equal
> greater than
= greater than or equal
<= less than or equal

*Logical Operators*
&& and
|| or
! not

I know its self explanatory…

VI. IF….Else Statements

If (condition)
//*Things to be executed if the answer to the condition is true*//
else
//*Things to be executed if the answer to the condition is false*//

If…else statement is use if the user wants to execute a set of code when the condition is true and another if the condition is not true.

VII. Switch Statement

switch (expression)
{
case 1:
//*code to be executed*//
break;
case 2:
//*code to be executed*//
break;
default:
//*code to be executed*//
break;
}

Switch statement is use if you want to select one of many blocks of code to be executed. this statement is used to avoid long blocks of if…elseif..else.

VIII. Looping Statement

while(condition)
code to be executed;

While statement is used to execute a block of code if and as long as a condition is true

do
{
code to be executed;
}while (condition)

Do—while statement is used to execute a block of code at least once and it will repeat the loop as long as the condition is true.

for each (array as value)
{
code to be executed;
}

For each statement is used to loop through arrays.

I think this is already enough and it has added new knowledge to those who are new in PHP… good luck and happy coding…

php_2.jpg

image source: www.cass.rrhosting.com

October 22nd, 2014

Automated Testing

Posted by Hannah in Programming

robotAny programmer knows that you need to test your code before it is declared ready. And while the coding part can wring your brains out, it is during the testing and debugging phase that things can really get frustrating. However, unless you want to be known for buggy programs, there’s nothing you can do but go through the testing phase.

One way to make your life easier is to automate the testing instead of doing it manually. This is especially true if you need to do exhaustive time-consuming test runs to run repetitive scenarios. Using automated testing will help you get more reliable results at a faster rate, usually meaning savings in terms of human capital and financial resources.

However, automated testing does require you to spend time and resources initially for the test programs are written. The more sophisticated the test program you need, the more resources you will need to spend on it. You can say that you need more than just basic programming skills for this. So before you do jump on the automated testing bandwagon though, you need to weigh the scope of your project to see whether you will really benefit much from automated testing or if manual testing will do.

Image via Crystal

October 15th, 2014

Select multiple HTML tag

Posted by editor in Codes, Information
Tips to simplify your life

Tips to simplify your life

Want to select multiple items from a list? Then you need to use the select multiple tag in HTML. The action handler then takes care of the form when the items are passed through it. What happens though, is that all of them are passed with the same widget name.

For example:
<select name=var multiple=yes>
The option you select will register with the action handler as var=option1, var=option2, var=option3. This thereby overwrites the previous $var variable’s contents.

The fix is to utilize the array from form element feature of PHP.

Use it like so:
<select name=var[] multiple=yes>
The first item then becomes $var[0], and then $var[1] for the next, and so on.

Hope this <a href=”http://phpprogrammingtips.net/information/looping-statements-doesnt-loop/”>tip</a> helps.

Next Page »