Monthly Archives: January 2011

Recommended PHP reading list

Standard

Once upon a time I frequently visited a good php link which I’d like to share with others.
Recommended PHP reading list

Advertisements

A CMS without database

Standard

//I’d like to add, as “code” is not working here, it remains incomplete.
//In near future I’d like to complete it….

Probably PHP has more flexibility than any other web language. For instance, you need not declare data type. You name your variables according to your requirements and later use them.

PHP vastly based on array concept. Few Global arrays like $_POST, $_SERVER and $_FILE play a major role throughout the process of building an application. So far I learnt, I can assure, for small or even medium size application PHP is unparalleled.

Let us build a Simple Content Management  System in PHP that will not take any help of database instead the content will be saved in a text file in a folder you mentioned.

Suppose you’d like to save your data in a folder called “cms”. So the path will be like this : cms/data.txt.

Let us start with a form that takes user inputs. The code is simple. Data passed through post method and we’ll catch them in a separate page.
First the form.

<code><fieldset><legend>Adding Contents</legend><form method=”post” action=”add_data.php”><br>Name : <input type=”text” name=”uname” /><br>Comments : <textarea name=”txt” cols=”50″ rows=”5″></textarea><br><br><hr><input type=”submit” value=”Send” /></form></fieldset>></code>

Next add_data.php page where we add contents in a text file:
$id = 1;

if (file_exists(“data.txt”))
{
$fp = fopen(“data.txt”, ‘r’);
$str = fread($fp, filesize(“data.txt”));
$str_arr = explode(“|”, $str);
foreach ($str_arr as $rec)
{
if ($rec)
{
$id++;
}
}
}

$mode = (file_exists(“data.txt”))? ‘a’ : ‘w’;
$fp = fopen(“data.txt”, $mode);
$str = $id . “–” . $_POST[‘uname’] .”–” . $_POST[‘txt’] . “|”;

fwrite($fp, $str);
fclose($fp);

header(“location: index.php”);

As you see in the last line after you add data it redirects you to the index.php page. The code is:
$fp = fopen("data.txt", 'r');
$str = fread($fp, filesize("data.txt"));
$str_arr = explode("|", $str);

echo ”

“; echo “”;foreach ($str_arr as $rec)
{
if ($rec)
{
$rec_arr = explode(“–“, $rec);
foreach ($rec_arr as $col)
{
echo ”
“;echo “”;
}
echo “Edit“;
}
}
echo ”

$col

“;

echo “Add data“;

I’d like to edit and delete data on index page. But that part in the next blog. Till the next one happy coding!

ASP.NET and PHP, a basic difference

Standard

In ASP.NET you need to declare data type of variable explicitly. Like int num; that means num variable knows how much place it will hold in memory. It could be string str; float flt etc. But in PHP you need not do that. Usually you write $number to declare a integer variable.
So it is wise to pick up variable name so that it will mean either integer or string.
In PHP when you take up someone’s name you may write $name. It could be a string.
Actually what happens, PHP engine interprets the variable according to the value you are assigning it. That makes it operative part a little bit easier.
The CRUD concept is present in both. But, in ASP.NET it is more complicated and divided into many sections. Of course, people who know ASP.NET, may find it more organized too.
Lastly, due to windows, ASP.NET gives you a desk top experience and much more user friendly. If PHP gets something like this (I know nothing about zend framework although), it will be unstoppable and unparalleled due to its Open Source Presence.
Lastly, to host a ASP.NET site is more costly than PHP, and other Open Source helps make PHP much more desirable.

ASP.NET and PHP

Standard

I started this blog mainly with a plan – I’d try to evaluate ASP.NET and PHP, tracing a honest comparison between these two great programming languages.
Frankly speaking when I tried to do my hands dirty with codes, I found that I have still miles to go in PHP specially. I’m still learning ASP.NET, but about PHP I’m still lurking in darkness. So I’d better have a grasp first.
One thing I can say for sure, PHP is easier to pick up and easier to implement.
If you consider The OOP part, Object Oriented part of ASP.NET will take some time to learn and understand and it gets complicated a little bit with newly built classes belong to App_Code folder and calling them into Page class and run methods.
In PHP, just use include method anywhere, either in the class where you’d like to call a function from another class or include in your PHP page.
So OOP seems bit easier to me in PHP than ASP.NET.
I’d like to write more about it later.