CanMan2004 Posted September 18, 2006 Share Posted September 18, 2006 HiI have a sql database with a bunch of mixed letters and numbers in it, for exampleD2 3VE3 4RN8 2WI then print one of these values on my page, for exampleD2 3XI 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 afterD2 3Xwould beD2 3YthenD2 3ZIf a record is shown asD2 3Zwith 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 beD2 4AthenD2 4Band 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 advanceEd Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/ Share on other sites More sharing options...
shocker-z Posted September 18, 2006 Share Posted September 18, 2006 you could hold a-z in a array and 1-26 in another array and change between them to show what next letter isso 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 :)regardsLiam Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93912 Share on other sites More sharing options...
Zane Posted September 18, 2006 Share Posted September 18, 2006 you'll probably need a custom function to do that...using a for loop to check and evaluate each character form right to leftI'll help you out more when I get to school... Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93913 Share on other sites More sharing options...
CanMan2004 Posted September 18, 2006 Author Share Posted September 18, 2006 thanks, help is appricated Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93919 Share on other sites More sharing options...
HuggieBear Posted September 18, 2006 Share Posted September 18, 2006 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).RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93920 Share on other sites More sharing options...
Barand Posted September 18, 2006 Share Posted September 18, 2006 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]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93928 Share on other sites More sharing options...
craygo Posted September 18, 2006 Share Posted September 18, 2006 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 withLaterRay Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93931 Share on other sites More sharing options...
Barand Posted September 18, 2006 Share Posted September 18, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93954 Share on other sites More sharing options...
CanMan2004 Posted September 18, 2006 Author Share Posted September 18, 2006 [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 withLaterRay[/quote]Worked great craygo, thanks Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93981 Share on other sites More sharing options...
HuggieBear Posted September 18, 2006 Share Posted September 18, 2006 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.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-93986 Share on other sites More sharing options...
craygo Posted September 18, 2006 Share Posted September 18, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94015 Share on other sites More sharing options...
CanMan2004 Posted September 18, 2006 Author Share Posted September 18, 2006 Hi BarandI 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 WAny help would be greatThanks in advanceDave Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94096 Share on other sites More sharing options...
craygo Posted September 18, 2006 Share Posted September 18, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94100 Share on other sites More sharing options...
CanMan2004 Posted September 18, 2006 Author Share Posted September 18, 2006 HiIts either 2 or 3 at the end, 80% if them are 3 longsome examples are3EF7YH2WE6TGAny help would be greatThanksDave Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94117 Share on other sites More sharing options...
craygo Posted September 18, 2006 Share Posted September 18, 2006 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 beRay Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94121 Share on other sites More sharing options...
Barand Posted September 18, 2006 Share Posted September 18, 2006 My thoughts exactlyAre 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? Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94125 Share on other sites More sharing options...
wildteen88 Posted September 18, 2006 Share Posted September 18, 2006 Here is my additon to Barands code:[code]<?phpfunction 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. Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94126 Share on other sites More sharing options...
CanMan2004 Posted September 18, 2006 Author Share Posted September 18, 2006 hithanks 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 beDD99 4RTES12 1EGand so on.I didnt want to bug you guys too much about the first part, but I thought I would mention it anyway Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94131 Share on other sites More sharing options...
craygo Posted September 18, 2006 Share Posted September 18, 2006 So the first part will stay static and the second will go to 9ZZ and end there???Ray Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94132 Share on other sites More sharing options...
CanMan2004 Posted September 18, 2006 Author Share Posted September 18, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94140 Share on other sites More sharing options...
Barand Posted September 18, 2006 Share Posted September 18, 2006 This should work for any length, any format[code]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94161 Share on other sites More sharing options...
CanMan2004 Posted September 18, 2006 Author Share Posted September 18, 2006 Thanks so much for eveyones time Quote Link to comment https://forums.phpfreaks.com/topic/21143-increasing-numbersletters/#findComment-94185 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.