Saturday, May 15, 2010
Login system
http://net.tutsplus.com/articles/news/how-to-build-a-login-system-for-a-simple-website/
The only problem is it doesn't finish- it doesn't show you how to register users or set an expiration date to the session. But it has enough for it to make sense to me and do the rest on my own.
Good luck!
Designing Databases
This week's prac was about databases. In working through the idea of a video store database, here's a possible solution:
| Id | Name | Address | Telephone No | Driver’s Licence No | Email address | Password |
| 0001 | Amber Jones | 23 Bla bla la | 01283471050 | 12893140509384 | secret |
| Id | Title | Copy No | Year | Genre | Actors | Release to shelf date | Current rental code | Current status |
| 0001 | The Negotiator | 1 | 2009 | Action | Joe Blow | 3.4.2010 | Newrelease | On shelf |
| Id | Rental Period (days) | Daily price within rental period | Daily price overdue | Auto assign at x weeks after release to shelf date |
| Newrelease | 1 | 5.00 | 5.00 | 0 |
| Weekly | 7 | 1.00 | 0.50 | 16 |
| Customer id | Title id | Date out | Date in |
| 0001 | 0001 | 6.4.10 | 7.4.10 |
Saturday, May 8, 2010
Tool for Wireframing
I'm using http://gomockingbird.com/
This is the type of thing you end up with:
Tuesday, May 4, 2010
MySQL
SELECT * FROM `books` WHERE LEFT(title, 1) = 'M' ORDER BY title LIMIT 0,3
This piece of code selects from the table called 'books' all titles that begin with M (where the left most character of the field 'title' is 'M'). It then orders the selection by title and limits it to three entries. The trick with this is that the ORDER BY needs to be done before the LIMIT- otherwise it doesn't work.
Just for a bit of fun, I had a play around with acually displaying the data on a site, and here's the code to do that:
<?php
$database="a4186211_wp";
mysql_connect ("mysql12.000webhost.com", "a4186211_tsr", "password");
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT name, tags, url FROM Resources" )
or die("SELECT Error: ".mysql_error());
$query="SELECT * FROM `books` WHERE LEFT(title, 1) = 'M' ORDER BY title LIMIT 0,3";
$result=mysql_query($query);
$num_rows = mysql_num_rows($result);
print "There are $num_rows records.<P>";
print "<table width=200 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
?>
Here's the link:
http://www.tracyr.comlu.com/Resources/new%202.php
Tuesday, April 20, 2010
JavaScript
http://tracyr.comlu.com/script/blog.html
It wasn't too difficult. Looking at the resources Alistair posted in UTS Online, I opened up the date file, and it suggested that to find a difference between two dates, do this:
timeA = new Date();
timeB = new Date( 'June 8 1982' );
timeDifference = timeA - timeB;
If 'new Date' is left blank, then it will assume today's date. The second date is my birthday- so this will calculate the difference between today's date and my birthday.
The only problem with this is that it's in milliseconds. In the same reference article for javascript date, it mentions there are 86 400 000 milliseconds in a day, so to get the number of days is pretty simple- take the time difference (which is now in milliseconds) and divide it by 86 400 000- which will give the number of days. To diplay this number in the document, I added the 'document.write'.
result= timeDifference/86400000;
document.write(result);
The last thing I did (mainly to check my maths since I wasn't convinced I was doing the right calculations!) was to divide the number of days I came up with by 365. This then converts the days into years. Now it's only approximate because it doesn't take into consideration leap years, but it was accurate enough to check by maths since I know how old I am!
To do this I added:
years = result/365;
And the final thing I did was to write this number to the document, but I wanted to write it so it was a whole number, without the decimal points, so I added in 'math.round' which rounds it to the nearest whole number- taken straight from w3schools.
document.write(Math.round(years) + "
");
So that was the JavaScript Prac!
Tuesday, April 13, 2010
PHP
"SQL PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only one primary key."
http://www.w3schools.com/sql/sql_primarykey.asp
My 000webhost members area had all the info to plug straight into the script directly under the instructions in quotation marks. The only one that got me stuck was the server- which was a different server from the one listed on the front page on the members area, I just needed to search the MySQL area for the database server.
Uploading:
No problems! Just used Filezilla and after rectifying the server issue, it all worked fine.
Adding a 'country' field:
Firstly, country needed to be added to the database table.
My strategy for this was simple- I scoured the document and every time I found 'name' 'email' and 'message' I added 'country' in the same syntax the others were in. First time round, I managed to get the JavaScript happening- new field and being able to type in the field, but wouldn't have 'country' emailed or appear at the top of the guest book. I went through it again, found that I had missed a couple of sections and did it again. Managed to get it to email, but not appear at the top of the page or add to the database. Finally found the part of the script that added it to the database and it all worked fine. I've hi-lighted all the changes I made to the script below.
__________________________________________________________________
::Guestbook - A::
dbc = mysql_connect($server, $user, $password) or die ("Connection failed!");
mysql_select_db($database) or die ("Database failed!");
}
/* DISPLAY RECORDS */
function display_records($offset, $entries_per_page)
{
$this->result = mysql_query("SELECT id, name, email, country, message, date FROM $this->table ORDER BY id DESC LIMIT $offset, $entries_per_page") or die ("Query failed!");
while ($row = mysql_fetch_array($this->result)) {
// SOME NICE FORMATTING HERE;
$display_name = nl2br(htmlspecialchars($row["name"]));
$display_email = nl2br(htmlspecialchars($row["email"]));
$display_country = nl2br(htmlspecialchars($row["country"]));
$display_message = nl2br(htmlspecialchars($row["message"]));
// THIS ALLOWS USING SMILIES AND IS NOT DANGEROUS;
$display_message = str_replace ("<img src=smiles/", "
// DISPLAY WHAT WE HAVE AT LAST;
echo "
Name: [" . $display_name . " ], Country: [" . $display_country . "], " . $row["date"] . "
" . "Email: " . $display_email . "
" . $display_message;
}
/******************************************************************************/
/* This code here handles pages stuff, number and next/previous links, etc. */
/* If you don't need some of the features, just delete corresponding parts. */
/******************************************************************************/
$this->count_result = mysql_query("SELECT count(id) AS number FROM $this->table") or die ("Query failed!");
while ($count = mysql_fetch_array($this->count_result)) {
$total_entries = $count["number"];
}
// HOW MANY PAGES OF RECORDS WE HAVE;
// THIS BLOCK IS ESSENTIAL FOR FURTHER PARTS;
$pages = $total_entries / $entries_per_page;
if ($pages < pages =" 1;"> 1) {
$pages = (int) $pages + 1;
}
else {
$pages = $pages;
}
if (($offset > $total_entries) or (!is_numeric($offset)))
$offset = 0;
// CURRENT PAGE NUMBER;
$pagenow = ($offset/$entries_per_page + 1);
echo "
* * *
Page " . $pagenow . " of " . $pages;
// NEXT/PREVIOUS PAGE LINKS DISPLAY
$next = $offset + $entries_per_page;
$previous = $offset - $entries_per_page;
if ($pages <> 1) {
echo " || ";
if ($previous < href="http://www.blogger.com/gb.php?offset=">";
echo ">>>";
}
elseif ($next >= $total_entries) {
echo "";
echo "<<<";
}
else {
echo "";
echo "<<<";
echo " | ";
echo "";
echo ">>>";
}
echo "
";
}
// DISPLAY LINKS TO ALL PAGES SEPARATELY;
echo "
$i = 0;
while ($i < $pages) { $ri = $i + 1; $showpage = $i * $entries_per_page; if ($ri == $pagenow) echo $ri . " "; else echo "" . $ri . " ";
$i++;
}
echo "
}
/******************************************************************************/
/* End of pages code, this section is the longest, but you get pages features */
/******************************************************************************/
/* ADD RECORDS TO DATABASE */
function add_record($name, $email, $country, $message, $smilies="on", $webmaster, $message_length, $language_filter, $bad_words)
{
if ($email == "") {
$email = "no_email";
}
// IF LANGUAGE FILTER IS ENABLED AND WEBMASTER EMAIL ADDRESS DEFINED DO THIS;
if (($language_filter == 1) and (strlen($webmaster) <> 0)) {
for ($i=0;$i
$message = substr($message, 0, $message_length);
}
// IF USER USES SMILIES DO THIS;
if ((isset($smilies)) and ($smilies == "on")) {
$format_smilies = array (
":-)", "
"8-)", "
":(", "
":-D", "
"%-)", "
">8-|", "
":-o", "
"?", "
":-(", "
"[$-)", "
":-P", "
";-)", "
);
for ($i=0;$i
// When guestbook is signed a message is emailed
// to webmaster if this feature is enabled;
if (strlen($webmaster) <> 0) {
$sendmessage = "Name: " . $name . "\nCountry: " . $country . "\nEmail: " . $email . "\nMessage: " . $message;
@mail($webmaster, "Guestbook signed", $sendmessage);
}
if (!$this->result)
echo "Error!";
}
/* DISCONNECT FROM DATABASE */
function disconnect_db()
{
mysql_close($this->dbc);
}
}
/******************************************************************************/
/* END OF GUESTBOOK CLASS */
/******************************************************************************/
/******************************************************************************/
/* INSTALLATION: */
/* 1) create a table in the MYSQL database with a query: */
/* CREATE TABLE guestbook ( */
/* id int(5) NOT NULL auto_increment, */
/* name varchar(50), */
/* email varchar(50), */
/* message text, */
/* date datetime, */
/* PRIMARY KEY (id) */
/* ) */
/* 2) define some variables below as they suit your environment; */
/* 3) possibly change any formatting in the display_records() function; */
/* 4) copy gb.php to your server and enjoy; */
/******************************************************************************/
// Let's define some variables;
$webmaster = 'ts_richardson@yahoo.com.au'; // EMAIL ADDRESS TO SEND WARNINGS TO
// WHEN GUESTBOOK IS SIGNED; LEAVE
// EMPTY IF YOU WANT THIS FEATURE
// DISABLED;
$server = 'mysql12.000webhost.com'; // DATABASE SERVER;
$database = 'a4186211_wp'; // DATABASE NAME;
$user = 'a4186211_tsr'; // USER TO CONNECT TO DATABASE;
$password = 'mypassword'; // USER PASSWORD;
$entries_per_page = 5; // HOW MANY RECORDS PER PAGE;
$message_length = 1024; // MESSAGE LENGTH ALLOWED, LEAVE 0
// IF YOU WANT ANY SIZE MESSAGES,
// THIS CUTS MESSAGE TO DEFINED SIZE;
$language_filter = 1; // 1 - enable language filter;
// 0 - disable language filter;
$bad_words = array ( // Bad words vocabulary (add your own);
'bottom', 'trousers'
);
// Let's spawn an instance of guestbook class;
$myGB = new Guestbook;
$myGB->connect_db($server, $database, $user, $password);
// aw- put the POST variables into the variables used in the script
$message = $_POST['message'];
$email = $_POST['email'];
$country = $_POST['country'];
$name = $_POST['name'];
$smilies = $_POST['smilies'];
// If user submitted form, add a record;
if (isset($message)) {
if (!isset($smilies))
$myGB->add_record($name, $email, $country, $message, "no", $webmaster, $message_length, $language_filter, $bad_words);
else
$myGB->add_record($name, $email, $country, $message, $smilies, $webmaster, $message_length, $language_filter, $bad_words);
}
// If opened without $offset variable defined, it is zero;
if ((!isset($offset)) or ($offset < offset =" 0;">display_records($offset, $entries_per_page);
$myGB->disconnect_db();
?>
* Name:
Email:
Country:
* Message:
|
|
|
Monday, April 12, 2010
Macro and Micro Analysis
There’s also the fact that by using HTML5 code right now your website gets stuck in some kind of “limbo” since even though your browser will render HTML5, it does not understand it as of yet. This may also apply to other software such as screenreaders and search engines.
Lastly you must consider that HTML5 is still under heavy development, and it’s probably the “most open” project the W3C has ever done. With the immense amount of feedback and all the hype around it, the current draft is bound to change and it’s impossible to predict how much."