Jump to content

Javascript timer not looping along with mysql_fetch_array results


RaviRanjanim

Recommended Posts

javascript is not looping with mysql_fetch_array result even though it showing timer on one product only .i don't know what mistake i have did ,,plz help me ,,i ma very new with php and java script..even i have taken javascript timer from google itself ..plz help me

 

 

<?php
$conn
= mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('bidding', $conn);
if(!$status) die("Failed to select database!");

$result = mysql_query("SELECT * FROM bids where id");

$numrow = mysql_num_rows($result);

if ($numrow == 0)
{
die('No record found.');
}
?>


<?php

while($row=mysql_fetch_array($result)){

$closedate = date_format(date_create($row['closing_date']), 'm/d/Y H:i:s');
$images_field= $row['image'];
$image_show= "images/$images_field";
?>
<div class="bidbar1">
<div id="lastsold"><strong>Last Sold : ₹ </strong><?php echo $row['lastsold']; ?> </div>
<div id="title"><?php echo $row['description']; ?></div>
<div id="detail"><img src="image/moredetail.jpg" height="150" alt="more detail" /> </div>
<div id="image"><img src="<?php echo $image_show; ?>" width="205" height="150" alt="more detail" /></div>
<div id="clock"><table><tr><td style="font-size:18px;text-align:center; padding- left:30px;">Days    Hr    Min    Sec</td></tr> <tr><td style="font-size:32px; font:bold; text-align:center;padding-left:30px;">

<script>

var cdtd = function(id,end) {
  var start = new Date();
  var timeDiff = end.getTime() - start.getTime();
               
  if(timeDiff <=0 ) 
  {
//$('#bidbar1').remove();
//return;
  
 
// alert('We have lift off!'); 
         //$('#bidbar1').delay(50000000).fadeout();
$(window).load(function(){$('#bidbar1').remove(); });
    return false;
}
  var seconds = Math.floor(timeDiff / 1000);
  var minutes = Math.floor(seconds / 60);
  var hours = Math.floor(minutes / 60);
  var days = Math.floor(hours / 24);
  hours %= 24;
  minutes %= 60;
  seconds %= 60;
  $( id + " .days").html(days);
  $( id + " .hours").html(hours);
  $( id + " .minutes").html( minutes);
  $( id + " .seconds").html( seconds );
  console.log(id + " .hoursBox",$( id + " .hoursBox").length,id,end,hours,minutes,seconds)
  var timer = setTimeout(function(){cdtd(id,end)},1000);
  
}
cdtd("#counter1",new Date("<?php echo $closedate; ?>"));
//cdtd("#counter2",new Date("march 16, 2014 18:06:30"));
//cdtd("#counter3",new Date("march 16, 2014 17:37:00"));
//cdtd("#counter4",new Date("march 16, 2014 17:38:00"));
//cdtd("#counter5",new Date("june 2, 2014 10:55:00"));
//cdtd("#counter6",new Date("April 2, 2014 10:30:00"));
//cdtd("#counter7",new Date("April 3, 2014 00:01:00"));
//cdtd("#counter8",new Date("April 1, 2014 00:01:00"));
//cdtd("#counter9",new Date("April 4, 2014 00:01:00"));
//cdtd("#counter10",new Date("April 5, 2014 00:01:00"));
//cdtd("#counter11",new Date("April 19, 2014 00:01:00"));
//cdtd("#counter12",new Date("April 20, 2014 00:01:00")); 
 
</script>
<div id="counter1">
  <div class="box days">0</div>
  <div class="box hours">0</div>
  <div class="box minutes">0</div>
  <div class="box seconds">0</div>
</div>

 

 

 

</td></tr></table></div>

<div id="mrp"><strong>MRP : ₹</strong><?php echo $row['mrp']; ?></div>

<div id="endt"><strong>End Time: ₹</strong><?php echo $row['closing_date']; ?></div>

<div id="fee"><strong>Bid Fee : ₹</strong><?php echo $row['bid fee']; ?></div>

<div id="current"><strong>Current Winner</br></strong></div>

<div id="buy">
<table width="271" border="0" cellpadding="0.1">
<tr>
<td><img src="image/buy.png" alt="buy now" longdesc="http://buy now" /></td>
<td width=110><?php echo $row['currentwinner']; ?></td>
<td><img src="image/bid.png" alt="bid now" longdesc="http://bidnow " /></td>
</tr>
</table>
</div></div>
<?php
}
?>

