0

I have this program which I am trying to produce a a string for a jquery application that stores my data but can not get the string to produce right. This is the program:

if (!$db_selected) {
    die();
    echo "database not selected";
} else {
    $query = "SELECT * FROM `events` WHERE `event_deletion` = 1";
    $result = mysqli_query($conn, $query);
    if (!$result) {
        trigger_error("dbget: ".mysql_error()." in ".$query);
        return false;
    } else {
        while ($row = mysqli_fetch_assoc($result)) {
            $obj = array();
            array_push($obj, $row['event_ID']);
            array_push($obj, $row['event_name']);
            array_push($obj, $row['event_start']);
            array_push($obj, $row['event_end']);
            array_push($xml, $obj);
            //print_r($xml);
        }
        print(generateXML($xml));
    }
}

function generateXML($array) {
    $xmlString = "events: [";
    for ($i = 0; $i < sizeof($array); $i++) {
        $obj = $array[$i];
        //print_r($obj);
        $set = "{
            id: \'" + $obj[0] + "\'," + print($obj[0] + "");
        "title:\'" + $obj[1] + "\'," +
            "start:\'" + $obj[2] + "\'," +
            "end:\'" + $obj[3] + "\'";
        //print($set + "\r\n");
        $xmlString = $xmlString + $set;
    }
    return $xmlString = $xmlString + "]";
}

I need the string at the end to look like this format with the id, name, start, and end.

events: [{
    title: 'All Day Event',
    start: '2015-02-01'
}, {
    title: 'Long Event',
    start: '2015-02-07',
    end: '2015-02-10'
}, {
    id: 999,
    title: 'Repeating Event',
    start: '2015-02-09T16:00:00'
}, {
    id: 999,
    title: 'Repeating Event',
    start: '2015-02-16T16:00:00'
},
]

I need the string to look like this above but I get this when I run the program:

connection made2345671044

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
James Mars
  • 21
  • 5
  • 2
    [`json_encode()`](http://php.net/manual/en/function.json-encode.php). You should never try to write JSON out by hand, and your current JSON is not valid. – Jay Blanchard Aug 11 '15 at 17:44
  • try http://php.net/manual/en/function.json-encode.php, it will create json for you no metter there is multi dimensional array. create array in query result output loop and then then json_encode, on jquery hand read this http://stackoverflow.com/questions/4935632/parse-json-in-javascript – Farhan Aug 11 '15 at 17:50
  • Will it create it like I need it to be in the example? – James Mars Aug 11 '15 at 17:52
  • yeah it will, you just need to creat array in php in such way what you want in json – Farhan Aug 11 '15 at 17:53
  • I am using this for jquery full calendar and when I print the json_encode all it uses is [] and not the {} around the objects. – James Mars Aug 11 '15 at 17:55
  • Do you want to pass string not json? also what is the string you are getting from generateXML() – Farhan Aug 11 '15 at 18:03
  • @JamesMars, there's a parameter for that. Try JSON_FORCE_OBJECT. http://php.net/manual/en/json.constants.php – sisve Aug 11 '15 at 18:26
  • after the whole script is ran I get connection made2345671044 – James Mars Aug 11 '15 at 18:45

0 Answers0