Author Topic: Starting PHP coding for the internet.  (Read 20032 times)

0 Members and 1 Guest are viewing this topic.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Starting PHP coding for the internet.
« on: June 25, 2009 »
Well, I don't know how much call there is for this and you can look at this as a very basic overview of how to start coding in PHP, if there is enough feedback I will expand it a bit more to try and help you guys learn how to code your own dynamic web pages.

Why use PHP?

Well, if you have dabbled in making web pages, you may have used HTML, and all it is really is a markup language to enable you to control the formatting and layout of pages.

The internet is said to exist in a "stateless protocol", The HTTP bit on the addresses for your web page stands for HyperText Transfer Protocol, you punch in an address, the address retrieves the HTML page back from the server... This is fine if you have a simple page that doesn't have any dynamic content.

Dynamic content?

Well, what if you were making an online store or a forum or a shoutbox or something where a user can interact with the page?

Because web pages were static, this should not normally be possible, we achieve log on and such with cookies and sessions and cheat our way around it..

Without going into how sessions and cookies work, we clearly need a way to pass variables and other parameters to our web pages and get them to generate different content depending on what we pass to it..

We're going to step into a little code, don't worry though, you don't need to grasp this quite yet, you just need to get the concept :)

Consider the following page:

http://www.dbfinteractive.com/downloadmenu.php

The user is presented with a basic menu in which the search criterea can be adjusted.. The user clicks on "GO" and then the results are displayed.

This is not black magic, we need to look at the HTML source of that page to see what is going on so right click it, click on "view source" and inside the source you will come to this;

Code: [Select]
<form method="post" action="productions.php">

<br />
<center><b class="contactwhite">DISCLAIMER!!<br /></b>
<b class="contactdesc">We do our best to keep these downloads up to date, however we can't be expected to
trawl the message board to find your libs to add to the database, so when you release a new version or you update
your library, please use the contact page or the forums to let us know so that we can update the archive!
</b></center><br />
<center><b class="contactwhite">WHAT WOULD YOU LIKE TO SEARCH FOR?<br /><br /></b></center>
<b class="contactdesc">
<input type ='radio' name='search_value' value='1' >UTILITIES -- These are stand alone programs written to do menial tasks.<br />
<input type ='radio' name='search_value' value='2' >MUSIC REPLAY -- A wide selection of different replayers.<br />

<input type ='radio' name='search_value' value='3' >LIBRARIES -- Frameworks and libs to load gfx etc.<br />
<input type ='radio' name='search_value' value='4' >DX DLLS -- We keep a selection here to save you searching the net.<br />
<input type ='radio' name='search_value' value='5' checked='checked'>ALL -- This option will return everything above!
<center><br /><b class="contactwhite">ORDER SEARCH RESULTS:<br /><br /></b></center>
<b class="contactdesc">
<input type ='radio' name='search_order' value='1' checked='checked'>ALPHABETICAL -- Return your results alphabeticaly.<br />
<input type ='radio' name='search_order' value='2' >DATE RELEASED -- By the date that we think the download was released.<br />

<input type ='radio' name='search_order' value='3' >DATE ADDED TO DATABASE -- By the date added to our database.<br />
<input type ='radio' name='search_order' value='4' >MOST DOWNLOADED -- In order of popularity (most to least).<br />
<input type ='radio' name='search_order' value='5' >BY AUTHOR -- Returns them grouped by author.<br />
</b>
<center><br /><input type = "submit" class="input" value="-- GO! --" /></ center>
</form>


This is a standard HTML form with radio buttons and a submit button, the first thing to note is the first line..

Code: [Select]
<form method="post" action="productions.php">
The method "post" quite literally "posts" the variables set by the form to the script contained in the action field. In this case, when submit is clicked the form will load a script called "productions.php", which in turn will access a database and construct a web page based on what the user chose..

The choices are contained in the variables "search_value" and "search_order".
The form is really simple, but you should be able to begin to see the benefit of php a little... The form calling a page that actually constructs another web page based on the values passed to it.

This is one of the reasons you will love php.

What is PHP?

PHP is a language that exists on your web server, the beauty of this is that it is platform independant, it will work with any computer that can display web pages and what it does is to process PHP commands and generate HTML code which is sent to the web browser.

It exists on the server, it is known as a "server side application" , Flash for example exists on the users computer and that is a "client side application".

We identify PHP pages by giving them a .PHP file extension as opposed to .html.

