notbanksy's blog

Web Design, Linux and other nonsense

Mootools 1.2 and MenuMatic in Joomla 1.5

So, you’re using Joomla and you want to include a decent, accessible vertical flyout menu such as MenuMatic (which is fantastic by the way)? If that describes you, then I have 2 pieces of advice for you:

  • Stop using Joomla – it’s horrible.
  • Read this article, it will tell you how to do it.

The main obstacle to using MenuMatic in Joomla is that it doesn’t work! The reason for this is because Joomla includes Mootools version 1.1 and MenuMatic requires Moo version 1.2. You’ve probably guessed that they’re not compatible.

Now what really irritates me, is the fact that Joomla only makes use of mootools in the administrative area, yet the developers have seen fit include the mootools library in the front end also…. So the task at hand is to keep Mootools version 1.1 for the back end (trust me, it’s totally unusable without it), but switching to Mootools version 1.2 for the front end.

Read the rest of this entry »

Filed under: joomla, php

Php MySQL Random Quotes

Here’s a quick tutorial for implementing a random quote on your site using php/ mysql.  It’s not the most imaginitive script ever, but it’s a sound bit of learning for php noobs, and does have practical applications. It can be used for testimonials, or anything at all (ok, that’s a bit of an excessive claim, but it’s a bit useful). The techniques covered will definitely come in useful at some point in your programming career.
It may seem counter intuitive to use a database to randomise a few quotations, but I’m going to assume scalability is the issue at hand here, and that you’re going to end up with hundreds of quotes. Whatever justifies it will do! Besides, I’ll show you a quick php only script at the end for those who think a database is too much trouble.
First we’ll need a database called quote. Then you will need to set it up to take the entries like this:
<?php
//connect to mysql
mysql_connect(“localhost”,”yourusername”,”yourpassword”)or die(mysql_error());
//connect to database
mysql_select_db(“quote”)or die(mysql_error());
mysql_query(“CREATE TABLE quotes(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
quotation TEXT,
author VARCHAR(165))”)
or die(mysql_error());
echo”table created!”;
?>
Easy!  Feel free to mess with the paramaters there – some of you may not want to allow for massive quotations, so you may decide to use VARCHAR for quotation too. All depends on the verbosity of your sources!
So your database is set up, and your positively bursting with literary intent? Grand!  Next part is to create a form to input the quotations, here it is, and it’s (almost) good old HTML :) We’ll call it add_quote.php
<form action=”<?php echo $_SERVER['PHP_SELF']; ?>” method=”post”>
Quote<input type=”text” name=”quote”>
Author<input type=”text” name=”author”>
<input type=”submit” value=”add new quote”>
</form>
You’ve probably seen <?php echo $_SERVER['PHP_SELF']; ?> before? If not, it is just a way of telling the form that it should be processed by the document it lives in – by it’s SELF in other words. Ok, so we have a field to input the quote, and author – now we need to capture the data and squirt it into the database. Don’t forget to connect again, too:
//connect to mysql
mysql_connect(“localhost”,”yourusername”,”yourpassword”)or die(mysql_error());
//connect to database
mysql_select_db(“quote”)or die(mysql_error());
if ($_POST) {
$quote=$_POST['quote'];
$author=$_POST['author'];
//create new db entry
mysql_query(“INSERT INTO quotes(quotation,author) VALUES(‘$quote’,'$author’)”)or die(mysql_error());
}
Notice the if statement there? It prevents the form from capturing any data unless anything has been POSTed using the submit button. Next we assign the data to variables imaginitively called $quote and $author.  The next bit I hope is self explanatory.
Finally on this page, we should show which quotes we have added already – so that we don’t accidentally duplicate any.
//get quotes from database
$query=”SELECT * FROM quotes”;
$result=mysql_query($query)or die(mysql_error());
//list quotes
while($row=mysql_fetch_array($result))
{
echo”<div><p>”.$row['quotation'].” ~ “.$row['author'].”</p></div>”;
}
Again, self explanatory stuff so simple even an army of monkeys could reproduce it!  We SELECT everything (* means everything in SQL) FROM our table (quotes) and assign the output to a variable: $result. Then for each row ($row) in the results (mysql_fetch_array($result)) we output the quote and author ($row['quotation'] & $row['author']) and some appropriate HTML.
Job done mate – and for convenience I would put a link to your random quote page under your form to check it works:
<a href=”random_quote.php”>See a random quote!</a>
You’re nearly done – the next page random_quote.php uses some code we’ve already written but with important differences, so I’ll hold your hand till we reach the other side. Step one connect to the database:
<?php
//connect to mysql
mysql_connect(“localhost”,”yourusername”,”yourpassword”)or die(mysql_error());
//connect to database
mysql_select_db(“quote”)or die(mysql_error());
You’ll be a dab hand at connecting to mysql by now! The next part of the process is to choose a random quote, but in order to do so, we need to know how many quotes are avaiable. Here’s how we count them:
//count number of quotes in database
$num_quotes=”SELECT COUNT(*) FROM quotes”;
$count=mysql_query($num_quotes)or die(mysql_error());
This should be pretty simple to understand – the query asks simply to COUNT every enrty FROM the table quotes, then assign the result to $count. The weird this is, that $count doesn’t come back as a number on its own – if you were to echo it now, you’d get something like resource id=3. Weird, huh? So this next bit of code strips away the unnecessary and just leaves the number of quotes on its own.
//format the count
$count = floatval($count);
Now we have the actual number of quotes assigned to our var, we can choose one at random – a number between 1 and the total number of quotes.
//choose a random quote
$num = Rand (1,$count);
Now pull that quote out of the database on its own. Each time a quote is added, it is given an id number which mysql automatically assigns (check back to AUTO_INCREMENT when we created the table). So by finding the id field with the corresponding random number we have located our random quote!
//get chosen quote from database
$query=”SELECT * FROM quotes WHERE id=$num”;
$result=mysql_query($query)or die(mysql_error());
Now, print it on the screen already!
//display quote
while($row=mysql_fetch_array($result))
{
echo”<div><p>”.$row['quotation'].” ~ “.$row['author'].”</p></div>”;
}
?>
And you’re done! Easy, wasn’t it?  Here’s the two files in full for all you copy and pasters out there.
add_quote.php
<form action=”<?php echo $_SERVER['PHP_SELF']; ?>” method=”post”>
Quote<input type=”text” name=”quote”>
Author<input type=”text” name=”author”>
<input type=”submit” value=”add new quote”>
</form>
<a href=”random_quote.php”>See a random quote!</a>
<?php
//connect to mysql
mysql_connect(“localhost”,”root”,”harlequin”)or die(mysql_error());
//connect to database
mysql_select_db(“quote”)or die(mysql_error());
if ($_POST) {
$quote=$_POST['quote'];
$author=$_POST['author'];
//create new db entry
mysql_query(“INSERT INTO quotes(quotation,author) VALUES(‘$quote’,'$author’)”)or die(mysql_error());
}
//get quotes from database
$query=”SELECT * FROM quotes”;
$result=mysql_query($query)or die(mysql_error());
//list quotes
while($row=mysql_fetch_array($result))
{
echo”<div><p>”.$row['quotation'].” ~ “.$row['author'].”</p></div>”;
}
?>
random_quote.php
<?php
//connect to mysql
mysql_connect(“localhost”,”root”,”harlequin”)or die(mysql_error());
//connect to database
mysql_select_db(“quote”)or die(mysql_error());
//count number of quotes in database
$num_quotes=”SELECT COUNT(*) FROM quotes”;
$count=mysql_query($num_quotes)or die(mysql_error());
//choose a random quote
$count = floatval($count);
$num = Rand (1,$count);
//get chosen quote from database
$query=”SELECT * FROM quotes WHERE id=$num”;
$result=mysql_query($query)or die(mysql_error());
//display quote
while($row=mysql_fetch_array($result))
{
echo”<div><p>”.$row['quotation'].” ~ “.$row['author'].”</p></div>”;
}
?>Here’s a quick tutorial for implementing a random quote on your site using php/ mysql.  It’s not the most imaginitive script ever, but it’s a sound bit of learning for php noobs, and does have practical applications. It can be used for testimonials, or anything at all (ok, that’s a bit of an excessive claim, but it’s a bit useful). The techniques covered will definitely come in useful at some point in your programming career.

It may seem counter intuitive to use a database to randomise a few quotations, but I’m going to assume scalability is the issue at hand here, and that you’re going to end up with hundreds of quotes. Whatever justifies it will do! You can see this script in action here.

First we’ll need a database called quote. Then you will need to set it up to take the entries like this:

<?php

//connect to mysql
mysql_connect(“localhost”,”yourusername”,”yourpassword”)or die(mysql_error());

//connect to database
mysql_select_db(“quote”)or die(mysql_error());

Read the rest of this entry »

Filed under: php

Php MySQL gig listing script tutorial part 1

After much fannying around and general breaking of things, I finally have produced a fully operational gig listing script for which I offer this humble tutorial. Hope you like it.

You may want to glance over the guestbook tutorial as much of the code for the secure login is reused for this script. I’m going to list the code again at the end of this tutorial, but if you want an explanation of how it works, you should head over to the guestbook tutorial.

First off a couple of things must be considered. This is not an all bells and whistles script. For example, this script will not detect if you enter a gig listing which is in the past, it anticipates a modicum of common sense – a feature all too lacking in modern web programming in my opinion! Also, it presupposes that you will not be booking gigs any longer in advance than max 2 years, min 1 year. If you’ve ever worked as a musician, you’ll realise that this is more than adequete. And for all those seasoned musos turned web programmers out there, yes there is a gig cancellation feature!
Read the rest of this entry »

Filed under: php , , ,

Php & MySQL Guestbook tutorial – Part 2

Welcome to part 2 of the guestbook tutorial. So, in part one, you learned how to add messages to the guestbook, and how to display them on the web page. Good stuff. In this part you’re going to learn how to create a password protected login area to delete those nasty spam attacks and perform other censorship!

Remember in part one we created a table in your database to hold the username and password of the administrator? Well, first things first that wants populating. For the sake of simplicity I’m only going to show you how to add a single username/ password, but you can have as many as you want!

Ok, so my example username will be TonyBlair and the password will be LiedToMe, as these are not only memorable examples, but also true. Here’s how we’re going to get this information into the database:

Read the rest of this entry »

Filed under: php , , ,

Php & MySQL Guestbook tutorial – Part 1

A few weeks ago, Reg asked me to add a guestbook to his site. ‘Sure’, I said, ‘no problem’, and began scouring the web for a nice, clean, simple, XHTML strict guestbook script. Could I find one? There was more chance of me finding the source code for vista (that’s another story though!)

So I quickly realised that the only way Reg was going to have a guestbook on his site was if I wrote it. So I did. And here’s a quick tutorial on the whole process.

You should equip yourself thus:

  • local server – I recommend xampp
  • text editor (do not use MS word, works, openoffice etc)
  • browser – the one you’re using will do fine.
  • an extremely large mug of tea or coffee.

… if you’re using lynx you might want to try a GUI at some point!

Read the rest of this entry »

Filed under: php , , ,

What’s notmrsbanksy up to?

Oh man! She's gone and set herself up as a dog rescue! Mostly saving pound dogs (UK) from being put down. Once Loved Dog Rescue.


She also lists UK pound dogs on her site Pound dog rescue link.


Boson has a new home!

Boson has a new home

WOOF!