-5

I have a php variable called $usershours that contains an array, and I want to echo it inside Javascript code. How do I do that?

I tried This, but it didn't work:

$usershours = "
data.addRow([ new Date(2012,4,1),50119]).+'<br>+'.
data.addRow([ new Date(2012,4,2),242575])";

<script type="text/javascript">
document.write($arrayholder);
</script>

I want the output to be like this:

data.addRow([ new Date(2012,4,1),50119]) 
data.addRow([ new Date(2012,4,2),242575])

PS - I need this format because This is the format that Google charts API accept to render charts correctly.

20
  • 3
    PHP is executing on the server and sending the browser HTML and JavaScript. Where are you expecting this to take place? Commented Aug 22, 2012 at 2:01
  • wrap it in <script> ---- the JavaScript Commented Aug 22, 2012 at 2:02
  • 2
    This isn't a game. $usershours = "data.addRow([ new Date(2012,4,1),50119]) data.addRow([ new Date(2012,4,2),242575]) "; echo $usersHours; is the output you asked for. You will not see anything on the screen, as the resulting JavaScript just calls a function on the data object. If there is more you are expecting, you need to write a different question. Commented Aug 22, 2012 at 2:15
  • 2
    It's not tricky at all. Put that between <script> and </script> tags. Commented Aug 22, 2012 at 2:18
  • 1
    Then wrap <script> tags around it: ?><script>data.addRow([ new Date(2012,4,1),50119]) data.addRow([ new Date(2012,4,2),242575])</script><?php Commented Aug 22, 2012 at 2:34

4 Answers 4

4

From what I gather, you want this code run by javascript on the client machine:

data.addRow([ new Date(2012,4,1),50119]) 
data.addRow([ new Date(2012,4,2),242575])

Simply:

<script type="text/javascript">
<?php 
$usershours = "
data.addRow([ new Date(2012,4,1),50119])
data.addRow([ new Date(2012,4,2),242575])";

echo $usershours;
?>
</script>
6
  • Thanks a lot for trying, but it gaves me empty result. Commented Aug 22, 2012 at 2:23
  • 1
    Please define "empty result", because those words obviously mean something different to you than to everyone else. This code does exactly what you asked. We're not "trying" at some game; your question has been answered. Commented Aug 22, 2012 at 2:24
  • @DanGrossman, you are correct, but what I meant with my question was to display it on the webpage, because I will use the result for google chart API to display some graphs. thanks again man! Commented Aug 22, 2012 at 2:31
  • @DanGrossman Empty result mean, white page, but you will find the code in the source code. Commented Aug 22, 2012 at 2:41
  • 1
    A white page, with the data.addRow statements inside some JavaScript, is exactly what you asked for. You didn't ask for any text or HTML to be written. Commented Aug 22, 2012 at 2:42
3

Though messing PHP with JS is far from an optimal approach, this will keep your code a bit tidier as well.

Let's pretend your data comes from an array [fetched from a database or a file] such composed:

$data = array(
    array("date" => "2012-04-01", "id" => 50119),
    array("date" => "2012-04-02", "id" => 242575)
);

Through json_encode($data) you get this output:

[
  { date : "2012-04-01", id : 50119},
  { date : "2012-04-02", id : 242575}
]

Now you know how to translate your data from a PHP structure to a Javascript one, it's just a matter of writing:

<script>

  var serverData = <?php echo json_encode($data) ?>;

  for(var i = 0; i < serverData.length; i++)
  {

     data.addRow([ new Date( serverData[i].date ), serverData[i].id ]);

  }

</script>

Granted that data.addRow() is a function declared somewhere in you JS code.

-1

Simply use var arrayholder = <?php json_encode($usershours);?>; if arrayholder has to be output as string (and not as code)

4
  • 2
    Semicolons are not required in JavaScript as long as you avoid a few ambiguous syntax gotchas. Commented Aug 22, 2012 at 2:13
  • 1. and 2. are irrelevant and false respectively. Commented Aug 22, 2012 at 2:14
  • @Dan Grossman: Yeah as far as I know only expressions need that semicolon ;(...), ;[...] and ;/.../, and also "use strict"; for some reason. Commented Aug 22, 2012 at 2:18
  • Corrected. Thanks for ponting my errors out. Commented Aug 22, 2012 at 2:59
-2

General advice: do not try to render valid JavaScript from PHP. It will eventually bite you, some alternate solutions: embedding the data in an HTML element and extracting it via JavaScript and Ajax

Having said that

?>
(function () {
   console.log(<?= json_encode($phpVar); ?>);
}());
<?

is how I do it when I'm ok with shooting myself in the foot

19
  • By bite you I mean render an unexpected error in the middle of your JavaScript, due to some configuration you can't control. Commented Aug 22, 2012 at 2:22
  • Why would you want to embed the data in an HTML element when you can just echo it out in js? Commented Aug 22, 2012 at 2:23
  • That's a pretty arbitrary rule to impose on yourself, also pretty irrelevant to the question. Commented Aug 22, 2012 at 2:24
  • 3
    JavaScript crashing is no more or less damaging to your app than malformed HTML which your JavaScript depends on. If you are writing software which outputs PHP errors, it's because you're writing bad software, and you need to fix it regardless of whether the errors appear in your HTML or in your JavaScript. If you want your software to work you need to write error-free software, not try to hide the errors in HTML because "HTML doesn't crash". That is an utterly ridiculous attitude and an incredibly silly programming practice. Commented Aug 22, 2012 at 2:30
  • 1
    @Mythril The solution is to write good code and make a reasonably modern installation of PHP a requirement for running your software. Writing software to run on broken hosts is incredibly self-defeating. I haven't said anything about your skill, but I will say that worrying about only mixing PHP with your HTML because "you can put escape sequences that prevent the DOM from being munged ( <![CDATA[]]> )" is a horrendous way of designing software. Commented Aug 22, 2012 at 2:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.