Jump to content

Increasing numbers/letters


CanMan2004

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.