Advanced maxlength JavaScript

function getlength(id)
{
	return document.getElementById(id).value.length;
}

function getcurrenttxt(id, max)
{
	if(getlength(id) >= max)
		return document.getElementById(id).value.substr(0, max);
}

function usertyping(id, max, notice)
{
	var ID = document.getElementById(id);
	var Notice = document.getElementById(notice);
	if(ID && Notice)
	{
		Notice.innerHTML = '<span>' + (getlength(id) == (max+1) ? max : getlength(id)) + '</span>/' + max;
		if(getlength(id) >= max) {
			ID.value = getcurrenttxt(id, max);
		}
	}
}

What this does is, it checks the character length in a textarea (or whatever you want), and if that length exceeds max, it won’t let the user type anymore (it actually does something else, but that’s the effect). Also, it lists the character limit of the textarea that specify and also the max. It’s fairly simple to just list the amount the user has left,

function usertyping(id, max, notice)
{
	var ID = document.getElementById(id);
	var Notice = document.getElementById(notice);
	if(ID && Notice)
	{
		Notice.innerHTML = '' + (max - getlength(id)) + '';
		if(getlength(id) >= max) {
			ID.value = getcurrenttxt(id, max);
		}
	}
}

Of course, I prefer the first method.

If you like the script to onload (load when the page does), then here’s a function I wrote just for that:

function body_onload()
{
	var limits = new Array();
	limits[0] = new Array('bm_text', 255, 'char_limit');

	for(var i = 0; i < limits.length; i++)
		usertyping(limits[i][0], limits[i][1], limits[i][2]);
}

Just list the textareas and the information in the limits array, and then call it when the body loads. Of course, even if the specified textareas don’t exist, it won’t cause trouble - the script checks if the appropriate elements exist before doing anything.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

November 12th, 2008 | Leave a Comment

IIS is evil

Right, so I’m trying to install Apache so I can work on some PHP stuff. It seems I discovered a zombie while I was trying to get it to work. I kept typing http://localhost/, but instead of the “success” page that Apache should give, I get this other page, back when I was using Microsoft’s IIS (Internet Information Services). So, I went back into the IIS folder, which should have been deleted when I uninstalled IIS, and deleted all of those files. When I refreshed the page, I got this:

Honestly. What a stubborn software. It seems that even though I uninstalled, it’s still mysteriously in working condition. Anyone know how to get rid of IIS? Or, how to make Apache the default server?

There’s also something else that I find weird: when I Googled how to uninstall IIS, I got all these web pages about to install it!

Argh!!! Fucking useless!

October 9th, 2008 | Leave a Comment

Delicious!….hmmmm, yes!!!

This video explains what the title means. Anyway, one of the things I hate about PHP is how redundant the MySQL statements are. Each one is the same thing, but different only by a little bit. Experiencing the same thing? Well, this function I just wrote will help lighten the redundancy!

<?php

