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