Page 1 of 1

beginner - mixing HTML and PHP

Posted: Tue Jan 20, 2004 9:26 am
by malcolmboston
ok i thought i'd write out some tutorials because i notice alot of 'advanced posts' are being bumped off for easy and simple problems which could be easily researched just by clicking on search

ok here we go

i am not writing out these codes to try and teach, i am gonna be trying to point out the simple commands in PHP that are extremely easy to learn and invaluable also listing common pitfalls.

please feel free to make adjustments to the code, because i have not tried to make it perfect, just 'newb-friendly'

ok so what is the purpose of this script?
- turn 1000+ lines of HTML into 30-50 lines of PHP,
- mix PHP and HTML together WITHOUT errors
- Show how a common App could be created

ok you'll need this first

Code: Select all

CREATE TABLE login (
  username varchar(25) NOT NULL default '',
  email varchar(40) NOT NULL default '',
  access_level varchar(20) NOT NULL default 'Member'
) TYPE=MyISAM COMMENT='actual login form used for logging in';
insert that into PHPMyAdmin, without it you will not see the end results and its just easier if i provide the code

ok heres the whole PHP script, ill break it down in a minute

Code: Select all

<html>
<head>
<title> looping rows demo </title>
</head>
<body>
<table width=500 border=0 cellspacing=0 cellpadding=0>
  <tr bgcolor=#009999>
<td><strong>Username</strong></td>
<td><strong>E-Mail Address</strong></td>
<td><strong>Status</strong></td>
</tr>
<?php
// database variables for connections 
$host = "localhost"; 
$user = "yourname"; 
$password = "yourpassword"; 
$DBname = "yourdatabase"; 
$tablename = "login"; 
//connection variables completed 
// establishing connections 
$link = mysql_connect ($host, $user, $password);
//connection established 
//the query defined 
$query = "SELECT * FROM login"; 
// select the database 
mysql_select_db($DBname); 
// query the database 
$result = mysql_query($query); 
$data = mysql_fetch_array($result, MYSQL_ASSOC);
print "
<tr bgcolor#9BD7FF>
    <td width=35%>$data[username]</td>
    <td width=40%>$data[email]</td>
    <td width=25%>$data[access_level]</td>
  </tr>";
while ($row = mysql_fetch_assoc($result))
print "
<tr bgcolor=#9BD7FF>
    <td>$row[username]</td>
    <td>$row[email]</td>
    <td>$row[access_level]</td>
  </tr>";
?> 
</table>
</body>
</html>
ok so what is this all doing i hear you ask, well basically its just displaying all the information in a database, simple as that, however this could be easily adapted for use in a CMS (Content Management System). lets break it down.

Code: Select all

$host = "localhost"; 
$user = "yourname"; 
$password = "yourpassword"; 
$DBname = "yourdatabase"; 
$tablename = "login";
this bit of code is used to define the connection variables, its not mandatory to use this but does make it easier for bigger projects

Code: Select all

$link = mysql_connect ($host, $user, $password);
this is making a connection to the database, notice how the order of the variables, any other order would cause an error connecting to the database

Code: Select all

$query = "SELECT * FROM login"; 
// select the database 
mysql_select_db($DBname); 
// query the database 
$result = mysql_query($query);
ok now we're at the meat of the code, where all the data is retrieved
the query is self explanatory, however not how there is a $var created by that piece of code, this means that $query is equal to whatever the value of the query is.
mysql_DB is a standard field and self-explanatory so i wont bother explaining it.
ok, $result = mysql_query, ok this is where its all happening, this piece of code creates an array, if you dont know what arrays are go view your PHP docs, the subject is too large for me to cover, creating an array is absolutely vital to the rest of this script, also you can test if the query has returned anything by using mysql_affected_rows()

Code: Select all

$data = mysql_fetch_array($result, MYSQL_ASSOC);
now because data in an array cannot be 'directly echo'd' we need to do something with it, that is what that line is for, read the actual code it is obvious what it is doing, notice also that another var is created with the result, MYSQL_ASSOC finds all the relevant information from your query set and is invaluable in this circmustance.

ok im not gonna tell you the rest because now you should be able to figure out what the rest does (you dont learn without getting shown) however i will point out potential syntax problems

when you are placing HTML within PHP it is vital that you either
- dont use "" beside your attributes
- you escape it
Otherwise PHP believes you are ending a 'php tag' and will throw up all sorts of nasty errors and exceptions

also before i go i would like you to notice the way i have set up the HTML, doing it this way makes sure that even if there are no results to display, the table will still render properly

if you still require help.........
..........read the manual, then post back

Posted: Tue Jan 20, 2004 10:09 am
by redmonkey
Considering the title of this thread is 'beginner - mixing HTML and PHP' you seem to have only devoted 4 lines about the actual subject....
malcolmboston wrote: when you are placing HTML within PHP it is vital that you either
- dont use "" beside your attributes
- you escape it
Otherwise PHP believes you are ending a 'php tag' and will throw up all sorts of nasty errors and exceptions
Using statements like the following.....
malcolmboston wrote: read the actual code it is obvious what it is doing
malcolmboston wrote: mysql_DB is a standard field and self-explanatory so i wont bother explaining it
....are not exactly helpful.

