Pain Posted December 23, 2011 Share Posted December 23, 2011 Hi. I have this code which input data to the database. <?php include("connect.php"); echo $date = date("Y-m-d H:i:s"); echo "\n <br />"; echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); mysql_query("UPDATE users2 SET date2 = '$date2'"); $query = mysql_query("SELECT date2 FROM users2"); $numrows = mysql_num_rows($query); if ($numrows != 0) { while ($row = $mysql_fetch_array($query)) { $date_final = $row['date2']; } } echo $date_final; ?> Variable date2 is stored as timestamp. Now i want to display this two variable on the web, but this code doesn't work. Why? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/ Share on other sites More sharing options...
Pikachu2000 Posted December 23, 2011 Share Posted December 23, 2011 Define "doesn't work", please. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1300994 Share on other sites More sharing options...
Pain Posted December 23, 2011 Author Share Posted December 23, 2011 I get this line: Fatal error: Function name must be a string in /srv/disk4/866075/www/kingstonuni.atspace.co.uk/RPG/muziejus.php on line 18 That's probbably because it is a timestamp, not a string. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1300995 Share on other sites More sharing options...
Pikachu2000 Posted December 23, 2011 Share Posted December 23, 2011 Extraneous character . . . while ($row = $mysql_fetch_array($query)) Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1300996 Share on other sites More sharing options...
Pain Posted December 23, 2011 Author Share Posted December 23, 2011 Jesus Christ now that was kinda retarded of me. Thank you for spotting this error. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1300997 Share on other sites More sharing options...
Pain Posted December 24, 2011 Author Share Posted December 24, 2011 Oh one more thing. I am trying to increment a variable by using timestamps. <?php $submit = $_POST['submit']; include("connect.php"); echo $date = date("Y-m-d H:i:s"); echo "\n <br />"; if ($submit) { $i++; echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); mysql_query("UPDATE users2 SET date2 = '$date2'"); $query = mysql_query("SELECT date2 FROM users2"); $numrows = mysql_num_rows($query); if ($numrows != 0) { while ($row = mysql_fetch_array($query)) { $date_final = $row['date2']; } } } if ($date > $date_final) { mysql_query("UPDATE users2 SET slaves = '$i'"); } ?> However it doesn't increment at all. Is it possible to do this with php mysql at all? Or should i try using javascript, something like a countdown timer etc. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301000 Share on other sites More sharing options...
Pikachu2000 Posted December 24, 2011 Share Posted December 24, 2011 I have no idea what you're asking. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301029 Share on other sites More sharing options...
Pain Posted December 24, 2011 Author Share Posted December 24, 2011 1. Say time is 13.40. 2. Say we have a variable $i++; 3. When time is 14.00, increment $i. Get it?:] Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301046 Share on other sites More sharing options...
Pikachu2000 Posted December 24, 2011 Share Posted December 24, 2011 1. Say time is 13.40. 2. Say we have a variable $i++; 3. When time is 14.00, increment $i. Get it?:] Not really, no. All I can surmise form that is this: $i = 1; if( $time == '14:00' ) { $i++; } But I somehow suspect that isn't really what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301094 Share on other sites More sharing options...
Pain Posted December 24, 2011 Author Share Posted December 24, 2011 Take Travian for example. The amounts of wood, clay and other materials are being increased every x seconds:) thats what i meant. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301135 Share on other sites More sharing options...
litebearer Posted December 24, 2011 Share Posted December 24, 2011 Curious... 1. Will the database table EVER (other than initially creating the table) be empty? 2. Will $i ever contain a value other that that created in the script you have provided here? Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301158 Share on other sites More sharing options...
jcbones Posted December 24, 2011 Share Posted December 24, 2011 Is your slaves columns suppose to increment every 20 seconds, or only every 20 seconds while the user is online? Is there a maximum number of slaves at one time? The reason all these questions are asked, is we can suggest a more efficient way, and possibly keep it in the database itself. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301207 Share on other sites More sharing options...
Pain Posted December 26, 2011 Author Share Posted December 26, 2011 Curious... 1. Will the database table EVER (other than initially creating the table) be empty? 2. Will $i ever contain a value other that that created in the script you have provided here? 1. Not it should never be empty. 2. No, i will probably input $i in to the database everytime the script is executed. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301363 Share on other sites More sharing options...
Pain Posted December 26, 2011 Author Share Posted December 26, 2011 Is your slaves columns suppose to increment every 20 seconds, or only every 20 seconds while the user is online? Is there a maximum number of slaves at one time? The reason all these questions are asked, is we can suggest a more efficient way, and possibly keep it in the database itself. It is supposed to increment everytime user clicks on a button and there is no maximum. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301364 Share on other sites More sharing options...
litebearer Posted December 26, 2011 Share Posted December 26, 2011 See the comments in your code... <?php $submit = $_POST['submit']; include("connect.php"); echo $date = date("Y-m-d H:i:s"); echo "\n <br />"; if ($submit){ /* as you never set or change $i anywhere else, this will equate to 1. Is this your intent? */ $i++; echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); /* this will set ALL records. is this your intent? */ mysql_query("UPDATE users2 SET date2 = '$date2'"); /* effectively this sets $date_final to the value of the last record */ /* if that is your intent, you are being redundant as date2 = $date2, therefore $date_final = $date2 */ $query = mysql_query("SELECT date2 FROM users2"); $numrows = mysql_num_rows($query); if ($numrows != 0){ while ($row = mysql_fetch_array($query)){ $date_final = $row['date2']; } } } /* if NOT submit, $date_final has no value nor been set */ if ($date > $date_final){ mysql_query("UPDATE users2 SET slaves = '$i'"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301366 Share on other sites More sharing options...
jcbones Posted December 26, 2011 Share Posted December 26, 2011 You will also be setting slaves to 1, so your users will never have more than 1 slave. I would suggest leaving it up to the database. UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) This will do the same as all of your code above, Although, I think you would want to specify a row for a specific user. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1301381 Share on other sites More sharing options...
Pain Posted January 12, 2012 Author Share Posted January 12, 2012 You will also be setting slaves to 1, so your users will never have more than 1 slave. I would suggest leaving it up to the database. UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) This will do the same as all of your code above, Although, I think you would want to specify a row for a specific user. Thank you, your code helped a lot! But how do i make slaves increment 20 seconds after the button is clicked? It just inserts the date + 20 seconds into the database and increments slaves right away. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1306724 Share on other sites More sharing options...
jcbones Posted January 12, 2012 Share Posted January 12, 2012 I'm not sure why you need it to wait 20 seconds before adding slaves. Rather, you should be checking that 20 seconds has passed before adding the slaves to the table. By checking if the date2 that is currently stored is less than the current time, we know that 20 seconds have passed, otherwise it will not update the column. You should also be checking the user, so I threw that in there as well. UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) WHERE date2 < NOW() AND user = '$currentUser'; Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1306744 Share on other sites More sharing options...
Pain Posted January 12, 2012 Author Share Posted January 12, 2012 I'm not sure why you need it to wait 20 seconds before adding slaves. Rather, you should be checking that 20 seconds has passed before adding the slaves to the table. By checking if the date2 that is currently stored is less than the current time, we know that 20 seconds have passed, otherwise it will not update the column. You should also be checking the user, so I threw that in there as well. UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) WHERE date2 < NOW() AND user = '$currentUser'; OK i can now see some progress happening. What happens now is: Slaves increment when i load the page, not when i click on submit. Slaves increment straight away and will not increment for the next 20 seconds. after 20 s i can reload page again and slaves will increment again. If i want to make it increment when i click on a button (not when i load the page) i should probably write your line somewhere in the if($submit) line? Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1306899 Share on other sites More sharing options...
Pain Posted January 12, 2012 Author Share Posted January 12, 2012 Please ignore my last post. I have removed everything redundant from the code. <?php include("connect.php"); echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); mysql_query("UPDATE users2 SET slaves=slaves+1, date2=DATE_ADD(NOW(),INTERVAL 20 SECOND) WHERE date2 < NOW()"); ?> Now when the page loads, slaves is incremented instantly. After 20 seconds i can reload again, and slaves is incremented again as well. One last thing that i want to do is to make this incrementation possible without reloading the page:) ? Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1307028 Share on other sites More sharing options...
jcbones Posted January 12, 2012 Share Posted January 12, 2012 You will need to use AJAX for that. Quote Link to comment https://forums.phpfreaks.com/topic/253772-displaying-timestamp-from-db/#findComment-1307065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.