Jump to content

Can I do this with php...?


hostfreak

Recommended Posts

I'm making a page that I want to have a number on it called Trip number. Everytime the page is opened by a member I want it to be able to have that number increment by one starting at 40,000. I don't need the number tracked in a database or anything because it will be on a email form that will get printed by the user and sent to the admin. So is that possible?
Link to comment
Share on other sites

[!--quoteo(post=368204:date=Apr 24 2006, 07:28 PM:name=sanfly)--][div class=\'quotetop\']QUOTE(sanfly @ Apr 24 2006, 07:28 PM) [snapback]368204[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I think that you will need to keep in a database, otherwise you have no way of keeping track of what number you should be up to
[/quote]


I see. I just don't know how I would ge the database to know since no information will be entered into a database from that page. It will just be sent through an email.
Link to comment
Share on other sites

[!--quoteo(post=368234:date=Apr 24 2006, 08:51 PM:name=sanfly)--][div class=\'quotetop\']QUOTE(sanfly @ Apr 24 2006, 08:51 PM) [snapback]368234[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Do you use a database to keep track of your members?
[/quote]


Yeah I do. It's just that I don't need none of the information on the page recorded since they will print it before they send and the information will be sent to an email. I'm a bit confused on this. Thanks for taking the time so far.
Link to comment
Share on other sites

Im assuming that your using mysql:

Make a table called counter with the fields count_id and count_num

[code]CREATE TABLE `counter` (
`count_id` INT( 25 ) NOT NULL AUTO_INCREMENT ,
`count_num` VARCHAR( 200 ) NOT NULL ,
PRIMARY KEY ( `count_id` )
) TYPE = MYISAM;[/code]

Insert your first value

[code]INSERT INTO counter( count_num )
VALUES (
'40000'
)[/code]

The id for this counter will be 1

somewhere on the page that you're counting, use the following script

[code]$r = mysql_query("SELECT count_num FROM counter WHERE count_id = '1'") or die("SELECT ERROR: " . mysql_error());
        $row = mysql_fetch_array($r);
            $new_count = $row['count_num'] + 1;
    $r = mysql_query("UPDATE counter SET count_num = '$new_count' WHERE count_id = '1'") or die("UPDATE ERROR: " . mysql_error());[/code]
Link to comment
Share on other sites

Why even have two fields in the table. Just have one, "count_num". There's only going to be one record in the table anyway, so theres no need for an autoincremented id. Why did you make "count_num" a varchar and not an int?

This would change the table to:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]CREATE TABLE `counter` (
`count_num` int(5) DEFAULT [color=red]'40000'[/color] NOT NULL ,
) TYPE [color=orange]=[/color] MYISAM; [!--sql2--][/div][!--sql3--]

The insert would then become:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']INSERT[/span] [color=green]INTO[/color] [color=orange]`counter`[/color] VALUES (40000); [!--sql2--][/div][!--sql3--]

The update in your PHP code would be:
[code]<?php
$q = "SELECT count_num FROM counter");
$r = mysql_query($q) or die("SELECT ERROR: $q<br>" . mysql_error());
$row = mysql_fetch_array($r);
$new_count = $row['count_num'] + 1;
$q = "UPDATE counter SET count_num = $new_count";
$r = mysql_query($q) or die("UPDATE ERROR: $q<br>" . mysql_error());
?>[/code]

Ken
Link to comment
Share on other sites

[!--quoteo(post=368389:date=Apr 25 2006, 11:56 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 25 2006, 11:56 PM) [snapback]368389[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Why even have two fields in the table.
[/quote]

So if you ever want another counter for anything, its all set up. Thinking ahead....
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.