Retrieve valid entries for a MySQL ENUM/SET column

Here's a handy little PHP function I threw together to retrieve valid entries for a MySQL ENUM/SET column, handy for generating dynamic radio group/checkbox/multiple select HTML elements:

PHP:
  1. // grab possible SET/ENUM values and return an array
  2. function getPossibleValues($table,$field){
  3.  
  4.     $query = "SHOW COLUMNS FROM `$table` LIKE '$field'";
  5.     $result = mysql_query($query);
  6.    
  7.     if(mysql_num_rows($result)>0){
  8.         list(,$fields) = mysql_fetch_row($result);
  9.         $options = explode("','",preg_replace("/(enum|set)('(.+?)')/","\2",$fields));
  10.         return $options;
  11.     } else {
  12.         return array();
  13.         return false;
  14.     }
  15.  
  16. }

4 comments November 23rd, 2005

The Ubiquitous Bloke in the Pub

The ubiquitous bloke in the pub is the bloke who can get you anything. He's also a web designer. Or he knows a web designer. Rather, he knows someone whose brother is a web designer and can 'knock you up a website, no problems'.

Its always amazed me that no one would trust someone to 'knock you up a car' or 'get you a cracking deal on a new tv'. No, you'd tend to go to the experts to get a car or a new tv - especially as they know what they are talking about.

2 comments November 21st, 2005

Domains: The Good, The Bad, The Ugly…

Every site on the interweb needs a domain name to be functional. The question is, what constitutes a good domain name? Anyone looking to register a new domain name these days will be hard pressed to find one that satisfies the following:

  • Available for registration
  • Actually relates to the site content
  • Has a decent domain extension
  • Optional: contains company name

We've all tried to register a domain to be told its not free. Maybe my word power is low, but I can never locate the *perfect* domain name - of course, JellyandCustard.com is that exception to the rule!

Continue Reading 1 comment November 18th, 2005

More multiple submit buttons

Following on from Khalid's previous post on multiple submit buttons, its also worth nothing that you can have multiple submit buttons with the same name attribute, e.g:


So, once the form's been submitted, in PHP the $_POST['action'] var will be set to either Edit or Delete.

Add comment November 17th, 2005

Automated Income Streams

There's been quite an increase recently in online income stream sites. These are basically sites that are tailored to a certain subject, and alongside the content, a few targetted adverts are placed in the hope that you, the visitor, will click on them. A good site will get a good google ranking, and a fairly decent stream of visitors. Some of these visitors will inevitable click on your adverts, and you make the money for the clickthrough. That's money that you make for doing nothing apart from setup a site, and then some search engine optimisation/listings.

5 comments November 16th, 2005

One Form, Multiple Actions

Not everyone is aware of this fact, so I thought I'd share it. You can have a single form, with multiple submit buttons, and then determine which button was clicked as to what action to take. Take the following code:


If your user clicks on the "edit" button, then $_POST["edit"] will be available, and the same if they click on the "delete" button. You can test for these buttons within your PHP code, and then perform the related actions:

PHP:
  1. if(isset($_POST["edit"]) && trim($_POST["edit"])!=='') {
  2. //they clicked edit
  3. //perform INSERT SQL Query
  4. } elseif(isset($_POST["delete"]) && trim($_POST["delete"])!=='') {
  5. //they clicked delete
  6. //perform DELETE SQL Query
  7. } else {
  8. //nothing has been clicked
  9. //load initial form
  10. }

3 comments November 16th, 2005

Shiira

As an avid Apple user, I'm always on the lookout for interesting and useful apps.

I stumbled across Shiira the other day and was pleasantly suprised. Yes, it is *another* browser, but its exceptionally fast. Its based upon Web-Kit (the same foundations as Safari) and so has great compatibility, yet it appears to render pages a lot faster than Safari does...

Give it a shot.

1 comment November 15th, 2005

Slow SCP Speeds

When I reformatted my server and put Fedora Core 4 on, I had a marked decrease in transfer speeds. Before the format, i was getting about 3mb/sec, which although is slow over a 100mbit network, it was bearable. However, after the format, I had speeds on scp throttling at about 60Kb/sec!

The confusing thing was that transfers over http and ftp were going at about 9mb/sec, ruling out any hardware problems. After quite a bit of hunting around the internet, I stumbled upon an article that I've now misplaced the link for :(.

Anyhow, the fix is below.

Add comment November 15th, 2005

Running Oracle 10g XE & PHP on Linux

I decided to reformat my local webserver the other day. This server generally runs all my code on it before its published onto the web. I was using Fedora Core 4 Test 3, but decided to lose some stuff I've installed on there, and put a full release of FC4 on.

I settled on the following LAMP options:

I decided that I'd stick Oracle XE on as well, and have a play with that. Surely if MySQL is that simple to install then Oracle can't be that much harder! Or so I thought...

Continue Reading 1 comment November 15th, 2005

Count occurence of character (i.e. line breaks) in a MySQL field

Unfortunately, there is no inbuilt MySQL function to count the occurance of a character in string. By counting the number of characters in the original string, then temporarily 'deleting' the character you want to count and counting the string again, subtracting the first count from the second, you get the number of occurances:

SELECT LENGTH('foobarfoobarfoobar') - LENGTH(REPLACE('foobarfoobarfoobar', 'b', '')) AS `occurances`
--> 3

Obviously, in this example 'b' is the string you want to count the number of occurances of.

You can use the same principle to count the number of lines in a field, by adding one to the difference:

1 comment November 8th, 2005

Next Posts


Categories

Links

Calendar

November 2008
M T W T F S S
« Sep    
 12
3456789
10111213141516
17181920212223
24252627282930

Feeds