Jump to content

Archived

This topic is now archived and is closed to further replies.

whare

auto increase?

Recommended Posts

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

Share this post


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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 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;

?>[/code]


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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
right peoject update and im still stuck lol

here look

[code]<?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>";
?>[/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 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;

?>[/code]

thanx all

Share this post


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

Share this post


Link to post
Share on other sites

×

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.