The following code creates list with the elements of an array. The blue colored code segments deserves attention.
<html>
<body>
<?php
$arr = array("Sim","Sum","Shis");
$count = 0;
?>
<ul>
<?php while($count < count($arr)) :?>
<li> <?php echo $arr[$count]; $count++; ?> </li>
<?php endwhile;?>
</ul>
</body>
<html>
19 June 2009
php code inside HTML tag
Posted by আলোর ছটা at 8:26 AM 0 comments
Labels: php
07 June 2009
MySQL in PHP
Sample code for using MySQL database in PHP is posted here in this blog.
<html>
<body>
<?php
//////////// connection////////////
$con = mysql_connect("localhost","root",""); //server,user,password
if(!$con)
die("Cannot connect with myqsl: ".mysql_error());
// select a database
mysql_select_db("badhan",$con); // databaseName:badhan
//////////// insert ////////////
$q = "INSERT INTO person (PersonName, BloodGroup) VALUES ('" . $_POST["pname"] . "', '" . $_POST["bloodgroup"] ."')"; // name and bloodgroup value are posted from another PHP file
// execute the query
$result = mysql_query($q,$con);
// show result if successfully added data
if(!$result)
die ("Error in insertion : " . mysql_error());
echo "1 row successfully added!!";
///////// select ////////////
$q = "SELECT * from person";
$result = mysql_query($q, $con);
if(!$result)
die ("Error : " . mysql_error());
// explore result of select query
while($row = mysql_fetch_array($result))
echo $row["PersonName"] . " , " . $row["BloodGroup"] . "<br/>";
// close the connection
mysql_close($con);
?>
</body>
</html>
Posted by আলোর ছটা at 11:59 PM 0 comments
06 June 2009
PHP Basics At A Glance
If you have ever learnt PHP, then this blog can help you to remember the basic and syntaxes of PHP.
1. PHP Tag for scripting:
<?php
?>
2. Printing : two commands are available
echo "Hello World!!";
print "Hello World!!";
3. Simplest php file:
<html>
<body>
<?php
echo "Hello World!!";
?>
</body>
</html>
4. Be careful about semicolon (;) after each command.
5. 2 styles of comments
// this is a line comment
/* This is a
multiline comment
*/
6. Variable names starts with ‘$’ sign. Note: PHP is a loosely typed language.
$myStr = "This is my string";
7. String concatenation operator is dot (.), not plus (+)
$myText = "Your name is " . $yourName;
8. If… elseif… else statement:
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
9. switch statement:
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
10. Array:
$persons = array("Asim","Snigdha","Ashis");
$personAges = array("Asim"=>34, "Snigdha"=>30, "Ashis"=>26);
11. while loop:
while (condition)
{
code to be executed;
}
12. do-while loop:
do
{
code to be executed;
}
while (condition);
13. for loop:
for (init; condition; increment)
{
code to be executed;
}
14. foreach loop:
foreach ($array as $value)
{
code to be executed;
}
15. function:
function functionName()
{
code to be executed;
}
16. Form, $_POST
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
welcome.php:
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
17. $_GET is used for form submission method = "get".
18. $_REQUEST contains the content of all of $_GET, $_POST, $_COOKIE
Posted by আলোর ছটা at 1:02 AM 0 comments
Labels: php
05 June 2009
Installing PHP and Configuring Apache
Operating System: Windows Vista (32 bit)
Softwares:
1. Php : Download [PHP 5.*.*-* installer] from http://www.php.net/downloads.php.
2. Apache: Download [Win32 Binary including OpenSSL 0.9.8i (MSI Installer)] from http://httpd.apache.org/download.cgi.
Note:
1. Other installers are also available in both links, but I used this combination.
2. In Apache installation, user “localhost” for both Network Domain and Server Name.
Configurations:
After installation of PHP and Apache, you are not finished. You have to manually configure few things.
1. Open httpd.conf file in installed Apache directory (Path in my installation: D:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf)
2. Write the following line at the end of the file
LoadModule php5_module "D:/program files/PHP/php5apache2_2.dll"
Note: Path depends on your installed directory and used version of php.
3. Find the last occurrence of the word “directives” in httpd.conf file. Add the following two lines after the paragraph containing the search result.
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
I'm thankful to http://blog.taragana.com/index.php/archive/how-to-configure-php-5-with-apache-2-on-windows-in-2-minutes/
Posted by আলোর ছটা at 11:58 PM 0 comments