0

Having some problems with a javascript code, when i try to pass a php string to it, it's a date timer for counting, and it's working if i give the static value but if i have a php string in it it gives only zeros.

<?php $r="June 7, 2087 15:03:25"; ?>
       <script type="text/javascript">
      $(function() {
        var endDate = "<?php echo $r;?>";

        $('.countdown.simple').countdown({ date: endDate });
      });
    </script>

you can check the code right here: http://jsbin.com/EboVeCO/1/edit , if you comment the $r and put the date exactly in the JS like it is, it will work, else is not working :(

What's the matter? why isn't it working?

UPDATE:

<?php foreach ($row as $r){
$rr = $r->release_date;
$rrr = date("F d, Y H:i:s", strtotime($rr));

<script type="text/javascript">
      $(function() {
        var endDate = "<? echo $rrr; ?>";

        $('.countdown.simple').countdown({ date: endDate });
      });
    </script>
echo ' <div class="countdown simple"></div>';
}

THIS wher $row comes from:

public function get_things() {
    $q = $this->db->query("SELECT * from s_data");
    if($q->num_rows() > 0){
    foreach($q->result() as $row){
    $data[] = $row;
    }
    return $data;
    }
    }

Salut!

0

3 Answers 3

2

Your code has a syntax error, unexpected ';' on line 4. echo needs something that it can print, by itself, its illegal.

Sign up to request clarification or add additional context in comments.

6 Comments

sorry , was a problem of pasting, there is $r there, but won't work :(
comment that might clear things : "the idea is that is not working on my server, i'm passing it like in the jsbin but it give only zeros, $r it's in a foreach so there's more strings of $r there, when i firebug it, it shows that it's changing correct in the <script> the php $r but the displaying not working "
jsbin refuses to show anything other than what seems like a barell with a rotating lid.
I'm just not getting that if on my server the endDate gets the correct date value, why it's not counting down.. i don't know why it breaks :(
update: Something happened, as i said $r is part of foreach, and the first values where 00 00 00, after selecting form DB the once that are > now() now it showing the counter working, but it seems to be grabbing the $r for only the first one and showing the same countdown time for all even if the $r is different, it's garbing only the first $r, what can i do? so it can work for each $r?
|
1

I think your <?php echo ;?> doesn't echo anything, thus endDate is just an empty string, which might cause problems for countdown().

If you just change endDate to a valid date it works fine.

var endDate = "June 7, 2087 15:03:25";

So, the problem is with your echo. I woudl try this:

<?php echo $r;?>

4 Comments

Note that jsbin won't evaluate the PHP, so you'll have to try the PHP fix on your own environment.
sorry , was a problem of pasting, there is $r there, but won't work :(
So, not a javascript problem then, rather a PHP problem.
Paul, the idea is that is not working on my server, i'm passing it like in the jsbin but it give only zeros, $r it's in a foreach so there's more strings of $r there, when i firebug it, it shows that it's changing correct in the <script> the php $r but the displaying not working ...
0

You're not exiting php before writing js. Also, you need to declare php in the <? part or it may be interpreted as something else, like xml. It should be closer to this:

<?php $i = 0; foreach ($row as $r){
$rrr = date("F d, Y H:i:s", strtotime($r->release_date));
// try print_r($rrr) here to see if your date is actually a valid string
?>
<script type="text/javascript">
      $(function() {
        var endDate = "<?php echo $rrr; ?>";

        $('#counter<?php echo $i; ?>').countdown({ date: endDate });
      });
    </script>
<div id="counter<?php echo $i; ?>" class="countdown simple"></div>
<?php
$i ++; }
?>

Although in a properly written example the js would likely not be written in the loop. You could handle the js in a separate file and the date could be set as a data attribute in the countdown simple element.

Also, your fetch function doesn't return anything if there are no results. So you may want to put this whole block in a conditional that only fires if $row is populated.

EDIT:

Try to reorganize this...

<?php foreach ($row as $r){
    $rrr = date("F d, Y H:i:s", strtotime($r->release_date));
    ?>
    <div class="countdown simple" data-date="<?php echo $rrr; ?>"></div>
<?php
    }
?>

<script type="text/javascript">
   $(function() {
      $('.countdown.simple').each(function(){
          var endDate = this.data('date');
          this.countdown({date: endDate});
      });
   });
</script>

14 Comments

Kai, thanks for posting an answer, not working, tried. the idea is that the endDate is replaced if i view the sourcevar endDate = "October 25, 2013 00:00:00";, but the counter not working :(
I think it is a copy paste error again, otherwise it wouldn't work with the raw string either.
when you print_r($rrr) does it display the expected date?
@KaiQing Something happened, as i said $r is part of foreach, and the first values where 00 00 00, after selecting form DB the once that are > now() now it showing the counter working, but it seems to be grabbing the $r for only the first one and showing the same countdown time for all even if the $r is different, it's garbing only the first $r, what can i do? so it can work for each $r?
What are you using to manage your database? This new behavior may be in your $q->result
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.