0

Hi I'm trying to make an array " events" in javascript that is filled with objects of two properties {Title: , Date:}

But, the value of those two properties is brought from the database using PHP!

Here's my code but I don't know why it doesn't work..

var events = [ 
    <?php 



//Connect to mysql server
//Select database

    $result=mysql_query("SELECT * FROM event");
    $count = mysql_num_rows($result);
    if($count>0){
    for($i=0;$i<$count-1;$i++){
    $row= mysql_fetch_row($result);

    echo "{ Title: ".$row['title'].", Date: ".$row['date']." },";
        }

    $row= mysql_fetch_row($result);
    echo "{ Title: ".$row['title'].", Date: ".$row['date']." }";

    }
    ?>];
9
  • 1
    json_encode() would make this much easier. Commented Dec 13, 2012 at 23:29
  • This code is wrong on so many levels that it's hard to describe exactly how. Take a step back and try one thing at a time. Commented Dec 13, 2012 at 23:30
  • @alex Sorry but where should it be? Commented Dec 13, 2012 at 23:31
  • Do you really mean the following: $i<$count-1 If $count is 1, you won't get any results. Commented Dec 13, 2012 at 23:31
  • @Jon I'm a beginner and I'm trying to collect things together. I'll try again. :( Commented Dec 13, 2012 at 23:31

1 Answer 1

1

Let's first take the building of your results:

Use array_push to create your data from your SQL (this is called an associative arrays:

var events = 
<?php 
  $json = array();
  $result=mysql_query("SELECT * FROM event");
  $count = mysql_num_rows($result);
  for($i=0;$i<$count;$i++) {
    $row= mysql_fetch_row($result);
    $item = array();
    $item['Title'] = $row['title'];
    $item['Date'] = $row['date'];
    $json[] = $item;
  }

Now echo the results as JSON using json_encode:

  echo json_encode($json);
?>;

Tested in PHP 5.3.2

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

6 Comments

It didn't work unfortunately. It only works with this var events = [ { Title: "Five K for charity", Date: new Date("02/13/2011") }, { Title: "Dinner", Date: new Date("02/25/2011") }, { Title: "Meeting with manager", Date: new Date("12/01/2012") } ]; but when I try php nothing happens :( Thank you anyway.
Can you add an example of the JSON you want it to return?
like this ? var events = [ { Title: "Five K for charity", Date: new Date("02/13/2011") }] Title and Date are retrieved from database.
Sorry, I understand that part, maybe I'm having trouble understanding where your code was breaking. Are you having trouble before you get to the encode part? Your question title suggests that you are having trouble encoding an array into JavaScript
can you look at the /var/log/apache/error.log it will have the last PHP error at the bottom
|

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.