Jump to content


Photo

auto increase?


  • Please log in to reply
6 replies to this topic

#1 whare

whare
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 01 August 2006 - 03:13 PM

Hi all


right first things first Im new to php so please explain thens so i can understand them lol

right 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 0006

now 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 this

Thanx All
Whare



#2 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 01 August 2006 - 03:50 PM

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 this

1, 2, 3, 4, 5, 6
then when you delete, say 3, you'd have
1,2,4,5,6
then when you add another,
1,2,4,5,6,7

But that's all I can think of. Maybe someone else has a better idea.

#3 whare

whare
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 01 August 2006 - 03:56 PM

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?

#4 bpops

bpops
  • Members
  • PipPipPip
  • Advanced Member
  • 232 posts

Posted 01 August 2006 - 04:04 PM

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

<?php

//Add the appropriate number of zeroes
switch(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;

?>


Again someone might have a much cleaner, better way of doing this.

#5 whare

whare
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 01 August 2006 - 04:18 PM

Great thanx

I 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 help

Thanx Alot
Whare

#6 whare

whare
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 02 August 2006 - 04:18 PM

right peoject update and im still stuck lol

here look

<?php
include "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 get
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	echo "<tr><td>"; 
	echo $row['first_name'];
	echo "&nbsp;";
	echo $row['last_name'];
	echo "</td><td>"; 
	echo $row['userid'];
	echo "</td><td>";
	echo $row['signup_date'];
	echo "</td></tr>"; 
} 

echo "</table>";
?>

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

<?php

//Add the appropriate number of zeroes
switch(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;

?>

thanx all

#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 02 August 2006 - 04:23 PM

THere is an easier way to add leading zeroes
<?php
$num = sprintf('%04d',$num);
$username .= $num;
?>
if you need more leading zeroes, just replace 4 with a larger number.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users