0
<?php 
       $sql = "SELECT * FROM expense WHERE userid = $userid";

$expense_date= array();  //array declared to be used below

$amount = array();// array declared


$result = mysqli_query($conn, $sql);

 if(!$result){
      die("query failed"); // query failed due to no connection or error in query
  } else {
 while( $row = mysqli_fetch_assoc($result)){ // fetches infromation 

        array_push($amount, $row["amount"]); //pushes my distances whic are returned from the query from the database into an array

        $date_entered = ( date("d-n-y", strtotime($row["timestamp"])));

        array_push($expense_date, $date_entered);//pushes date travelled

      } 
  }

//$arr = array('distance'=>$dist_covered, 'dateTravel'=>$travel_date);
//print_r(json_encode($arr));    ------ make sure distance are being inserted into the database

    echo $date_entered; ?>

<script type="text/javascript">

var unavailableDates = <?php print_r(json_encode($date_entered))?>;

function unavailable(date) {

    dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();

    if($.inArray(dmy, unavailableDates) == -1) {

        return [true, ""];
    }
    else {
        return [false, "", "Unavailable"];
    }
}

$(function() {
    $( "#Datepicker1" ).datepicker({
        numberOfMonths:2,
        beforeShowDay: unavailable
    }); 
});
</script>

I am trying to get the unavailableDates variable to pick out a date from the database. the variable which is in php is echoing out the date in the same format. but the variable is not recognizing it. If i enter the dates manually it works.

2
  • What happens when you remove print_r ? Commented Feb 11, 2016 at 14:49
  • You probably want echo instead of print_r Commented Feb 11, 2016 at 14:50

2 Answers 2

3
var unavailableDates = <?php echo json_encode($date_entered)); ?> || '';

Both print_r() and var_dump() are used to display data for debugging purposes. print and echo are used for outputting strings. Using echo is most common.

$d = date("d-n-y");
print_r($d);
var_dump($d);
echo $d;

Will produce:

11-2-16
string(7) "11-2-16"
11-2-16

So, even though you're using print_r() instead of echo, the result is the same output. This is because it's a string variable in this case. Or is this an array of dates?

You may have another issue in your code. Are you getting any console errors?

I may be missing something. Is unavailableDates supposed to be an array of dates? In that case, you might have your variables mixed up a bit. See the array_push() PHP function.

See http://php.net/manual/en/function.array-push.php

array_push($expenseDates, $date_entered);//pushes date traveled

Then...

var unavailableDates = <?php echo (!empty($expenseDates)) ? json_encode($expenseDates) || []; ?>;
Sign up to request clarification or add additional context in comments.

6 Comments

and print_r's output is NOT valid javascript, so this simply kills the entire <script> block due to JS syntax errors.
@MarcB the print_r call is in PHP tags
@ajmedway: so? just because that's valid php doesn't mean the OUTPUT of that php is valid for the context it's going into. OP's code is outputting into a place in the document where a javascript value has to be, and print_r output will NOT generate a valid javascript value.
@MarcB: OP indicates this is being run from a .php file. Anything in php tags is parsed as PHP code, if he echo's into that juncture of the script like i this answer it will render as the assigned value of that javascript variable.
@ajmedway: and my comment was to expand on why switching from echo to print_r was necessary.
|
0

Right, your code here is a bit of a mess to be honest, lots of obvious issues which I have corrected below:

<?php
$sql = "SELECT * FROM expense WHERE userid = $userid";
$expense_date = array(); //array declared to be used below
$amount = array(); // array declared
$result = mysqli_query($conn, $sql);
if (!$result) {
    die("query failed"); // query failed due to no connection or error in query
} else {
    while ($row = mysqli_fetch_assoc($result)) {
        $amount[] = $row["amount"];
        $date_entered = date("d-m-y", $row["timestamp"]); #CORRECTED - ONLY FEED IN THE TIMESTAMP 
        $expense_date[] = $date_entered;
    }
}
//echo $date_entered; #WHY IS THIS BEING ECHOED OUT HERE FROM PHP?
?>

<script type="text/javascript">
    var unavailableDates = '<?= $date_entered ?>';
    function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if ($.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        }
        else {
            return [false, "", "Unavailable"];
        }
    }
    $(function () {
        $("#Datepicker1").datepicker({
            numberOfMonths: 2,
            //beforeShowDay: unavailable # WHAT IS THIS?
            beforeShowDay: unavailable('MISSING_DATE')
        });
    });
</script>
  1. Is this all happening in 1 .php file?
  2. Look in the datepicker code - looks like that is meant to be a call to the unavailable() .js function? What date is that meant to call?

3 Comments

nope it doesn't work. i use the echo as it echoes out the date so i know the variable is containing the date
@johnabraham it's not going to work yet mate - this is pseudo code for now - please look at the other questions too
@johnabraham have you looked at my other questions? There is more than just the one issue here - at least 5 or 6 things from your original code need to be corrected for this to work properly. If you feedback I can help you out.

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.