0

I am writing a inline-PHP snippet that is grabbing values out of a query.
Here is the code I use:

$(".menu-price-slider").each(function(index) {
       console.log(timeToMinutes("<?php echo $query_results['d'.(++$d).'_o']?>"));
});

This code is giving me $query_results[1] every time, what I want is a counter that is increasing everytime this code gets executed.
$query_results[1], $query_results[2], $query_results[3], $query_results[4], etc.

Thanks in advance

6
  • 1
    Is timeToMinutes a PHP method, or is it a Javascript method? Commented Jan 27, 2015 at 15:20
  • 1
    Make sure $d is initialized (presumeabe to 0) before, and in same scope as, your executions. Commented Jan 27, 2015 at 15:23
  • What are you actually trying to do? You are not showing much code context here. Is there some sort of a loop in play? You do realize that one PHP has written that out into the javascript parameter in the source code it is totally static (in other words PHP doesn't run in your browser)? Commented Jan 27, 2015 at 15:30
  • @MikeBrant I updated the code alittle so you get the context Commented Jan 27, 2015 at 15:36
  • Mike's point is that echoing PHP instructions from javascript is a waste of time, because the javascript is executed in the browser, after PHP has already run on the server. Commented Jan 27, 2015 at 15:49

2 Answers 2

2

OK. If you are expecting to get a different value for parameter of timeToMinutes with each iteration, then you are mistaken. I think you need to get a better understand for how PHP and javascript work. PHP is only working at the time of page render. One the page is rendered, javascript would work within the browser. If you didn't put all the values you need from PHP into the javascript source, you have no way to get to them after the page is rendered, short of using AJAX techniques you pull the data in after initial page render.

I might suggest a technique like this:

// pre-populate array of values from PHP
// here PHP $query_results must be numerically indexed array
// there should be equal number of elements in this array and
// .menu-price-slider DOM elements
var timeToMinutesParams = <?php echo json_encode($query_results); ?>;

// iterate through DOM elements,
// using index of element to get matching value from timeToMinutesParams
$(".menu-price-slider").each(function(index) {
       console.log(timeToMinutes(timeToMinutesParams[index]));
});
Sign up to request clarification or add additional context in comments.

2 Comments

Worked! Here is the code: var opening_array = [<?php for($i = 1; $i < 8; $i++) echo '"'.$query_results["d{$i}_o"].'", '?>];
Output: ["10:00:00", "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00"]
0

You could make a file initialize it with 0 if it does not exist and if it exists read from it and at the end increase the number in the file.

4 Comments

Is it possible to do this with a shorthand-if? for example $d = ($d ? $d++ : 0)
PHP does not support something like this sadly... :(
Couldn't you do $d = (isset($d) ? $d++ : 0);?
@AlecDeitloff Tried it, sadly, isset($d) is always false

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.