ok here we go
i am not writing out these codes to try and teach, im going 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';MySQL is an open-source database and is commonly used in PHP development as a ways and means of producing dynamic web-sites, Login systems etc. While not as powerful as other databases such as PostgreSQL, MySQL integrates with PHP seamlessly and has plenty of facilities for the less demanding user.
MySQL is also extremely fast you can find a full explanation of MySQL here
Insert the above piece of code into PHPMyAdmin ( a very good MySQL GUI), 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>";
?>Code: Select all
</table>
</body>
</html>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>Code: Select all
</table>Code: Select all
<?php
$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 as a value only has to be changed at one spot and not at various places around the page, this is purely done by preference but you will generally see all experienced coders doing this, so it is better to get in the habit now.
Code: Select all
$link = mysql_connect ($host, $user, $password);Ok, if you are a regular to these forums then you will of noticed that all of the above php.......
Code: Select all
$host = 'localhost';
$user = 'yourname';
$password = 'yourpassword';
$DBname = 'yourdatabase';
$tablename = 'login';
$link = mysql_connect ($host, $user, $password);Code: Select all
include ('database_connections.php')Code: Select all
<?php
include ('database_connections.php')the include command means if the file is not found then PHP will do its upmost to continue without the file
Code: Select all
<?php
require ('database_connections.php')
?>Code: Select all
$query = "SELECT * FROM login";
// select the database
mysql_select_db($DBname);
// query the database
$result = mysql_query($query);notice the line of code, $query, what it is doing is retrieving all the information relevant to our query, so in this case, select everything from the table called login, however notice 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, we do this because we will need to use this later to create arrays and such.
mysql_select_db is a standard field and is just telling the query where to actually find the field named login.
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 here, 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
Code: Select all
mysql_affected_rows()as the script will fail if there are no values in the array
Code: Select all
$data = mysql_fetch_array($result, MYSQL_ASSOC);Code: Select all
<tr bgcolor=#9BD7FF>
<td width=35%>$dataїusername]</td>
<td width=40%>$dataїemail]</td>
<td width=25%>$dataїaccess_level]</td>
</tr>";- change you syntax
- escape from PHP and then go back into it
let me explain the latter more closely, when i say escape php and tehn go back into it i dont mean close php (?>) and then go back into it (<?php) in that sense i will show you an example of what i mean
Code: Select all
<?php
<table width="100%">
// error above
<tr>
<td>' '</td>
<td>above and below are errors</td>
<td>' '</td>
</tr>
</table>Code: Select all
print "escaping characters is done "Like this".";
// notice quotes inside each other, the \ symbol will escape and then go back to...............<spacer> this would be find as it uses no syntax that PHP has reserved
ok we're coming to the back-end of our tutorial now, so what have we done so far?
- established a connection to our database using variables for ease of use
- ran a query to retrieve the data we want
- created an array
- mixed HTML with PHP
now for the remaining pieces of code
Code: Select all
print "
<tr bgcolor#9BD7FF>
<td width=35%>$data[username]</td>
<td width=40%>$data[email]</td>
<td width=25%>$data[access_level]</td>
</tr>";notice these values
Code: Select all
$data[username]Code: Select all
while ($row = mysql_fetch_assoc($result))
print "
<tr bgcolor=#9BD7FF>
<td>$row[username]</td>
<td>$row[email]</td>
<td>$row[access_level]</td>
</tr>";basically the command is saying "while you are fetching the new results and turned them into a $row variable print out a standard table row and insert the data into it accordingly"
what this will do is everytime it finds a result in the array it will print out a row and it will all look neat and tidy
as you become more and more able with PHP you will begin to use various commands such as else, if, else/if, while etc etc these are all control structure and should be viewed here
now for our last piece of code
Code: Select all
?>
</table>
</body>
</html>ok well that concludes this weeks tutorial, i am aware of cookies being a nightmare for beginners so i am planning on writing a tutorial on that subject soon (and yes i was also stumped at cookies)
If you have any problem go see my friend herehere
please feel free to annotate and to ask questions
Hope this helps someone out
and good luck with PHP everyone