0

I have a variables from my database called MSRP price and I need to loop through the functions for every product showed. What I would like to do is take the Database value and pass it to the variable and output the results on each product on the same page.

See example here

<html>
<body>
  <script>
    function myFunction(msrp) {
      var today = 6;
      var text = "";
      var i;

      for (i = 1; i <= 10; i++) {
        var profits=msrp * ((100-(i*10))/100)

        if (i == today) {          
          text += "Today  $" + profits.toFixed(2) +"<br>";     
        } else {
          text += "Day " + i + "  $ " + profits.toFixed(2) +"<br>";
        }

        document.getElementById("demo").innerHTML = text;
      }
    }
  </script>

  <!-- Products -->
  @foreach ($products as $product)
  <script>
    myFunction($product[msrp]);
  </script>  
  @endforeach

</body>
</html>
4
  • Is your all code in javascript ? it seems like your code is in laravel as well ? Commented Feb 2, 2016 at 18:27
  • the issues I seem to have is with JavaScript i can push the variable correct but i cant seem to get the javascript to output correct. My guess is that this part of code is not correct. 'document.getElementById("demo").innerHTML = text;' Commented Feb 2, 2016 at 18:37
  • No the problem is with myFunction($product[msrp]); Commented Feb 2, 2016 at 18:40
  • so the issue i have is similar to what i got before, i get the same numbers for all the products. I am guessing that it cycles through and the function changes the id demo on all the products. I guess i have to create a unique id for esch one. Commented Feb 2, 2016 at 18:48

2 Answers 2

1

Javascript will not read any variable from Laravel since it's server side. What you have to do is to 'print' the value of the variable in HTML, then call the javascript function with it. Try this and check if it works...

<!-- Products -->
<script>
  @foreach ($products as $product)
    myFunction( {{ $product->msrp }} );
  @endforeach
</script>  
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the support, i was able to get that going and some other stuff fixed. however this is where my issue came all along. The script function gets called only once and it will only show in the first id=demo using the last value pulled from foreach. What ever i do, I cant seem to repeat the result for every single product pulled from foreach.
Do you have several 'demo' elements and want to show different values in every element?
If your answer is yes, then you are overwritting ONE element called demo with the last execution of myFunction
0

You should send value of $product[msrp]as string to myFunction in other vay javascript use it as variable.

<script>
  @foreach (var productin $products)
  {
      myFunction("@$product[msrp]");
  }
</script>

Comments

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.