Jump to content

Display data function


rajaahsan

Recommended Posts

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..:-)

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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);

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.