Hi all,

Is it possible to use the same javascript code in a while loop? The first record that is pulled from the database displays the javascript counter. Yet on the second iteration of the while loop (a new record pulled from the database), the javascript just seems to get ignored by php. Here is the section of code:

<?php

$request = mysql_query("SELECT * FROM auctions WHERE category_id BETWEEN '12' AND '84' ORDER BY a_timestamp DESC");
		echo '<h1>'.$_GET['ctg'].' auctions</h1>';		
	
	$i = 0;		
	while ($i <= 6 and $result = mysql_fetch_assoc($request)) {

		$time = $result['a_timestamp'];
		$length = $result['a_length'];
		$expires = strtotime($time. '+' .$length. 'days');
		$expires = date('m/d/Y g:i A',$expires);
?>
	<table border="0" cellpadding="5px">
			<tr class="shade1">
				<td rowspan="3" height="50px" width="50px">
		<?php echo "<img src=https://students.ee.port.ac.uk/ece60363/".$result['a_image'] ." height=70px width=70px>"; ?>
				</td>
				<td class="error">Buy it Now price: &#163;<?php echo $result['a_buyitnow']; ?></td>
				<td class="error">Current Price: &#163;</td>
				<td class="error">
					Time Remaining: 
					<script language="JavaScript">

						TargetDate = "<?php echo $expires; ?>";

						BackColor = "";

						ForeColor = "";

						CountActive = true;

						CountStepper = -1;

						LeadingZero = true;

						DisplayFormat = "%%D%% Days %%H%% Hrs %%M%% Mins %%S%% Secs";

						FinishMessage = "Auction Ended!";

					</script>

					<script language="JavaScript" src="/ece60363/count.js"></script>
				</td>
			</tr>
			<tr class="shade2">
				<td colspan="2"><a href="/ece60363/auction.php?auc=<?php echo $result['a_id']; ?>">
						<?php echo $result['a_title'];?></a>
				</td>
				<td>Quantity Remaining: <?php echo $result['a_quantity']; ?></td>
				
			</tr>
			<tr class="shade1">
				<td colspan="6" rowspan="2"><?php echo $result['a_description'];?></td>
			</tr>
			<tr>
				
			</tr>
	</table>
	<hr>
<?php 
	$i++;
	}	
?>

Any thoughts???

Okay, my apologies for my dull moment! I think this is to do with the fact that PHP is sending the request (server-side), and the Javascript (client-side) is only taking the value last generated from the while loop.
Can anyone confirm this? Does anyone know a work around?

Many thanks

Member Avatar for diafol

SOrry, don't understand why you're placing js into the body. As far as I know - which isn't much on JS, you're overwriting the same variable.

<script language="JavaScript">
  TargetDate = "<?php echo $expires; ?>";
 BackColor = "";
 ForeColor = "";
 CountActive = true;
 CountStepper = -1;
 LeadingZero = true;
 DisplayFormat = "%%D%% Days %%H%% Hrs %%M%% Mins %%S%% Secs";
 FinishMessage = "Auction Ended!";
</script>
<script language="JavaScript" src="/ece60363/count.js"></script>

How about placing the script into the head area of the page? I think you'll find that the file won't run until the whole page is sent to the browser.
If your count.js file just contains a function, this should also be placed in the head area. From what I can see your script is creating initial variable values.

Do you really need js with this?

Ardav,
Thanks for your reply. To be honest, I am new to JavaScript and have only been learning PHP for the last 2-3 months, so I apologise if I my understanding is not up to scratch. The functionality I am trying to achieve is to have a countdown timer for each record that is retrieved from the database.

The countdown script I am using is publicly available from this site. It was the only script I found whereby I could format and 'plug-in' my own values from a database.

If I was to place the Javascript in the head section, is there a way I could 'call' the countdown clock within the while loop to dynamically display the countdown for each record retrieved??

Your advice is much appreciated.
Regards,
Nonshatter

Member Avatar for diafol

Ok, thought as much. This won't work as php is done before it reaches the browser. So javascript is pointless. You can use a php timer function, but you'll find that the counter will be extremely quick - for small recordsets it will be in the 10s of milliseconds range.

Okay..... I will look into the PHP timer and get back to you. However, how do websites like Ubid manage do it?

Member Avatar for diafol

Ah, OK I understand now. Yes, this will be via javascript. The secret is not to load your js in the body. All your specified tags will be counted and updated together. You can use an id/name/classname/ rel attribute to give your elements a 'hook' so that javascript can do its magic (countdowns) on them. You may be better to post this question in the javascript/dhtml/ajax forum as it is more apt. The php/html is easy, it's the js that will require some messing about with.

Hint: have a js array that is built via php, listing all valid records that are able to be counted (any ones where DB value - now() is positive). Do a 'for each' loop over all these 'elements', changing the displayed values by -1 every second.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.