0

I have these php variables:

$n= $json['health'];
$n2= $json2['health'];

The output are numbers.

Here is the jquery script:

<script>
$(function() {
var valMap = [0, 25, 50, 100, 250, 500];
$("#slider-range").slider({
    min: 1,
    max: valMap.length - 1,
    value: 0,
    slide: function(event, ui) {                        
        $("#amount").val(valMap[ui.value]);                
    }       
});
//$("#amount").val(valMap[ui.value]);
})
</script>

I tried to get the vars in the script as my php variables like that:

var valMap = ['<?php echo($n); echo($n2);?>';];

How can I set the variables in the right way?

7
  • what do $n and $n2 look like? can you show a var_dump($n)? Commented May 12, 2018 at 15:51
  • Two Numbers. 152 and 204. This is the php output. Commented May 12, 2018 at 15:53
  • 1
    then it would be var valMap = ['<?php echo $n .", " . $n2; ?>']; Commented May 12, 2018 at 15:54
  • apart from the missing , you also had a ; to much here: ;?>';]; Commented May 12, 2018 at 15:55
  • @Jeff Why adding the single quote inside the var valMap = [ ] Commented May 12, 2018 at 15:58

3 Answers 3

1

You should try something like that:

var valMap = [<?php echo "$n, $n2"; ?>];
Sign up to request clarification or add additional context in comments.

Comments

0

Declare an array with these variables in your PHP code.

$n_arr=array($json['health'],$json2['health']);

Then in your script use that array like this.

var valMap = <?php echo json_encode($n_arr);  ?>;

This should be the tidy way to do it!

Comments

0

You should try something like that:

var valMap = [<?php echo $n.",".$n2; ?>];

3 Comments

No, this is going to print: var valMap = [3..67];
sorry there was an issue in echo. I have changed it now. Please try again with my changes
Yes, now it's right, but this answer is very similar with my own answer, and this answer is already (almost) in a comment...

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.