Jump to content


Photo

Increasing numbers/letters


  • Please log in to reply
21 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 12:16 PM

Hi

I have a sql database with a bunch of mixed letters and numbers in it, for example

D2 3V
E3 4R
N8 2W

I then print one of these values on my page, for example

D2 3X

I print with

<? print $row['anumnber']; ?>

How can I get php to increase the letter and numbers in the sequence? For example, the next value after

D2 3X

would be

D2 3Y

then

D2 3Z

If a record is shown as

D2 3Z

with Z being the last letter, it would then increase the number before the Z and put Z back to A, so the next one would be

D2 4A

then

D2 4B

and so on.

Is this at all possible? I have tried increasing by using +1, but it seems to work only for numbers.

Can anyone help?

Thanks in advance

Ed

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 18 September 2006 - 01:10 PM

you could hold a-z in a array and 1-26 in another array and change between them to show what next letter is

so you select last letter into the value $var

$var=str_replace($letters,$numbers,$var);
$var++;
$var=str_replace($numbers,$letters,$var);

I think that would work but not 100%

Just an idea :)

regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 18 September 2006 - 01:11 PM

you'll probably need a custom function to do that...
using a for loop to check and evaluate each character form right to left

I'll help you out more when I get to school...

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#4 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 01:23 PM

thanks, help is appricated

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 September 2006 - 01:23 PM

I'd use substr() to get the last two characters (individually)...

$penultimate = substr($row['anumber'], -2, 1);
$last = substr($row['anumber'], -1, 1);

As to what you do with them after that, I'm not sure.  You could put all the letters into an array and refer to them via their index... So if you wanted the letter Z (26th letter of the alphabet) you could use $letter[25]; (remember that an array index starts at 0, not 1).

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 18 September 2006 - 01:28 PM

This only does the secon half of the string but you can extend the principal to the first part.

Basically, if $x = B, then $x++ gives C, however if $x = Z then $x++ gives AA

<?php
function calc_next ($id) {
    list ($a, $b) = explode(' ', $id);
    if ($b{1}=='Z') {
        $b{1} = 'A';
        $x = $b{0};
        $x++;
        $b{0} = $x;
    }
    else {
        $x = $b{1};
        $x++;
        $b{1} = $x;
    }
    return "$a $b";
}

$next = 'D2 3W';
for ($i=0; $i<10; $i++) {
    $next = calc_next($next);
    echo $next.'<br>';
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 September 2006 - 01:31 PM

I do not think there is a way to increase letters like you do with numbers. But there is a way you can to it with a database table or an array. I am going the table route cause I like it :)

here is the table
CREATE TABLE `letters` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `letter` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

-- 
-- Dumping data for table `letters`
-- 

INSERT INTO `letters` VALUES (1, 'A');
INSERT INTO `letters` VALUES (2, 'B');
INSERT INTO `letters` VALUES (3, 'C');
INSERT INTO `letters` VALUES (4, 'D');
INSERT INTO `letters` VALUES (5, 'E');
INSERT INTO `letters` VALUES (6, 'F');
INSERT INTO `letters` VALUES (7, 'G');
INSERT INTO `letters` VALUES (8, 'H');
INSERT INTO `letters` VALUES (9, 'I');
INSERT INTO `letters` VALUES (10, 'J');
INSERT INTO `letters` VALUES (11, 'K');
INSERT INTO `letters` VALUES (12, 'L');
INSERT INTO `letters` VALUES (13, 'M');
INSERT INTO `letters` VALUES (14, 'N');
INSERT INTO `letters` VALUES (15, 'O');
INSERT INTO `letters` VALUES (16, 'P');
INSERT INTO `letters` VALUES (17, 'Q');
INSERT INTO `letters` VALUES (18, 'R');
INSERT INTO `letters` VALUES (19, 'S');
INSERT INTO `letters` VALUES (20, 'T');
INSERT INTO `letters` VALUES (21, 'U');
INSERT INTO `letters` VALUES (22, 'V');
INSERT INTO `letters` VALUES (23, 'W');
INSERT INTO `letters` VALUES (24, 'X');
INSERT INTO `letters` VALUES (25, 'Y');
INSERT INTO `letters` VALUES (26, 'Z');

Here is the code
<?php
$string = "D2 3Z";
  $last = substr($string, 4, 1);
  $third = substr($string, 3, 1);

    $sql = "SELECT * FROM letters WHERE letter = '$last'";
      $res = mysql_query($sql) or die (mysql_error());
        $r = mysql_fetch_assoc($res);
          $newid = $r['id'] + 1;
          $newthird = $third;
          if($newid == 27){
            $newthird = $third + 1;
            $newid = 1;
          }
    $sql2 = "SELECT * FROM letters WHERE id = '$newid'";
      $res2 = mysql_query($sql2) or die (mysql_error());
        $row = mysql_fetch_assoc($res2);
        $last = $row['letter'];
$newstring = substr($string, 0, 3).$newthird.$last;
echo $newstring;
?>

you may like some of the ones above but this is what I came up with

Later

Ray

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 18 September 2006 - 02:02 PM

I do not think there is a way to increase letters like you do with numbers.

Then read my post above yours
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 02:27 PM

I do not think there is a way to increase letters like you do with numbers. But there is a way you can to it with a database table or an array. I am going the table route cause I like it :)

here is the table

CREATE TABLE `letters` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `letter` varchar(100) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;

