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."
Wednesday, April 7, 2010
CSS and Positioning
1. Class vs ID
"The difference between ID and class is that an ID selector can be called only once in a document, while a class selector can be called multiple times in a document. The second difference is that ID can be called by Javascript's getElementByID function.
There is no hard rule on when to use ID and when to use Class. My suggestion is to use class as much as possible for maximum flexibility, with the only exception being when you want to use Javascript's getElementByID function, in which case you need use ID.
Class and ID names are both case sensitive. For example, .classone and .ClassOne are two different classes."
Reference and more info:
http://www.1keydata.com/css-tutorial/class-id.php
2.Positioning
It's taken me a while to get my head around the different forms of positioning. Here's a website I think demonstrates it pretty well:
http://www.barelyfitz.com/screencast/html-training/css/positioning/
I also had a go at the challenge css prac from the lecture, and I'm told my solution is a little unconventional, so feel free to have a look at it here:
Thursday, April 1, 2010
CSS and design
I really loved this site Viveka shared on Tuesday http://www.csszengarden.com/, it gave me lots of ideas for using CSS in my own site. I've been working on designs this week, and have come up with the raw beginnings of two to choose between-wondering if you can all help? Please check these out and tell me which one you like best. Cheers!

