Solved

How to make a running number(ex:001,002,003,etc) appear in a textbox

Posted on 2008-10-30
7
366 Views
Last Modified: 2013-12-13
Dear Experts,
i wanna ask, how to make a running number (ex:001,002,003,...etc) appear in a textbox. note,  i dont want to make this running number from auto increment in mysql database, but i wanna describe it manually.also this running number automatically increment after i push a submit button(this submit button are a Saved Query into database).in other word, after i click my submit button,this running number automatically increased.i am using php 5,javascript and mysql database.

many thanks...
Regards
Louis
0
Comment
Question by:antonius_louis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:ionutz2k2
ID: 22838771
I think what you need is a $_SESSION variable (ex. $_SESSION['myIndex']) that stores that number you want to increment. The script that is run when you press the submit button increments that variable and the script that creates the page and shows that number will show the incremented value.
0
 
LVL 17

Expert Comment

by:psimation
ID: 22839089
This will submit the form to itself and have an incremented number already in textbox when you submit.

Add your SQL insert code below the if statement ( check for $_POST[submit] etc before you do your sql)

<?php
session_start();
if (isset($_SESSION[count]) {
$_SESSION[count]++;
} else {
$_SESSION[count] = "1";
}
?>
<form name="form" method="post" action="">
<input type="text" name="textbox" value="<?=$_SESSION[count]"?>">
<input type="submit" name="submit>
</form>

This code will work "per user" and for the length of the session only.

If you want a number that increments for ALL users who use your site, then it's going to become really tricky.

1st - you will need to use mysql table to first retreive the last number used, then display it, then increment it, and then update the table holding this number.

HOWEVER, this will possibly cause duplicate numbers on busy sites, since there is a timelag between reading, incrementing and updating, meaning two users who hit your site can potentially get the same number back from the table...

What is the purpose of this number you want to have displayed ( also, remeber that displaying the number inside a texfield means a user can change it...)

If you want to indicate or use it as part of a membership number, then I suggest you only generate that number AFTER the data has been entered into the table, then get the "last insert id" back and use that number as part of your unique sequenced number...



0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 22844432
If you want to control this number for the duration of a session, put the number in the session.

If you want to control this number on a per-user basis and have it persist beyond the session, put the number into the user records in your data base.

If you want to control this number across the entire population of uses in the data base, use LOCK TABLES while retrieving and updating the number.

Tell us a little more about what you want to use the number for, and we can give you a more succinct explanation.

Best regards, ~Ray
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:antonius_louis
ID: 22848538
for All  Experts:Thanks for the explanation...

for Psimation : could you give me a specific expalanation about your comment ("1st - you will need to use mysql table to first retreive the last number used, then display it, then increment it, and then update the table holding this number.").

regards
0
 
LVL 17

Expert Comment

by:psimation
ID: 22848667
OK, very basic example...

//create a new table called number_table containing only one collumn called "number" of type int size 255
//connection strings here
$sql = "select number from number_table";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$number = $row[number]; // <<== now you have the last number that was writen to table
//now use the number
echo '<input type="text" name="whatever" value="'.$number.'">';
//now increment number and write back to table
$number++;
$sql_update = "update number_table set number = \"$number\";
$result_update = mysql_query($sql_update);
//fin

Now, just remeber the comment from Ray_Paseur re locking tables.

I'm sure he can give you an example of doing that ( you need to add extra SQL to do that)


0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22854097
Man Page here: http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

The query sequence would be like this:

LOCK TABLE number_table WRITE
SELECT number FROM number_table
/* USE NUMBER as $number */
$number++;
UPDATE number_table SET number = $number
UNLOCK TABLES

Now in the spirit of 100% truth, you do not absolutely have to use MySQL for this - you could also use a flat file, but the ease with which you can lock and unlock tables makes MySQL the easier-to-code answer.

And another note: Minimize the time and risk of script termination between SELECT and UPDATE- you do not want to keep a table locked longer than the minimum!

Best regards, ~Ray
0
 

Author Closing Comment

by:antonius_louis
ID: 31511564
works great. thanks a lot...

regards
Louis
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question