When we request a page like "index.php" this tells the server that the page contains php commands and must be processed by php before being served to the clients browser... You can mix php and html together so you can just drop in and out of php as you require, it really is as simple as that.

At a very simple level...

Code: [Select]
<?php
echo'<html>';
?>

<head>
</head>
<body>
<?
echo'</body>';
echo'</html>';
?>

This shows how you can mix html and php together, for this to work though we need to have php installed on our server and this file must have a .php extension.

You will notice two different sorts of tags.
<?php
?>
and
<?
?>
Both do exactly the same thing, these tags contain php code between them, both methods will work, the top method is considered to be the most correct.

You will see that php commands are terminated with semi colons in most cases, in the example we use the "echo" command.

This command sends code to the browser :)
So...

echo'</html>';

would send;

</html>

to the browser.

Pretty simple eh?

A few caveats...

consider the following html code...

Code: [Select]
<p>I wrote my first "hello world" program on a bitchin' CBM VIC 20  a long time ago</p>

This would cause problems because of the quotes..
Did you notice that those echo commands contained the code to be sent to the browser in quotes like this;
echo '<html code>' ;

?

You can in fact do both types of quotes...

echo '<html code>' ;
echo "<html code>" ;

Both will work, so if you had the like;
<p>i fuckin' love php</p>

Then you could legally send it to the browser like this;

echo"<p>i fuckin' love php</p>";

if you had the line;

<p>"hello world"</p>

You could do it like this;

echo '<p>"hello world"</p>';

Either way is cool..  Obviously you could just use straight HTML to do this but sometimes you will have to send stuff to the browser with php that contains both sets of quotes and you'll need to escape the quotes with the backslash like this;


echo "<p>\"hello world\"</p>";

This should be anough to get you interested in php, you do need a few more guidelines before getting started though.

Since PHP exists on the server then php pages MUST be accessed through the HTTP protocol, this is because the server has to process the php.

So, two options are open, you can etiher install php on your computer or you can upload your pages to your webspace.
I reccomend you use xampplite and install php on your computer,

http://www.apachefriends.org/en/xampp-windows.html

Grab xampplite from there, extract the zip, click on "setup xampp"
then apache_start.bat
then mysql_start.bat

Then open your browser and type "localhost"

If you see the xampp welcome page then you have the thing working fine
Go into the htdocs directory and delete everything that you find in there.
When you drop your pages in that folder you can access them with "localhost"

The other thing that is really cool to type your code with is notepad++

http://notepad-plus.sourceforge.net/uk/site.htm

Thats the editor I use for all my web coding.

That should be enough to start you off, I will format it nicely when I get time, sorry for any errors.

Cheers,

Shockwave.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #1 on: June 30, 2009 »
So, two options are open, you can etiher install php on your computer or you can upload your pages to your webspace.
I reccomend you use xampplite and install php on your computer,

http://www.apachefriends.org/en/xampp-windows.html

Grab xampplite from there, extract the zip, click on "setup xampp"
then apache_start.bat
then mysql_start.bat

Then open your browser and type "localhost"

If you see the xampp welcome page then you have the thing working fine
Go into the htdocs directory and delete everything that you find in there.
When you drop your pages in that folder you can access them with "localhost"

Damn!  :telloff: I just downloaded the xampplite .zip, which took me about 10 minutes, and then had to download it again because it wouldn't open. Hopefully it will work this time (the download's not finished yet).
Thanks for the great guide BTW, I really appreciate it.
Its a good introduction to PHP.
Obviously though, I'm going to have to spend much more time with it first before I can write the php program I mentioned in another thread...
Is it ok if I have specific questions about PHP coding, to ask them in this thread?

Offline benny!

  • Senior Member
  • DBF Aficionado
  • ********
  • Posts: 4384
  • Karma: 228
  • in this place forever!
    • View Profile
    • bennyschuetz.com - mycroBlog
Re: Starting PHP coding for the internet.
« Reply #2 on: June 30, 2009 »
@Shocky:
Great tutorial.

...
Is it ok if I have specific questions about PHP coding, to ask them in this thread?

I would do it this way:

If you have a concrete question about certain parts of Shockwave's tutorial, I would
add them to this post. If you have general PHP question, I would create a new topic
for them.(Just my personal point of view)