-- 
-- Dumping data for table `letters`
-- 

INSERT INTO `letters` VALUES (1, 'A');
INSERT INTO `letters` VALUES (2, 'B');
INSERT INTO `letters` VALUES (3, 'C');
INSERT INTO `letters` VALUES (4, 'D');
INSERT INTO `letters` VALUES (5, 'E');
INSERT INTO `letters` VALUES (6, 'F');
INSERT INTO `letters` VALUES (7, 'G');
INSERT INTO `letters` VALUES (8, 'H');
INSERT INTO `letters` VALUES (9, 'I');
INSERT INTO `letters` VALUES (10, 'J');
INSERT INTO `letters` VALUES (11, 'K');
INSERT INTO `letters` VALUES (12, 'L');
INSERT INTO `letters` VALUES (13, 'M');
INSERT INTO `letters` VALUES (14, 'N');
INSERT INTO `letters` VALUES (15, 'O');
INSERT INTO `letters` VALUES (16, 'P');
INSERT INTO `letters` VALUES (17, 'Q');
INSERT INTO `letters` VALUES (18, 'R');
INSERT INTO `letters` VALUES (19, 'S');
INSERT INTO `letters` VALUES (20, 'T');
INSERT INTO `letters` VALUES (21, 'U');
INSERT INTO `letters` VALUES (22, 'V');
INSERT INTO `letters` VALUES (23, 'W');
INSERT INTO `letters` VALUES (24, 'X');
INSERT INTO `letters` VALUES (25, 'Y');
INSERT INTO `letters` VALUES (26, 'Z');

Here is the code
<?php
$string = "D2 3Z";
  $last = substr($string, 4, 1);
  $third = substr($string, 3, 1);

    $sql = "SELECT * FROM letters WHERE letter = '$last'";
      $res = mysql_query($sql) or die (mysql_error());
        $r = mysql_fetch_assoc($res);
          $newid = $r['id'] + 1;
          $newthird = $third;
          if($newid == 27){
            $newthird = $third + 1;
            $newid = 1;
          }
    $sql2 = "SELECT * FROM letters WHERE id = '$newid'";
      $res2 = mysql_query($sql2) or die (mysql_error());
        $row = mysql_fetch_assoc($res2);
        $last = $row['letter'];
$newstring = substr($string, 0, 3).$newthird.$last;
echo $newstring;
?>

you may like some of the ones above but this is what I came up with

Later

Ray


Worked great craygo, thanks

#10 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 September 2006 - 02:50 PM

I'd imagine there's a bigger processing overhead with the MySQL solution than with Barand's.

If that's a consideration then you might want to take another look at his suggestion.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#11 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 September 2006 - 03:29 PM

Sorry Burand I was writing it while you guys were posting it. then went back and edited stuff.

You the man, you know this!!!!

Ray

#12 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 05:52 PM

Hi Barand

I liked your example very much, the only problem is that it only works with 2 trailing numbers\letters, if I add a 3rd, it forgets about it and does not increase it?

On your example, it forgets about the end trailing W

Any help would be great

Thanks in advance

Dave

#13 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 September 2006 - 05:59 PM

You may want to give him examples of what your date CAN look like. Your original post did not say you would have 3 charactors at the end. If it can go even further then let us know. Give us ALL options so statements can be written.

Ray

#14 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 06:16 PM

Hi

Its either 2 or 3 at the end, 80% if them are 3 long

some examples are

3EF
7YH
2WE
6TG

Any help would be great

Thanks

Dave

#15 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 September 2006 - 06:23 PM

One last question before the pain begins :)

What do you want to do once the last 3 get to 9ZZ or whatever the last will be

Ray

#16 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 18 September 2006 - 06:24 PM

My thoughts exactly

Are first 2 always D2?

Or, when you get to D2 9ZZ is the next one D3 1AA?

Are there always 2 (number, letter) in the first part or is there yet another surprise in store?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#17 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 18 September 2006 - 06:26 PM

Here is my additon to Barands code:
<?php

function calc_next($id)
{
    list($a, $b) = explode(' ', $id);

    if ($b{1} == 'Z')
    {
        $b{1} = 'A';

        $x = $b{0};
        $x++;
        $b{0} = $x;
    }

    for ($i = 1; $i <= strlen($b)-1; $i++)
    {
        $x = $b{$i};
        $x++;
        $b{$i} = $x;
    }

    return "$a $b";
}

$next = 'D2 3WZ';
for ($i = 0; $i < 10; $i++)
{
    $next = calc_next($next);

    echo $next . "<br />\n";
}

?>
It'll, now change the last two letters.

EDIT: Last two posts must of been made when I changing the code. SO prehaps this code may need to reworked again.

#18 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 06:32 PM

hi

thanks for all the help so far.

The numbers end at 9ZZ.

To answer the other question, the first part can be 3 or 4 letters/numbers, for example a full example could be

DD99 4RT
ES12 1EG

and so on.

I didnt want to bug you guys too much about the first part, but I thought I would mention it anyway

#19 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 September 2006 - 06:34 PM

So the first part will stay static and the second will go to 9ZZ and end there???

Ray

#20 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 18 September 2006 - 06:46 PM

Yep, that's right, although im going to have to do one for the 3 or 4 letters/numbers at the start soon, any help on that would be great, but I understand ive taken much of your time up everyone.

Thanks again




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users