function add_mysql_entry($info)
{
if(is_array($info) and sizeof($info) > 0)
{
// Python: mysql_values,mysql_tables = {},{}
$mysql_values = array();
$mysql_tables = array();
if(is_array($info['post']))
{
foreach($info['post'] as $post)
{
// You need single quotes, or else it’s a suntax error.
$mysql_values[] = “‘”.htmlspecialchars($_POST[$post[0]]).”‘”;
$mysql_tables[] = $post[1];
}
}
if(is_array($info['background'])) {
foreach($info['background'] as $background_info)
{
$mysql_values[] = “‘”.$background_info['value'].”‘”;
$mysql_tables[] = $background_info['column'];
}
}
$mysql_values = implode(’,', $mysql_values);
$mysql_tables = implode(’,', $mysql_tables);
$query = mysql_query(”
INSERT
INTO “.$info['table'].”(”.$mysql_tables.”)
VALUES(”.$mysql_values.”)”) or die(mysql_error());
}
}

?>

This function is basically a simple MySQL INSERT statement. It makes it so that you don’t have to declare each $_POST variable, preform all precautions, execute the query, and then make sure that the query did what you wanted it to do. Here’s an example:

<?php

$info = array(
'table' => 'shoes_tb',
'post' => array(
array('add_shoes_name', 'shoe_name'),
array('add_shoes_desc', 'shoe_desc'),
array('add_shoes_price', 'shoe_price')
),
'background' => array(
array('value' => date("d/m/Y"), 'column' => 'shoe_date_added')
)
);
$add_shoe = add_mysql_entry($info);

?>

In this example, there are three $_POST variables that the programmer wants to use: add_shoes_name, add_shoes_desc, and add_shoes_price. He wants insert them in the columns shoe_name, shoe_desc, and shoe_price in the table shoes_tb. Of course, getting something such as the date and not letting the user see it can’t be done through a $_POST variable (other than using the hidden input type). This is where background comes in. The syntax of the $info array is:

<?php

$info = array(
'table' => the MySQL table's name,
'post' => array(
array(input_field_1, column_name_1),
array(input_field_2, column_name_2),
...
array(input_field_n, column_name_n)
),
'background' => array(
array('value' => value_1, 'column' => column_name_1),
array('value' => value_2, 'column' => column_name_2),
...
array('value' => value_n, 'column' => column_name_n)
)
);

?>

All in all, this is function is delicious.

September 1st, 2008 | 2 Comments

Changing printers in Windows Vista

Today, I had to actually change printers - that usually happens around every time one’s own printer type is outdated by a new type of printer. The reason why I had to change printers was because I had needed to have a ASCII reference table. You can get one here.

The process of changing printers is very simple. The most obvious pars would be plugging the printer into an electrical socket and then establishing a connection between your computer and the printer (wireless or wired, it will usually be wired (USB wire)). Yes, but for some reason Windows Vista doesn’t update the printers. I mean, it’ll know when you disconnect a speaker, mouse, keyboard or screen, but for some stupid reason it won’t know when the printer is disconnected - either that, or doesn’t care. First of all, once it’s physically connected, open the Control Panel (Start -> Control Panel). Now, I’m going to write this in list form to make it easier to read:

  1. Once you are in the Control Panel, click Hardware and Sound. It should be the picture of a printer.
  2. After you are in Hardware and Sound, click Printers and a window with a list of printers will appear.
  3. Now, do you know the name of your printer? My new printer is the HP Officejet 5610 series; therefore, I would double-click on the text that says HP Officejet 5600 series. The name of your printer and one displayed on your screen may not match up, like mine. I’m not sure why this, but Windows doesn’t seem to care. What you basically need to do is, double-click on the shortcut that has the name of the printer you want to use. If there’s more than one of those shortcuts, try each one until you get the one that works.

To test, print something. If it comes out of the printer that you want it to come out of, then mission accomplished. If not, then… post a comment.

Here are pictures if you are a visual learner:

September 1st, 2008 | 1 Comment

One thing you should NOT do with PHP

I know how dangerous it is, but it’s out of habit. What am I talking about? Well, it’s probably one of the most common mistakes a PHP developer/programmer/user will find: an array in a loop. It happens to me all the time, no matter what I’m working with, let it PHP, Python or JavaScript. This is just a reminder to me and anyone who faces this problem: never build an array in a loop! The array will just overwrite itself, and it can be a very well hidden problem. This includes loops and nested loops (loops in loops)… unless you want the array to overwritten. There are instances where the array needs to be overwritten (the mysql_fetch_assoc function is a good example).

This whole post may sound silly, but I suggest you be careful with array building in loops! it won’t cause any major problems (with exceptions), but you will never get the result you want (unless you want to the array to be overwritten, like I said before).

Drieick

August 18th, 2008 | Leave a Comment

Powered by WordPress | Blue Weed by Blog Oh! Blog | Entries (RSS) and Comments (RSS).