But please do ask question if you need any help. This is what this forum is here for ;-)
Good luck!
[ mycroBLOG - POUET :: whatever keeps us longing - for another breath of air - is getting rare ]

Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #3 on: June 30, 2009 »
Ok, the download was finally successful, but unfortunately there is no file called "setup xampp" per se. Instead, there are files called "xampp_chkdll.exe", "xampp_cli.exe", "xampp_restart.exe", "xampp_service_mercury.exe", "xampp_start.exe", "xampp_stop.exe", "xampp-control.exe", and "xampp-portcheck.exe". So which one do I use to install it? And is this going to install a crapload of stuff on my computer which will take up a ton of space...? This is not a server...I have no server computer. The server is owned by webs.com, which I have a member site of.
Maybe I better go with the other option, and just upload the .php file(s) to my web space...
« Last Edit: June 30, 2009 by Japaneseninja »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #4 on: June 30, 2009 »
It's definately better to use xampp, purely because of the time it will save you.

What batch files are in that directory?

I am wondering if the archive has been changed by apachefriends, if so I will upload a version somewhere for you to grab.

It's definately worth getting xampp set up, if you've got the following batch files;

apache_start.bat
apache_stop.bat
mysql_start.bat
mysql_stop.bat
setup_xampp.bat
makecert.bat

Then we can use the one you downloaded, if not I'll put my version online.

I'll check back tonight when I get home from work.

Xampp is clean btw and wont install crap on your computer, the only thing that it can sometimes interfere with is if you have IM programs running that use the same ports (skype sometimes does this) but when you want to use skype you can just shut xampp down.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #5 on: June 30, 2009 »
It's definately better to use xampp, purely because of the time it will save you.

What batch files are in that directory?

I am wondering if the archive has been changed by apachefriends, if so I will upload a version somewhere for you to grab.

It's definately worth getting xampp set up, if you've got the following batch files;

apache_start.bat
apache_stop.bat
mysql_start.bat
mysql_stop.bat
setup_xampp.bat
makecert.bat

Then we can use the one you downloaded, if not I'll put my version online.

I'll check back tonight when I get home from work.

Xampp is clean btw and wont install crap on your computer, the only thing that it can sometimes interfere with is if you have IM programs running that use the same ports (skype sometimes does this) but when you want to use skype you can just shut xampp down.
I have all the ones you mentioned, except for "makecert.bat". (See attached image) So xampp interferes with skype? I use it, but not a lot, so I guess that's ok...

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #6 on: June 30, 2009 »
Ok, it seems like you have the correct software.

If you look back at the original post you'll see I have written;

Click on:
"setup xampp"
then click on:
apache_start.bat
then click on:
mysql_start.bat

These are the batch files I can see them in the screenshot you have posted.

Once you have followed those steps you should end up with two dos windows open. When you reach that stage, open your browser and type the word:

localhost

into the address bar, you should see the welcome screen.

You can then go into the htdocs directory, delete everything there and just put your php stuff into that folder, you can access it with "localhost".

Shockwave ^ Codigos
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #7 on: June 30, 2009 »
Ok, it seems like you have the correct software.

If you look back at the original post you'll see I have written;

Click on:
"setup xampp"
then click on:
apache_start.bat
then click on:
mysql_start.bat

These are the batch files I can see them in the screenshot you have posted.

Once you have followed those steps you should end up with two dos windows open. When you reach that stage, open your browser and type the word:

localhost

into the address bar, you should see the welcome screen.

You can then go into the htdocs directory, delete everything there and just put your php stuff into that folder, you can access it with "localhost".


Yes, but the problem is I don't see any file called "setup xampp"...
Or do you mean the batch file called "setup_xampp.bat"? For some reason, I thought you meant a .exe

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #8 on: June 30, 2009 »
if you open a .bat ( batch file ) with notepad, you'll notice that it's a series of commands that are passed to the .exe file when it's run.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #9 on: June 30, 2009 »
Yes, the batch file is the one you want. all three are batch files as it happens.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #10 on: June 30, 2009 »
Btw, dont bother opening the batch files as Clyde suggested, you dont need to.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #11 on: June 30, 2009 »
Ok, it seems like you have the correct software.

If you look back at the original post you'll see I have written;

Click on:
"setup xampp"
then click on:
apache_start.bat
then click on:
mysql_start.bat

Ok, I got all the way to "click on: apache_start.bat" in your instructions. It started up of course, but it seems to be hanging at the words "Apache 2 is starting...". See attached image.
Its been that way for the past 10 minutes, and I'm not sure if I should close it...I probably will if I get too impatient.

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #12 on: June 30, 2009 »
It is working :P

