views:

150

answers:

1

Hello,

I need to convert some code done by someone else, to work in my mvc model

It is using some functions like EOD that I don't understand. Does that still work in a class?

Primarely, my question focusus on the json output.

The old code does not use the php json_encode function, but outputs it directly like this

?>
{
        "username": "<?php echo $_SESSION['username'];?>",
        "items": [
            <?php echo $items;?>
        ]
}

<?php

I would do it like this, but I need to be sure it's right for the items part

header('Content-type: application/json'); 

$output = array("username"=> isset( $_SESSION['username'] ) ? $_SESSION['username'] : "?",
                "items"=>$items
                );
$this->content = json_encode($output);

This is some background on how the $items is made. An item is stored like this:

$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
                       {
            "s": "1",
            "f": "{$to}",
            "m": "{$messagesan}"
       },
EOD;

and it is put in the $items variable like this

$items = '';


if ( !empty($_SESSION['openChatBoxes'] ) ) {
        foreach ( $_SESSION['openChatBoxes'] as $chatbox => $void ) {
            $items .= $this->chatBoxSession($chatbox);
        }
    }

//The chatBoxSession() function takes an item from the  $_SESSION['chatHistory'] array and returns it.

I hope this was somewhat clear enough? The php manual warns that in some cases you don't get an array output, instead you get an object. So, with the EOD syntax, I am not really sure.

It could save me some time if I know some things are doing what they supposed too, and giving the right output.

thanks, Richard

+1  A: 

This is called a heredoc. It works as though all the text inside is a single string. Do not use it if you are trying to generate JSON. Instead, use the fact that the statement is not done until it hits a semicolon:

$somevar = {
  "s": "1",
  "f": "{$to}",
  "m": "{$messagesan}"
};
Ignacio Vazquez-Abrams
thanks, and adding the last comma after the lastclosing curl, you forgot that. But am I also using the correct way to output the items.I json_encode adding the straight curls [] itself, or do I have to do that?
Rich
What you wrote in your answer is a single object, right?But the ajax function expects an array of items.Would this be correct "items"=>$items OR do I need to do"items"=>array($items)?????
Rich
Normally you create a structure containing linear arrays and associative arrays, and call `json_encode()` on that to turn it into JSON all at once. Whether or not you need to call `array()` depends on what structure you want.
Ignacio Vazquez-Abrams
oh, damn, I see what you mean, I can't use json_encode this way if I have already readymade objects contained in the session array. I have to rethink the situation a bit.
Rich
Would this be correct then, if I keep with the syntax$output = <<<EOD "username": "$username", "items": [$items] EOD;
Rich
Heredoc syntax always results in a string.
Ignacio Vazquez-Abrams