1

I am getting an Array out of a database and want to display it. This is my approach so far:

for ($i = 0; $i < count($results); $i++)
{
 $output .= "<tr>
 <td>".$results[$i]."</td>
 </tr>";
}
print_r($output);

This works fine but the values inside the array look like this: "6.212,50"

So they are strings but I need to unformat them to do further calculations for that I use numeral.js which works like this:

numeral().unformat('6.212,50') //outputs 6212.50

How can I run this inside the PHP loop? I already tried this:

for ($i = 0; $i < count($results); $i++)
{
 $output .= "<tr>
 <td><script>numeral().unformat('$results[$i]')</script></td>
 </tr>";
}
print_r($output);

But that displays nothing.

2 Answers 2

3

that is because the javascript is interpreted but is nowhere displaying it.try this

for ($i = 0; $i < count($results); $i++)
{
  $output .= "<tr><td><script>document.write(numeral().unformat('".$results[$i]."'));</script></td>
</tr>";
}

document.write will print the result in javascript.

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

1 Comment

perfect! Thank you :)
2

The script is run on the client side, and script tags don't return something into the table. They just execute the JavaScript and think that it is done.

Another thing is that html is just text. If you parse the float and write it to the table, the content will still be strings.

If you want to do calculations on the client side, you write a Javascript that finds the values, parses them into floats, and stores them inside the script for calculation.

If you want to print the sum or something, then set the content of some html tag to the sum. But it will be set to its string value.

If you only want to parse the number to get rid of some dots, I suggest writing a php function and parse it on the server before writing it to the view.

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.