I have decided to open source my “Web Applications” coursework. It is a simple bloggging engine written in PHP and mySQL.
If you’re interested in having a look, then check out the git repo here: https://github.com/JosephRedfern/RedBlog/
I have decided to open source my “Web Applications” coursework. It is a simple bloggging engine written in PHP and mySQL.
If you’re interested in having a look, then check out the git repo here: https://github.com/JosephRedfern/RedBlog/
So, I’ve decided to try and publish more things to GitHub this year – starting with LogBox. LogBox will hopefully soon become a collection of scripts that can be used to monitor the performance of websites hosted on shared hosting servers. At the moment, LogBox contains only the one script, which goes by the name of “loadavg”. If you’ve every used a *nix machine before, you’ll probably know what the load average is – it is a measurement of the current CPU load, usually over the last 1, 5 and 15 minutes.
The loadavg script will check the load average of the server which it is being run on, and if the reported load averages cross a certain threshold, then a warning email will be sent to an email address of your choice. In addition to this, the current load averages will be logged to a MySQL Database.
The script is designed to be run as a cronjob (which most shared hosts support) – however, if cron is not available then it is possible to use “fakecron”, which is available here: http://quirm.net/2008/10/02/fake-cron/.
I have written this PHP function which will do the following:
This is the PHP Code:
[cc lang='php' ] $db_server = “127.0.0.1″; //MySQL Server
$db_user = “”; //MySQL Username
$db_password = “”; //MySQL Password
$db_name = “”; //MySQL Database
$api_key = “”; //TheEchoNest API Key
$apinresults = 5; //Number of API Results to store in database. For initial queries, a larger value will increase load time (considerably). Default = 5
$conn = mysql_connect($db_server, $db_user, $db_password) OR DIE(mysql_error()); //connect to DB, or print error
mysql_select_db($db_name, $conn) OR DIE(mysql_error()); //select database, or print error
function getAudio($artistname){
GLOBAL $api_key;
GLOBAL $apinresults;
$artist = mysql_real_escape_string($artistname); //help to prevent sql injection
$artist = str_replace(” “, “+”, $artist);
if($artist != “”){
$query = “SELECT url FROM audio WHERE artist = ‘”.$artist.”‘”; //sql query to check number of rows for that artist
$query = mysql_query($query); //query to check number of rows
if(mysql_num_rows($query) == 0){ //if number of rows is 0, download fresh data from API
$url = “http://developer.echonest.com/api/v4/artist/audio?api_key=”.$api_key.”&name=”.$artist.”&format=xml&results=”.$apinresults; //create API request URL
$xml = file_get_contents($url); //download XML document
$audios = new SimpleXMLElement($xml); //Parse XML Document
foreach($audios->audio->audio as $audio){ //go through XML Document, insert values into DB.
$query = “INSERT INTO `audio` (`aid` ,`title` ,`url` ,`artist` ,`date` ,`length` ,`link` ,`release` ,`id`)VALUES (NULL, ‘”.mysql_real_escape_string($audio->title).”‘,’”.mysql_real_escape_string($audio->url).”‘,’”.$artist.”‘,’”.mysql_real_escape_string($audio->date).”‘,’”.mysql_real_escape_string($audio->length).”‘,’”.mysql_real_escape_string($audio->link).”‘,’”.mysql_real_escape_string($audio->release).”‘,’”.mysql_real_escape_string($audio->id).”‘)”;
mysql_query($query); //insert data into db
}
}
//AFTER having fetched data from API (if no entries in db table), select the URL from the table, order by random, and select a single value.
$query = mysql_query(“SELECT `url` FROM `audio` WHERE `artist`=’”.$artist.”‘ ORDER BY RAND() LIMIT 1″);
$row = mysql_fetch_row($query);
echo $row[0]; //output URL to browser
}
}
?>[/cc]
I have tried to add as many comments as possible – but if there is anything you’d like to ask me about, then just add a comment.
This is the accompanying SQL file:
[cc lang='sql' ]CREATE TABLE IF NOT EXISTS `audio` (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`url` text NOT NULL,
`artist` text NOT NULL,
`date` text NOT NULL,
`length` text NOT NULL,
`link` text NOT NULL,
`release` text NOT NULL,
`id` text NOT NULL,
PRIMARY KEY (`aid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;[/cc]
will also be needed. You might notice that I have included all of the data within the XML file – this is purely for future use, and so that the uploader of the song can be credited.
The script can be called as so:
[cc lang='php']
[/cc]
You can download a zip of the files here
A couple of weeks ago, I began work on rCMS – content management system that I am coding. It is designed to be very lightweight, fast, and easy to use. At the moment, I have the core functionality sorted. It is coded in PHP and mySQL – I might look into ODBC, but I don’t think that the kind of website that rCMS would be used for would warrant it’s usage. But research shall be done!
I have not yet decided how expansion should be done. For instance – if as user wanted an image gallery, how should this be allowed? A plug-in system of some description will probably be required… I will need to give it quite some thought, though.
I will keep this blog updated with my progress!