You should have started the mysql as well as I explained above and then tried your browser asn typed "localhost" in the browser.

Here are the steps.

setup xampp (you wont need to do this now as you'v done it already)
apache_start
mysql_start
You will end up with two dos windows (like the one you posted).
Minimize them if they are bugging you.
Open your browser and type "localhost" in the address bar, really it's as simple as clicking two batch files and opeinign a browser window.
I can''t make the explanation any simpler I am afraid.

I dont think you made any mistakes so dont worry.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #13 on: July 01, 2009 »
It is working :P

You should have started the mysql as well as I explained above and then tried your browser asn typed "localhost" in the browser.

Here are the steps.

setup xampp (you wont need to do this now as you'v done it already)
apache_start
mysql_start
You will end up with two dos windows (like the one you posted).
Minimize them if they are bugging you.
Open your browser and type "localhost" in the address bar, really it's as simple as clicking two batch files and opeinign a browser window.
I can''t make the explanation any simpler I am afraid.

I dont think you made any mistakes so dont worry.

Thanks for that. 8) Its installed and working now (I saw the welcome page)...now the question is what to do with it now that its installed.  :P I could not find an htdocs directory.
« Last Edit: July 01, 2009 by Japaneseninja »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #14 on: July 01, 2009 »
now the question is what to do with it now that its installed.  :P I could not find an htdocs directory.


Why not check your own screenshot you posted a few posts above? :P You'll clearly see the directory htdocs.

When you find it just go into it and delete everything you see, when you're working on your website you just store it in that htdocs directory and you'll be able to develop live on your computer without ever having to upload anything to the web :)

This will save you so much time, you'll come to love it.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #15 on: July 01, 2009 »
now the question is what to do with it now that its installed.  :P I could not find an htdocs directory.


Why not check your own screenshot you posted a few posts above? :P You'll clearly see the directory htdocs.

When you find it just go into it and delete everything you see, when you're working on your website you just store it in that htdocs directory and you'll be able to develop live on your computer without ever having to upload anything to the web :)

This will save you so much time, you'll come to love it.
Duh!  :o I was thinking you meant the directory was somewhere on the pages seen after typing "localhost" in my browser...and that explains why I couldn't find it there.
Thanks, much appreciation.
Still don't completely understand how that's supposed to work (i.e. how storing stuff in the htdocs directory will make it unecessary to upload stuff to my site), but I'll figure it out...

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #16 on: July 01, 2009 »
Quote
Still don't completely understand how that's supposed to work (i.e. how storing stuff in the htdocs directory will make it unecessary to upload stuff to my site), but I'll figure it out...

What it means is that you have php and mysql running on your computer (just start mysql and apache with the batch files as we previously discussed). You can write your php pages, or indeed html pages and drop them into that htdocs directory which means that you can totally develop the code for your website on your home computer without ever needing to upload the files onto your live server. You can simply view your site in development by just typing "localhost".

You will only need to upload the site to your live host when it's  finished, the whole point of this excercise is to make development of your site much faster and much easier.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Japaneseninja

  • ZX 81
  • *
  • Posts: 16
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #17 on: July 01, 2009 »
Quote
Still don't completely understand how that's supposed to work (i.e. how storing stuff in the htdocs directory will make it unecessary to upload stuff to my site), but I'll figure it out...

What it means is that you have php and mysql running on your computer (just start mysql and apache with the batch files as we previously discussed). You can write your php pages, or indeed html pages and drop them into that htdocs directory which means that you can totally develop the code for your website on your home computer without ever needing to upload the files onto your live server. You can simply view your site in development by just typing "localhost".

You will only need to upload the site to your live host when it's  finished, the whole point of this excercise is to make development of your site much faster and much easier.
Ahh, ok...that makes more sense. Thank you. :goodpost:

Offline mziskandar

  • C= 64
  • **
  • Posts: 46
  • Karma: 1
    • View Profile
Re: Starting PHP coding for the internet.
« Reply #18 on: July 02, 2009 »
I use wampserver when doing php.. http://www.wampserver.com/en/

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Starting PHP coding for the internet.
« Reply #19 on: July 02, 2009 »
I'll give that a try too :)
I always used xampplite before with no trouble wampserver looks cool too.
Shockwave ^ Codigos
Challenge Trophies Won: