Archive

Posts Tagged ‘numbers’

MySQL Procedure To Get Incremental Numbers

March 9th, 2009 No comments

Run the following table definition to create a table called tests in the test database.

DROP TABLE IF EXISTS test.tests;
CREATE TABLE test.tests (
 id int(10) unsigned NOT NULL auto_increment,
 var varchar(45) NOT NULL,
 PRIMARY KEY (id)
);

Fill it with some default values:

INSERT INTO tests(var) VALUES('one'), ('two'), ('three'), ('four');

Now lets say that you want to find the values in the table, but that you want to increment a value for each row, the following procedure will do this. If you are using the MySQL Query browser then open a script table and past the following and execute, changing any database/table definitions.

DELIMITER $$
 
DROP PROCEDURE IF EXISTS `test`.`Increment` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Increment`( number CHAR(64) )
BEGIN
 
SET @total=0;
SELECT id, @total:=@total+number AS RunningSum FROM tests;
 
END $$
 
DELIMITER ;

You will now be able to call the procedure in the following way:

CALL Increment(4);

The value that you pass to the procedure is used to work out the increment. So in this case the output would be something like the following:

id  RunningSum
1   4
2   8
3   12
4   16

Generate Password Function In PHP

November 17th, 2008 No comments

I have talked about generating random passwords before. Although that function generated some nice passwords, they perhaps aren’t as unique as they ought to be.

This function, take from Webtoolkit creates passwords of different length with varying levels of complexity.

function generatePassword($length=9, $strength=0) {
  $vowels = 'aeiu';
  $consonants = 'bdghjmnpqrstvxyz';
  if ( $strength & 1 ) {
    $consonants .= 'BDGHJLMNPQRSTVWXYZ';
  }
  if ( $strength & 2 ) {
    $vowels .= 'AEU';
  }
  if ( $strength & 4 ) {
    $consonants .= '23456789';
  }
  if ( $strength & 8 ) {
    $consonants .= '@#$%';
  }
 
  $password = '';
  $alt = time() % 2;
  for ($i = 0; $i < $length; $i++) {
    if ($alt == 1) {
      $password .= $consonants[(rand() % strlen($consonants))];
      $alt = 0;
    } else {
      $password .= $vowels[(rand() % strlen($vowels))];
      $alt = 1;
    }
  }
  return $password;
}

The first parameter is the number of characters that the function should return. The second parameter is a number up to 8 which converts into complexity. The least complex password consists of only lower case consonants. The most complex password consists of upper and lower case letters,

You might notice that some of the letters and numbers are missing, this is deliberate. When passwords are generated many of the characters can be very similar. Zero can look like an upper case O and I can look like the number one or a lower case L. Removing these letters stops people getting their passwords wrong and having to reapply for them in the future. Also, many people write down their passwords, even though you shouldn’t, and in doing this many characters can also look the same. For example, in a mix of upper and lowercase letters it is difficult to see the difference between an upper and lower case W.

You can run the function like this.

echo generatePassword(8,1); // LareSuSy
echo generatePassword(8,2); // hUsuserU
echo generatePassword(8,3); // MEdEgYze
echo generatePassword(8,4); // tanapa3a
echo generatePassword(8,5); // ary2ugeR
echo generatePassword(8,6); // uqUtebyq
echo generatePassword(8,7); // yRysuNEV
echo generatePassword(8,8); // ygyqyha%

This is just the output that I got from these parameters, a different password is run each time. You should be using level 8 for any system administrator passwords.

Adding Numbers In JavaScript

July 28th, 2008 No comments

You think I’m joking right? Well due to a silly mistake when creating the language the concatenation character is the plus symbol. The same symbol used when you are adding numbers together and so sometimes JavaScript will add them together and sometimes it will concatenate them.

This occurs if JavaScript encounters any part of the calculation to be a string. If it is then it will concatenate it. For example.

alert("1"+2+3);// prints out "123" rather than 6

To stop this you need to add the parseInt() function to the part of the addition that might be mistaken as a string. Or the whole thing just to make sure. The following is a bit of an overkill but ensures that the values will be added, NOT concatenated.

alert(parseInt(1)+parseInt(2)+parseInt(3));

It is especially important to do this if you get any values from any form fields. This is because they are passed to JavaScript as strings so you will need to parseInt() the values to work with them.

Odd and Even Numbers in PHP

December 25th, 2007 No comments

To find if a number is odd or even you can use one of two operators.

The modulo operator (% in PHP) can be used to calculate the remainder of the value divided by 2. This gives a value of 0 for even numbers and a value of 1 for odd numbers. This can be used in an if statement as 0 will equate to false and 1 will equate to true.

$value = 10;
if($value%2){
  echo '$value is odd';
}else{
  echo '$value is even';
}

The second method is to use the & (AND) operator with the number 1. This will perform a bitwise calculation on the number and 1, returning 0 if the number is even and 1 if the number is false. So using the same if statement logic as before we can write.

$value = 10;
if($value&1){
  echo '$value is odd';
}else{
  echo '$value is even';
}

This method gives better performance than using modulo as it uses bitwise calculations that are native to computers.

Finally, you can use the is_long() function to see if the value you return from the division of the number by 2 has a remainder. This is a slightly longer (and more resource intensive) way of doing it than using modulo, but it works.

var_dump(is_long(4/2));
// prints bool(true)
var_dump(is_long(3/2));
// prints bool(false)

Categories: PHP Tags: , , , , , , ,