Jump to content

Archived

This topic is now archived and is closed to further replies.

CanMan2004

Increasing numbers/letters

Recommended Posts

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

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

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

[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;
    }
    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>';
}
?>[/code]

Share this post


Link to post
Share on other sites
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
[code]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');[/code]

Here is the code
[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;
?>[/code]

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

Later

Ray

Share this post


Link to post
Share on other sites
[quote author=craygo link=topic=108486.msg436484#msg436484 date=1158586307]
I do not think there is a way to increase letters like you do with numbers.
[/quote]
Then read my post above yours

Share this post


Link to post
Share on other sites
[quote author=craygo link=topic=108486.msg436484#msg436484 date=1158586307]
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
[code]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');[/code]

Here is the code
[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;
?>[/code]

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

Later

Ray
[/quote]

Worked great craygo, thanks

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Here is my additon to Barands code:
[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";
}

?>[/code]
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
So the first part will stay static and the second will go to 9ZZ and end there???

Ray

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
This should work for any length, any format

[code]
<?php
function IncNextPos (&$id, $pos) {
    $min = is_numeric($id{$pos}) ? '1' : 'A';
    $max = is_numeric($id{$pos}) ? '9' : 'Z';
   
    if ($id{$pos}==$max) {
        $id{$pos} = $min;
        IncNextPos($id,$pos-1);
    }
    else {
        $x = $id{$pos};
        $x++;
        $id{$pos} = $x;
    } 
}

function calc_next ($id) {
    $pos_space = strpos($id, ' ');
    $b = str_replace(' ', '', $id);  // remove space
   
    IncNextPos($b,strlen($b)-1);
   
    return substr($b,0,$pos_space) . ' ' . substr($b, $pos_space);
}

$next = 'D2 9ZW';
for ($i=0; $i<10; $i++) {
    $next = calc_next($next);
    echo $next.'<br>';
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.