Woodburn2006 Posted September 8, 2006 Share Posted September 8, 2006 im getting a member id from a database in the format of: MEM0001is there anyway i can split the text and the number, so that i have: MEM and 0001?everytime i want to add a member i want 1 to be added so that it goes: mem0001, mem0002 etci know how to do the latter part, i just need to know how to split the mem from the number so that i can work out what the next one will be. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 8, 2006 Share Posted September 8, 2006 Are the letters [i]always[/i] the first 3 characters? If so, use substr, if not, regex. Quote Link to comment Share on other sites More sharing options...
robos99 Posted September 8, 2006 Share Posted September 8, 2006 "everytime i want to add a member i want 1 to be added so that it goes: mem0001, mem0002 etc"I know you said you know how to do this, but you do realize that the numbered portion is still going to be a string right? So you can't just say $numbers++ because you can't perform math on a string. I'm sure there's a way to convert data types....but I'm not sure off the top of my head. Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted September 9, 2006 Author Share Posted September 9, 2006 cool thanks, i now have the number, but as you said the numbers are in a string and when i try adding 1 to it, all i get is the value being 1, is there any way i can convert this to a number so that i can do maths with it? Quote Link to comment Share on other sites More sharing options...
extrovertive Posted September 9, 2006 Share Posted September 9, 2006 How about in your database table, how two fields? ID and memberid? ID will be the auto increment number and memberid will be a string of "member" + ID as a string? Each time you add a member, you take the ID of what was last inserted and then append it to the "member" string then insert it to the memberid field. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 9, 2006 Share Posted September 9, 2006 Did you actually try adding 1 to the number part? Adding a number to a number is a string should work correctly. Copy and run this example:[code]<?php$str = "mem0001";$mem = substr($str,0,3);$num = substr($str,3);echo $str . ' => ' . $mem . ',' . $num . '<br>';$num++;echo '$num + 1 = ' . $num;echo '<br>New member number = ' . $mem . sprintf("%04d",$num);?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
Woodburn2006 Posted September 13, 2006 Author Share Posted September 13, 2006 it is not adding the mem at the beginning, it is just numbers.worked fine when i tried it with the code you have but when i used it in my function it dint workany ideas why it is doing this? Quote Link to comment 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.