views:

97

answers:

2

So, I want people to be able to "bump" what they've wrote in my database but at the same time only allow the input to be in the table ONCE at a time.

For Example:

Jim's code is 5555. Jim enters his code and it shoots to the very bottom of the table. After 34 minutes he tries to enter his code in again (Because various other people have inputted their code between now and then) but gets a display error letting him know he has 26 minutes to wait still.

Joe inputs his code and waits an hour and five minutes and is able to push his code back to the bottom again.

Basically, I'm displaying data from the bottom up in my table.

Is there any way to easily do this? Thanks.

function some_more_custom_content() {

    $output="<BR>";

    ob_start();

    if ($_REQUEST['code'] != "") {
        $code = $_REQUEST['code'];
        $query="INSERT INTO `fc` (`code`,`datetime`) values ('" . mysql_real_escape_string($code) . "', now())";
        $result=mysql_query($query);
        $entry['datetime'] = strtotime($entry['datetime']);

        while ($fetch_array = mysql_fetch_array($result)) {
            $seconds = time() - strtotime($fetch_array["datetime"]);

            if ((time() - $entry['datetime']) < 60*60) {
                echo ("The code " . htmlentities($code) ." was updated less than an hour ago.");
            } else {
                echo ("Inserted " . htmlentities($code) ." into the top.");
            }
        }
?>

I get a syntax error. ANy idea where it is?

UPDATE: Getting error of : Parse error: syntax error, unexpected $end

A: 

Not that hard:

Use a timestamp for the last "bump time".

When a code is entered, check if it already exists in the database.
If it doesn't, insert it and set the bump timestamp to now().
If it does exist, check if the timestamp was within the hour. If it was, display error.
If it wasn't, reset it to now().

Sort your display data by bump timestamp.

EDIT:

$entry = /* get entry from database, assuming the case where it already exists */;

// depending on the format of timestamp you get from the database,
// you may have to convert it to a UNIX timestamp:
$entry['timestamp'] = strtotime($entry['timestamp']);

if ((time() - $entry['timestamp']) < 60*60) { // 60*60 is one hour in seconds
    // display error
} else {
    // reset bump
}
deceze
I already have it so people enter codes, it gets the now() stamp and it even displays how long ago it was entered (cause I throw the whole table out there). How do I check if the timestamp was within an hour and display that error? That's my issue.
Updated answer.
deceze
If you're not familiar with UNIX timestamps: http://en.wikipedia.org/wiki/Unix_time Once you get used to the idea that everything is expressed in seconds it's easy to do calculations with it.
deceze
@Frank: Paste it in your question, not somewhere else.
deceze
Okay, I edited it sorry.
@Frank: Also, you can write this bit of code yourself, you already got the advise for free. :)
deceze
I am REALLY new to this type of stuff so I really am lost with what I am doing.I'm not sure how to (if I fix the timestamp thing) where to even put it in my code so it displays properly.
Then you'll benefit all the more from figuring it out yourself. You need to learn to understand and modify your own code at some point, if you want to continue on this path. :o)
deceze
I don't want to continue this path. I am going to school for accounting. Just trying to make a site for a few friends :)
Still won't hurt you to do it yourself. Go stretch your legs for a while if you don't get it right now. :)
deceze
still got an error :(
+2  A: 

you should create a table with a unique index on the code field and then use a query like:

INSERT INTO CODES (code) 
  VALUES (555)
  ON DUPLICATE KEY UPDATE lastUpdated = 
               case when NOW() - INTERVAL 5 MINUTE > lastUpdated 
                  then NOW() 
                  else lastUpdated end

this will update the lastUpdated field only in cases when it's older than 5 minutes

Saggi Malachi