beginner - mixing HTML and PHP
Posted: Tue Jan 20, 2004 9:26 am
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
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
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.
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
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
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()
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
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';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>Code: Select all
$host = "localhost";
$user = "yourname";
$password = "yourpassword";
$DBname = "yourdatabase";
$tablename = "login";Code: Select all
$link = mysql_connect ($host, $user, $password);Code: Select all
$query = "SELECT * FROM login";
// select the database
mysql_select_db($DBname);
// query the database
$result = mysql_query($query);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);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