rajaahsan Posted April 27, 2011 Share Posted April 27, 2011 This is the php code i have wrote but i need some help....as this code is displaying my value from database when time(which is saved in database too) is equal to current server time. let say e.g : time saved in database is 6:00:00, so when current time is equal to database time which is 6:00:00 it then display my stored value(e.g 40 (int) ) from database. which is : 40 so i need help out here when the value shows : 40 on exact 6:00:00 (time) but disappear after a second i want to be display this value for 30 more sec.after that a new value from database will come and diplay as the time goes to 6:00:30 hope friend you will help me out soon thank you below is the code of php file. // First Connect to database echo "<meta http-equiv='refresh' content='20'>"; $link = mysql_connect("$dbhost","$dbuser","$dbpass") or die(mysql_error()); // Connection starts here mysql_select_db("$dbname") or die(mysql_error()); $sql = "SELECT * FROM datatable WHERE time = '$ctime' "; // simple sql example echo $cdate; echo "<br>"; echo $ctime; $re = mysql_query($sql) or die(mysql_error()); // Here's where you'll get an error if the SQL is invalid. while($row = mysql_fetch_array($re)) { // Beganing 1 include("variables.php"); echo "<br>"; echo $row['value']; // value to be displayed for 30Sec after displayed on site. } //Closing 4 mysql_close($link); // code ends here..:-) Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 27, 2011 Share Posted April 27, 2011 I assume you always want to show a value, so one solution would be to simply select the smallest time that is greater than or equal to the current time. SELECT * FROM datatable WHERE time >= '$ctime' ORDER BY time ASC LIMIT 1 Quote Link to comment Share on other sites More sharing options...
rajaahsan Posted April 27, 2011 Author Share Posted April 27, 2011 please explain it more clearly... you are rite i want to display values all the time but when the specific time comes, i didnot get your point what should i do with this code, SELECT * FROM datatable WHERE time >= '$ctime' ORDER BY time ASC LIMIT 1 // what does this line will do now? explain this code ( ORDER BY time ASC LIMIT 1 ) what does it do? i just want to display the value not for 30second as my code displays only for a second. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 27, 2011 Share Posted April 27, 2011 I told you what it would do ...select the smallest time that is greater than or equal to the current time But, to explain further... The WHERE clause will match ALL records where time is >= $ctime. That would include the record you want plus the rest for the remainder of the day. But, we want the lowest of those values. So, we then ORDER the results by the "time" column in ASCending order so the first record is the smales and the last record is the greatest. Lastly, we restrict the result to only the very first record using LIMIT 1 On second though, though, this would not do exactly as you requested. If, the time was exactly 6:00:00 it would get the records that exactly matches 6:00:00. But, at 6:00:01 to 6:00:30 it would get the record for 6:00:30. So, the value would change every 30 seconds, but it wouldn't get the previous record in-between exact matches - it would get the next record. To get the previous record you would need to modify $ctime to be 29 seconds less OR you could modify the query to look for a value 29 seconds less. I would have to see how you are setting $ctime and/or know exactly what type of field "time" is in the database. Quote Link to comment Share on other sites More sharing options...
rajaahsan Posted April 27, 2011 Author Share Posted April 27, 2011 i just want to display the value for 30second as my code displays only for a second and then disapper till the next value comes and so own... as i have used while statement which check continuously the time value from database , so thats is why my value disappers is there any trick which i can perform i between this while loop code , while($row = mysql_fetch_array($re)) { // Beganing 1 include("variables.php"); echo "<br>"; echo $row['value']; // value to be displayed for 30Sec after displayed on site as in this condition it show for 1 sec and disappers. } //Closing 4 Quote Link to comment Share on other sites More sharing options...
rajaahsan Posted April 27, 2011 Author Share Posted April 27, 2011 FIrst of all thank for replying..... my variables are as under $cdate = date("d-m-y"); // current server date $ctime = date("h:i:s"); // current server time i have upload sql database table as well in pic you can understand then.... [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 27, 2011 Share Posted April 27, 2011 Define a different "time" for purposes of the db query $ctimeSQL = date("h:i:s", strtotime('-29 seconds')); And change your query as I showed above: $sql = "SELECT `value` FROM datatable WHERE time >= '$ctimeSQL' ORDER BY time ASC LIMIT 1"; Also, since you are only getting one record it makes no sense to use a while loop. // Define date and time $cdate = date("d-m-y"); // current server date $ctime = date("h:i:s"); // current server time -29 seconds $ctimeSQL = date("h:i:s", strtotime('-29 seconds')); // current server time -29 seconds // Connect to database $link = mysql_connect("$dbhost","$dbuser","$dbpass") or die(mysql_error()); // Connection starts here mysql_select_db("$dbname") or die(mysql_error()); // Create and run query $sql = "SELECT `value` FROM datatable WHERE time >= '$ctimeSQL' ORDER BY time ASC LIMIT 1"; // simple sql example $re = mysql_query($sql) or die(mysql_error()); // Here's where you'll get an error if the SQL is invalid. $row = mysql_fetch_array($re); //Output HTML echo $cdate; echo "<br>"; echo $ctime; echo "<meta http-equiv='refresh' content='20'>"; include("variables.php"); echo "<br>"; echo $row['value']; // value to be displayed for 30Sec after displayed on site. mysql_close($link); Quote Link to comment Share on other sites More sharing options...
rajaahsan Posted April 27, 2011 Author Share Posted April 27, 2011 thank you vvvvvvvvvvv much 4end.... Quote Link to comment 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.