Your whole speil seems to revolve around your working with a database (which you don't explain at all) not outputting HTML from within a PHP script.

You refer to data being echo'd, yet you use print in the example, there is no explanation as to what the while loop does, yet this is the part of the code that deals with outputting HTML?

If you put together a tutorial with code examples you should at least take the timeout to ensure that your code will work (which yours doesn't).

A tutorial of this title should at least include some details about working with echo, print, heredoc and breaking out of PHP to do some HTML then breaking back in, which are the most common ways of producing HTML within PHP scripts. Generally, tutorials are simple examples which users should find easy to follow, yours on the otherhand makes unessecary use of a database and associated code which bears no relevance to the subject matter.

Personally, I think your tutorial would more confuse than help anybody new to PHP

Posted: Tue Jan 20, 2004 10:30 am
by malcolmboston
ok well, tbh i think your wrong

fine, its a 'beginners tutorial', well if its a beginners tutorial why the hell should i be explaining heredoc, that is certainly not a beginners topic.

i do not believe in giving out full explanations, otherwise what are you learning, jesus the PHP manual is there and its free! now im not one to say RTFM but sometimes thats how i feel, a qusetion came on earlier about positing $vars, how many <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>' times has that been asked? 100? 200?!? use the manual! its your friend.

as for explaining everything.
if i really have to explain what echo means then you need to find another day job, without even a PHP background, i knew what print and echo meant. also does that mean i have to explain what <table width=100%> means? No i dont

dont think im dissing you, i see what you mean (in some parts)and i will correct it tomorrow because ive got my own projects to complete, its just annoying when 80% of the forums (and yes some of my questions) are taken up by useless questions that have already been answered, and btw the code does work im using it at the moment for part of a CMS newsletter list

Posted: Tue Jan 20, 2004 10:33 am
by malcolmboston
ok well, tbh i think your wrong

fine, its a 'beginners tutorial', well if its a beginners tutorial why the hell should i be explaining heredoc, that is certainly not a beginners topic.

I do not have time to explain what every single command does, i have a life other than programming if people want more information, i written out a manual the way i would of found it easier to understand, annotated so you get a brief idea of what is happening, if you want further information then go view the manual.

i do not believe in giving out full explanations, otherwise what are you learning yourself, jesus the PHP manual is there and its free! now im not one to say RTFM but sometimes thats how i feel, a qusetion came on earlier about positing $vars, how many <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>' times has that been asked? 100? 200?!? use the manual! its your friend.

as for explaining everything.
if i really have to explain what echo means then you need to find another day job, without even a PHP background, i knew what print and echo meant. also does that mean i have to explain what <table width=100%> means? No i dont

dont think im dissing you, i see what you mean (in some parts)and i will correct it tomorrow because ive got my own projects to complete, its just annoying when 80% of the forums (and yes some of my questions) are taken up by useless questions that have already been answered, and btw the code does work im using it at the moment for part of a CMS newsletter list.

also i tried to explain the format of HTML outlining the pitfal people can also see it visually on the screen

and please dont tell people it dont work because that means your servers screwed

Posted: Tue Jan 20, 2004 10:58 am
by Roja
malcolmboston wrote: i do not believe in giving out full explanations
Then don't post a 'beginners guide'.

A beginner has little or no knowledge, and needs full explanations to help ease the transition from lack of knowledge to knowledge.
malcolmboston wrote: , otherwise what are you learning, jesus the PHP manual is there and its free!
And thats clearly not enough of a resource for most people. While it is fairly well written and well commented, there are definitely areas (GD, sessions, cookies) that are not clear, not easy to understand, and highly confusing - even for the experienced user. As such, on occasion, it is nice to have a place where you can go to ask questions, and get help.

The manual, while helpful is NOT all you need to learn php.
malcolmboston wrote: now im not one to say RTFM but sometimes thats how i feel, a qusetion came on earlier about positing $vars, how many f**king' times has that been asked? 100? 200?!? use the manual! its your friend.
And it will be asked another 1,000 times. Because its not clear, obvious, and simple for new users. Thats partially because php isnt natural-language (and the new user doesnt 'get it'), but its also because it could be made clearer - which is why we have forums, and 'beginner' posts to help them.
malcolmboston wrote: as for explaining everything.
if i really have to explain what echo means then you need to find another day job, without even a PHP background, i knew what print and echo meant.
Did you know that print cant be called using variable functions? Did you know that one is slower than the other? Did you know that print doesnt actually PRINT (it outputs), and that echo doesnt always echo (if output buffering is on)?

All of those are fine points that a beginner might not 'get', just by inference. Its important to be clear, consistent, and helpful, and I agree with redmonkey.. you weren't.
malcolmboston wrote: also does that mean i have to explain what <table width=100%> means? No i dont
You don't have to explain anything. But if you are going to make a beginner's post, then yes, to avoid criticism, you should document well, and be consistent.
malcolmboston wrote: dont think im dissing you, i see what you mean (in some parts)and i will correct it tomorrow because ive got my own projects to complete, its just annoying when 80% of the forums (and yes some of my questions) are taken up by useless questions that have already been answered, and btw the code does work im using it at the moment for part of a CMS newsletter list
Then it may only work in that context. I haven't tested it, but have you tried running *only* the code you posted?

The point I'm getting at is that making a beginners post takes a considerable amount of patience, thought, and compromise. You have to over-explain things, be completely obvious, and get to a level of detail that most people will NEVER need. Thats what teaching is - you are providing the vast array of knowledge you have to someone else.

If you dont have the time (because you have your own projects to complete), willingness, or ability to write a beginners post that will be HELPFUL, then by all means, don't.

Questions on the forums get answered regardless of how many 'silly questions' there are. You won't make a dent in the number of posts you think are silly until you realize that they ARENT silly.

People are just trying to learn.

Posted: Tue Jan 20, 2004 1:02 pm
by twigletmac
I've moved this into PHP - Code, since it's not a code snippet it shouldn't be in the Code Snippet forum. This is not a comment on how useful or not the tut is, just done to prevent confusion in the Code Snippet forum.

Should you wish to have it included in the Tutorials forum then feel free to submit it to the admins. But do take into account the comments that you've received from Roja and redmonkey.

IMHO, one thing that should always be included in a beginner's tutorial is some information on debugging - running database functions without a fall back position can be very confusing.

Mac

Posted: Tue Jan 20, 2004 1:05 pm
by redmonkey
malcolmboston wrote: and please dont tell people it dont work because that means your servers screwed
No, that means your code don't work!

You go straight from your print command to closing the HTML table,body and document but don't close the PHP scripting tag first. I didn't waste my time trying to run your code, but unless I'm missing something that won't work.

Tutorials are a learning aid, sometimes they can be considered an extension of the manual or sometimes they will take a specific case and take you through it step by step detailing each as you go, that is you learn. If manuals were all you needed then there would be no tutorials and no forums on the subject. The very fact that these sort of things exist quite clearly shows there is a need for them. If manuals are all you need why do you ask so many questions on this forum?

When you write a tutorial you have to assume that your intended audience knows little or nothing of your subject matter (unless you are writing an advanced tutorial, then you should make that clear) and write accordingly.

Before I started with PHP I knew what regular expressions were and how to use them but that doesn't mean that I can assume that everybody new to PHP will automically know what regular expressions are and how to use them. It really depends on your background and where you have come from.

Personally I don't write tutorials because I don't consider myself to be the best at explaining/documenting things and I too have very little free/spare time.

There is nothing magical or mystical about heredoc, it is a very simple concept and not something that I would consider to be strictly for advanced users.

As previously pointed out, if you don't have the time to explain what you are saying then don't write tutorials.

Posted: Tue Jan 20, 2004 2:19 pm
by jason
Okay...so maybe I might have some input with regards to what constitutes a good beginners tutorial.

The best tutorials out there clearly define what they expect the user to know. A beginner's PHP tutorial is geared towards users that don't know PHP. You can safely assume they know HTML. Obviously, if they don't know HTML, they need to find an HTML tutorial.

The problem with most beginners with PHP isn't that PHP is difficult. The fact is, they don't know how to program.

That's the problem many beginner tutorials make, is that they assume the user knows how to program. Such is the case with this tutorial. It's basically an overview of the syntax. Guess what? A new PHP user is still going to have questions after this tutorial. And they will still be the same basic questions that are always there.

A beginners tutorial does, however, need to explain things clearly. "ok im not gonna tell you the rest because now you should be able to figure out what the rest does (you dont learn without getting shown) however i will point out potential syntax problems " is a not explaining things clearly. The problem is even if you say they can look it up in the manual, where are they going to look it up? You don't offer any direction as to what to do, or even where to go. You even mention they should read the manual, but don't provide a link. Go to http://www.php.com and tell me where the manual is. =)

It's also important that you write in a clear voice, and that you know what you are talking about.
"when you are placing HTML within PHP it is vital that you either
- dont use "" beside your attributes
- you escape it
Otherwise PHP believes you are ending a 'php tag' and will throw up all sorts of nasty errors and exceptions"
That's not entirely true. I can use quotes in my PHP strings without escaping them. I just use single quotes (') instead of double quotes (") when I do that.

Other things to consider: punctuation and spelling matter.
"ok now we're at the meat of the code, where all the data is retrieved
the query is self explanatory, however not how there is a $var created by that piece of code, this means that $query is equal to whatever the value of the query is. "
That is a very long sentence. =)

I think the biggest piece of advice I can give you with regards to writing tutorials is to take your time. I know that sometimes when I write, I just want to get it done. But that's the worst thing you can do. Take your time, spell things out clearly, and above all, be clear in what you want to say.

Don't take this as me saying "No, you suck!", but rather, as someone who hopes that you will continue trying to write tutorials.

Posted: Tue Jan 20, 2004 2:21 pm
by jason
Note about script: Yes, it does NOT work.

Debug session started.
Parsing Error: PHPDocument1 line 44 - parse error, unexpected '<'
Debug session ended.