Archive

Archive for the ‘Tutorials’ Category

IRC Bot (part 2)

March 28th, 2009

Part 1

This lesson's goals

  • Set up a MySQL database
  • Check every message for incoming commands
  • Update commands dynamically

Yes, that's quite a challenge. Unlike the previous tutorial, I'd call this an average tutorial. Not too hard, but not truly easy either. Full tutorial after the jump.

Good, so you decided to follow up from the previous tutorial and make it do what you want? The bot will be controlled through the IRC window itself by sending it different queries.

Let's start by laying out a work plan, things we want it to do:

  • Add your own functions (!hai => Say something back; !slap => Slap someone, etc)
  • Track user stats (Words per line, #of lines, #of words, avg. #of characters per word, etc.)

That's what we'll be covering this tutorial. We'll keep the functions and user data stored in a MySQL database.
Read more...

Popularity: 100% [?]

PHP, Tutorials

Using MySQL to select data from multiple tables (Using JOIN)

March 24th, 2009

I got on the idea of writing a little post about this a while ago, but forgot about it. And just today I uncovered the note I had written to myself, to keep my promise to all of you, so here goes.

My situation was as follows: Imagine a store. Every product has a unique identification code (NASA-number), a product description and a price, to keep it very basic. Then, every product has a stock number (The amount of items currently in stock). We're keeping these values apart from eachother because keeping the amount of items in stock isn't enough. We also need to keep a log of everything that happens to the product (how many do we sell per day, how many are delivered to us, how much do we throw out).

So basically, we have two tables. One contains all product information, and the second keeps daily logs of what happens to each and every product. Let's give you a basic table layout:

CREATE TABLE `nasa` (
`nasa` INT(16) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`price` FLOAT NOT NULL,
`content` VARCHAR(255) NOT NULL,
UNIQUE KEY `nasa` (`nasa`)
);

This is the listing of the products, every single item has a unique NASA-number, names can overlap providing the content is different (think a litre of whole milk vs. a gallon of whole milk), than we have the log:

CREATE TABLE `history` (
`DAY` INT(16) NOT NULL,
`nasa` INT(16) NOT NULL,
`stock` INT(16) NOT NULL,
`received` INT(16) NOT NULL,
`sold` INT(16) NOT NULL,
`counted` INT(16) NOT NULL
);

So this one has a daily log. But if you want to display the data of these tables on your screen, you might be tempted to run two queries, one to fetch the name (for legibility, people aren't very likely to remember numbers), maybe the content and one to fetch the current stock (are we out? did we receive anything?). This would go as follows

SELECT name,content FROM nasa WHERE nasa=62334

and then

SELECT stock FROM history WHERE nasa=62334

What if you combined these two into a single query? First write down which things you need: name, content, sales and current stock. Then, lo and behold the power of the JOIN-statement.

SELECT name,content,stock,sold FROM nasa JOIN history ON nasa.nasa =
  history.nasa WHERE history.nasa = 62334 AND history.DAY = today

See that? You can just select the four columns you need. MySQL will retrieve any row where the history.nasa matches the nasa.nasa so be sure to specify which row you want to retrieve. In this case you could also order descending by the date and limit the rows retrieved to 1:

SELECT name,content,stock,sold FROM nasa JOIN history ON nasa.nasa =
  history.nasa WHERE history.nasa = 62334 ORDER BY history.DAY DESC LIMIT 1

Thanks for reading, hope it makes any sense :))

P.S. In case you were wondering, 62334 is an actual NASA number used in Albert Heijn. It's the code for fresh cod filet.

Popularity: 43% [?]

Web development

WordPress Theme Generator

July 20th, 2008

You'll never guess what I've made now! :).

A theme generator, which takes in regular templates (all HTML), and turns them into a 'basic' template, with a widget-ready sidebar, archive support, comments, and what not!

Click

Please let me know what you think in the comments section. And, please; Do note that this is a beta version, hmm let's make up a number. Let's call it version Oh-point-six ?

Thanks for reading.

Popularity: 39% [?]

Miscellaneous, Web development , , , , ,

CSS Hyperlink Tooltips

July 19th, 2008

Ever wanted nicely styled CSS tooltips?

Here's a quick explanation:

 
<a href="#">Link text<span>Your tooltip</span></a>

Use that HTML. Then add the following to your stylesheet:

 
a { position: relative; outline: none; }
a span { display: none; }
a:hover span {
display: block;
font-size: 8pt;
position: absolute;
top:-25px; left: -45px;
border:1px solid #383a47;
background-color:#5cbbe3;
color: #383a47;
text-align: center;
padding: 1px 5px;
width: 130px; }

This code hides the span tag when you're not hovering over it. (a span { display: none; }) and positions it nicely when you DO hover over it.
Read on for an example (just a single hyperlink, but it's not valid xhtml so I put it on a different page)
Read more...

Popularity: 24% [?]

Web development

Using clear in CSS

July 15th, 2008

If you ever use floats, and then tried to add content to a container that's not floated, you may have experienced the same issues that many have. The solution is far simpler than expected

 
.clear { clear: both; }

This forces any item with a class of clear to be clear of floats on the left and right, which are - by chance - the other two options to clear; left and right. So after floating elements left and right, you should add a to force content below.

Popularity: 13% [?]

Web development