whare Posted August 1, 2006 Share Posted August 1, 2006 Hi allright first things first Im new to php so please explain thens so i can understand them lolright here is what i want to do I want to do a number increase with a prefix so i could have DAV0001 and then for the next one DAV0002 all the way to DAV9999 but i also would like it to use the the nembers that are not in use dew to removed accounts so EG:DAV0001 [INUSE]DAV0002 [INUSE]DAV0003 [DELETED]DAV0004 [INUSE]DAV0005 [INUSE]DAV0006 [NOTUSED]so with that it will go back and use 0003 again befor using 0006now i would like them to be formated like that on the webpage along with other info that i am working on now but im just stuck on that oohhh by the way I am using mySQL db just to help you help me with thisThanx AllWhare Link to comment https://forums.phpfreaks.com/topic/16215-auto-increase/ Share on other sites More sharing options...
bpops Posted August 1, 2006 Share Posted August 1, 2006 Well the easiest thing to do would be to create a column in your database that has the AUTO_INCREMENT attribute, where it will start at 1 and then add 1 to the highest of that column in the table, however, it will not do what you want to do in terms of rewriting deleted ones... so it would go like this1, 2, 3, 4, 5, 6 then when you delete, say 3, you'd have1,2,4,5,6 then when you add another,1,2,4,5,6,7But that's all I can think of. Maybe someone else has a better idea. Link to comment https://forums.phpfreaks.com/topic/16215-auto-increase/#findComment-67100 Share on other sites More sharing options...
whare Posted August 1, 2006 Author Share Posted August 1, 2006 yes i thought of using that but again Im not sure on how to do the prefix of DAVxxxx (x = number) but i do need to keep a constan of x amout of digits In this case 4 anyideas? Link to comment https://forums.phpfreaks.com/topic/16215-auto-increase/#findComment-67104 Share on other sites More sharing options...
bpops Posted August 1, 2006 Share Posted August 1, 2006 I'm still a bit of a new guy myself, but you could try checking the number using strlen (even though its a number, php should be able to look at it as a string), if its 1, then append 3 zeroes to the front, if 2, append 2 zeroes. There's probably a better way of doing this, but this should work also.. so[code]<?php//Add the appropriate number of zeroesswitch(strlen($num)) { case 1: $num .= "000".$num; break; case 2: $num .="00".$num; break; case 3: $num .="0".$num; case 4: break;}//Append the number now to the username$username .= $num;?>[/code]Again someone might have a much cleaner, better way of doing this. Link to comment https://forums.phpfreaks.com/topic/16215-auto-increase/#findComment-67110 Share on other sites More sharing options...
whare Posted August 1, 2006 Author Share Posted August 1, 2006 Great thanxI will have a look and give it a go see if i can get it to work :) although I think i should have played with an easyer script as I have only been working with php for 3 days lol but never mind its not that hard if you can understand the basics and its even easyer with places like this that have users who will helpThanx AlotWhare Link to comment https://forums.phpfreaks.com/topic/16215-auto-increase/#findComment-67127 Share on other sites More sharing options...
whare Posted August 2, 2006 Author Share Posted August 2, 2006 right peoject update and im still stuck lolhere look[code]<?phpinclude "db.php";// Get all the data from the "example" table$result = mysql_query("SELECT * FROM users") or die(mysql_error()); echo "<table border='1'>";echo "<tr> <th>Name</th> <th>Pilot ID</th> <th>Join Date</th> </tr>";// keeps getting the next row until there are no more to getwhile($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['first_name']; echo " "; echo $row['last_name']; echo "</td><td>"; echo $row['userid']; echo "</td><td>"; echo $row['signup_date']; echo "</td></tr>"; } echo "</table>";?>[/code]Where echo $row['userid']; that is what i want to change to DAVxxx[userid] but the number of "0" depends on the userid so if the userid is 1 there will be 3 "0" but if it is 21 there will only be 2 "0" added now i tried to add the code the bpops said about but i have no idea what im doing lol here is bpops code[code]<?php//Add the appropriate number of zeroesswitch(strlen($num)) { case 1: $num .= "000".$num; break; case 2: $num .="00".$num; break; case 3: $num .="0".$num; case 4: break;}//Append the number now to the username$username .= $num;?>[/code]thanx all Link to comment https://forums.phpfreaks.com/topic/16215-auto-increase/#findComment-67899 Share on other sites More sharing options...
ryanlwh Posted August 2, 2006 Share Posted August 2, 2006 THere is an easier way to add leading zeroes[code]<?php$num = sprintf('%04d',$num);$username .= $num;?>[/code]if you need more leading zeroes, just replace 4 with a larger number. Link to comment https://forums.phpfreaks.com/topic/16215-auto-increase/#findComment-67903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.