Link to comment
Share on other sites

The JavaScript code for defining cdtd function needs to go outside of the while loop.

 

The only javascript code that should be in the while loop is when calling the cdtd function for creating the counter.

<!-- Javascript cdtd function outside of loop -->
<script>
var cdtd = function(id,end) {
  var start = new Date();
  var timeDiff = end.getTime() - start.getTime();
               
  if(timeDiff <=0 ) {
    $(window).load(function(){$('#bidbar' + id).remove(); });
    return false;
  }

  var seconds = Math.floor(timeDiff / 1000);
  var minutes = Math.floor(seconds / 60);
  var hours = Math.floor(minutes / 60);
  var days = Math.floor(hours / 24);
  hours %= 24;
  minutes %= 60;
  seconds %= 60;
  $( "#counter" + id + " .days").html(days);
  $( "#counter" + id + " .hours").html(hours);
  $( "#counter" + id + " .minutes").html( minutes);
  $( "#counter" + id + " .seconds").html( seconds );
  console.log("#counter" + id + " .hoursBox",$("#counter" +  id + " .hoursBox").length,id,end,hours,minutes,seconds)
  var timer = setTimeout(function(){cdtd(id,end)},1000);
  
}
</script>
<!-- now have the while loop -->

<?php

$count = 1; // initialise counter
while($row=mysql_fetch_array($result))
{
  $closedate = date_format(date_create($row['closing_date']), 'm/d/Y H:i:s');
  $images_field= $row['image'];
  $image_show= "images/$images_field";
?>
<div id="bidbar<?php echo $count; /* append counter */ ?>">
  <div id="lastsold"><strong>Last Sold : ₹ </strong><?php echo $row['lastsold']; ?> </div>
  <div id="title"><?php echo $row['description']; ?></div>
  <div id="detail"><img src="image/moredetail.jpg" height="150" alt="more detail" /> </div>
  <div id="image"><img src="<?php echo $image_show; ?>" width="205" height="150" alt="more detail" /></div>
  <div id="clock">
    <table>
      <tr>
        <td style="font-size:18px;text-align:center; padding- left:30px;">Days    Hr    Min    Sec</td>
      </tr>
      <tr>
        <td style="font-size:32px; font:bold; text-align:center;padding-left:30px;">
          <div id="counter<?php echo $count;  /* append counter */ ?>">
            <div class="box days">0</div>
            <div class="box hours">0</div>
            <div class="box minutes">0</div>
            <div class="box seconds">0</div>
          </div>
          <script>cdtd(<?php echo $count; /* pass current count */ ?>, new Date("<?php echo $closedate; ?>"));</script>
        </td>
      </tr>
    </table>
  </div>

  <div id="mrp"><strong>MRP : ₹</strong><?php echo $row['mrp']; ?></div>
  <div id="endt"><strong>End Time: ₹</strong><?php echo $row['closing_date']; ?></div>
  <div id="fee"><strong>Bid Fee : ₹</strong><?php echo $row['bid fee']; ?></div>
  <div id="current"><strong>Current Winner</br>↓ </strong></div>
  <div id="buy">
    <table width="271" border="0" cellpadding="0.1">
      <tr>
        <td><img src="image/buy.png" alt="buy now" longdesc="http://buy now" /></td> 
        <td width=110><?php echo $row['currentwinner']; ?></td> 
        <td><img src="image/bid.png" alt="bid now" longdesc="http://bidnow " /></td>
      </tr>
    </table>
  </div>
</div>
<?php
  $count++; // increment counter;
}
?>

NB: The code is untested

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.