Jump to content

How to display data based on time ?


argonz
 Share

Recommended Posts

e.g : my database---------------------------id | text | time |---------------------------1 | print1 | 3 |---------------------------2 | print2 | 5 |--------------------------3 | print3 | 2 |--------------------------I want the field text show base on time, field time is the duration of the text should be show before the next text appear.detail :page onload(1st second) --> print1 showat the 4th second --> print2 show at the 9th second --> print3 showhow to do that ?Best Regards !

Edited by Argonz
Link to comment
Share on other sites

So... relative to the time the page loads, after a certain time, you want another image?You'll have to use JavaScript for that, and setTimeout() in particular. You can use PHP to create the JavaScript based on what's in the database.Try to do it "manually" with these sample images and times, and you'll see constructing the PHP from then will be trivial.

Link to comment
Share on other sites

?phpmysql_select_db("belajar",mysql_connect("localhost","root",""));function cetak($id){	$query = mysql_query("select * from delay where id = $id");	$row = mysql_fetch_array($query);	echo $row[text];}?><script type="text/javascript">var waktu=0;																																														   function timer(time,id){		alert("test ji");	if(waktu==time){		document.write("<?php echo cetak("+id+"); ?>");	}	setTimeout("timer(time,id)",1000);	waktu++;}timer(1,1);timer(3,2);timer(5,3);</script>

I write like above but it does not work, I hv no idea where is the wrong of the code.

Link to comment
Share on other sites

PHP and JavaScript are completely separate - you can't interact between them like that. It's like trying to open your garage door by pressing your car's accelerator :).You can, as boen_robot says, use PHP to write all the values out in JavaScript, e.g. in an array, and then read that array when the time comes.

Link to comment
Share on other sites

you cant insert a javascript value within a php declaration, also php server script runs on page load only.you best option would be to create an javascript array of the text from database, and use id as a index ref to this text, and then do a search through the array for the relative text from the id variable.

<script type="text/javascript">/*<![CDATA[*//*---->*/textarray = new Array();<?php // create a javascript array of database text recordswhile ($row = mysql_fetch_assoc($result))	{echo 'textarray['.$row['id'].']="'.$row['text'].'";	}?>/*--*//*]]>*/ </script>

Link to comment
Share on other sites

You can, as boen_robot says, use PHP to write all the values out in JavaScript, e.g. in an array, and then read that array when the time comes.
I don't understand what u mean.., My English is so bad.., could you give me the sample code how to use PHP to write the value in JavaScript !@dsonesuk : I've tried as your code, but its does not work. I don't understand that code mean. Is it OK PHP code in JavaScript code.
<script type="text/javascript">textarray = new Array();<?phpmysql_select_db("belajar",mysql_connect("localhost","root",""));$result = mysql_query("select * from delay where id = $id");while ($row = mysql_fetch_assoc($result)){	echo 'textarray['.$row['id'].']="'.$row['text'].'";}?></script>

that code doesn't work..!

Link to comment
Share on other sites

Ok! forgot to change query to show all records, and misplaced quote in code.

<?php // create a javascript array of database text records  $query = mysql_query("select * from delay");  while ($row = mysql_fetch_array($query, MYSQL_ASSOC))	 { echo 'textarray['.$row['id].']="'.$row['text'].'";'."\n";	 } ?>

If this runs correctly, and you right click the page and veiw page source, you should see a javascript array list of all text field records.similar to below

<script type="text/javascript">/*<![CDATA[*//*---->*/textarray = new Array();textarray[27]="MATTHAIS";textarray[26]="JOWAN";textarray[25]="JUAN";textarray[24]="MICHAEL";textarray[19]="KEVIN";textarray[20]="JONATHAN";textarray[23]="ANDREW";textarray[18]="RYAN";textarray[17]="WILLIE";textarray[16]="AUBREY";textarray[15]="CLINTON";textarray[14]="JOHANDRE";textarray[13]="RYNO";textarray[1]="LARISSA";textarray[22]="LEIF";textarray[21]="WERNER";textarray[12]="ROSSWELL";textarray[11]="ALEX";textarray[10]="VEGARDT";textarray[9]="RICHARD";textarray[8]="CHRIS";textarray[7]="THABO";textarray[6]="JEAN-PIERRE";textarray[5]="ANDRIES HEYDENREICH";textarray[4]="HEINRICH";textarray[3]="NEVILLE";textarray[2]="PAUL";/*--*//*]]>*/</script>

Edited by dsonesuk
Link to comment
Share on other sites

:) Let me clarify what I meant previously by "manually"... without PHP.... no PHP... just HTML and JavaScript.This is going to be VERY difficult if you try mixing PHP and JavaScript. Do the JavaScript with a hard coded sample data and make it work with THAT.Only then should you move to PHP.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...