PHP Json error when displaying information with two repeat structures

Asked

Viewed 68 times

0

I’m starting the following json:

{
"issues": [
    {
        "title": "Falha no servidor DNS",
        "currentstate": "Em Andamento",
        "description": "Texto Descrição",
        "starttime": "24 de Setembro de 2015 as 13:30",
        "endtime": "24 de Setembro de 2015 as 13:50",
        "duration": "20",
        "updates": [
            {
                "state": "Em Andamento",
                "autor": "isUFV.online Robot",
                "description": "A equipe tecnica recebeu o chamado",
                "updatetime": "24 de Setembro de 2015 as 13:35"
            },
            {
                "state": "Falha Critica",
                "autor": "isUFV.online Robot",
                "description": "A equipe tecnica recebeu o chamado",
                "updatetime": "24 de Setembro de 2015 as 13:35"
            },
            {
                "state": "Offline",
                "autor": "isUFV.online Robot",
                "description": "A equipe tecnica recebeu o chamado",
                "updatetime": "24 de Setembro de 2015 as 13:35"
            }
        ],
        "link": "http://www.bewebdeveloper.com/tutorial-about-how-to-create-an-rss-feed-with-php-and-mysql"
    },
    {
        "title": "Rompimento na fibra de comunicação BH-OP",
        "currentstate": "Solucionado",
        "description": "Texto Descrição",
        "starttime": "24 de Setembro de 2015 as 13:30",
        "endtime": "24 de Setembro de 2015 as 13:50",
        "duration": "20",
        "updates": [
            {
                "state": "Em Andamento",
                "autor": "isUFV.online Robot",
                "description": "A equipe tecnica recebeu o chamado",
                "updatetime": "24 de Setembro de 2015 as 13:35"
            },
            {
                "state": "Em Andamento",
                "autor": "isUFV.online Robot",
                "description": "A equipe tecnica recebeu o chamado",
                "updatetime": "24 de Setembro de 2015 as 13:35"
            }
        ],
        "link": "http://www.bewebdeveloper.com/tutorial-about-how-to-create-an-rss-feed-with-php-and-mysql"
    },
    {
        "title": "Detectado erro no",
        "currentstate": "Falha",
        "description": "Texto Descrição",
        "starttime": "24 de Setembro de 2015 as 13:30",
        "endtime": "24 de Setembro de 2015 as 13:50",
        "duration": "20",
        "updates": [
            {
                "state": "Em Andamento",
                "autor": "isUFV.online Robot",
                "description": "A equipe tecnica recebeu o chamado",
                "updatetime": "24 de Setembro de 2015 as 13:35"
            },
            {
                "state": "Em Andamento",
                "autor": "isUFV.online Robot",
                "description": "A equipe tecnica recebeu o chamado",
                "updatetime": "24 de Setembro de 2015 as 13:35"
            }
        ],
        "link": "la"
    }
]

}

And my PHP code and this:

<?php
    foreach ($json as $item) {
        if ($item->currentstate=='Solucionado') {
            $status = 'green';
        } elseif ($item->currentstate=='Em Andamento') {
            $status = 'blue';
        } elseif ($item->currentstate == 'Falha Critica') {
            $status = 'orange';
        } else {
            $status = 'red';
        }
?>

<h3>
    <div class="bordertop_lightblue item_case">
        <div class="clearfix case_style borderleft_<?php echo $status; ?>">
            <div class="float_right">
                <div class="txt_grey txt_ft14">
                    <?php echo $item->currentstate; ?>
                        :&nbsp;
                        <abbr class="timestamp" title="<?php echo $item->starttime; ?>">
                            <?php echo $item->starttime; ?>
                        </abbr>
                </div>
            </div>

            <div class="_4bl9">
                <a href="<?php echo $item->link; ?>">
                <span class="txt_bold"><?php echo $item->title; ?> - </span>
                <span class="txt_<?php echo $status; ?> txt_ft14">
                    <?php echo $item->currentstate; ?>
                </span></a>
                <div class="txt_grey txt_justify">
                    <a href="<?php echo $item->link; ?>">
                        <?php echo $item->description; ?>
                    </a></div>
                </div>
        </div>
    </div>
</h3>

<?php
    if (property_exists($item->updates)) {
        $updates = $item->updates;
        foreach ($updates as $iUpdate) {
            if ($iUpdate->state=='Solucionado') {
                $status = 'green';
            } elseif ($iUpdate->state=='Em Andamento') {
                $status = 'blue';
            } elseif ($iUpdate->state == 'Falha Critica') {
                $status = 'orange';
            } else {
                $status = 'red';
            }
?>

<div class="bordertop_lightblue item_case">
    <div class="clearfix case_style borderleft_<?php echo $status; ?>">
        <div class="float_right">
            <div class="txt_grey txt_ft14">
                <?php echo $iUpdate->state; ?>:&nbsp;
                    <abbr class="timestamp" title="<?php echo $iUpdate->updatetime; ?>">
                        <?php echo $iUpdate->updatetime; ?>
                    </abbr>
            </div>
        </div>
        <div class="_4bl9">
            <div class="txt_bold">Test</div>
                <div class="txt_grey txt_justify">
                    <?php echo $iUpdate->description; ?>
                </div>
            </div>
        </div>
    </div>
<?php
        }
    }
}

echo $jsonError = json_last_error();
?>

PHP is showing the following error:

Notice: Trying to get property of non-object in /app/novo/index.php on line 873 Notice: Trying to get property of non-object in /app/novo/index.php on line 876 Notice: Trying to get property of non-object in /app/novo/index.php on line 879
Notice: Trying to get property of non-object in /app/novo/index.php on line 891 :  Notice: Trying to get property of non-object in /app/novo/index.php on line 894
Notice: Trying to get property of non-object in /app/novo/index.php on line 899 - Notice: Trying to get property of non-object in /app/novo/index.php on line 902
Notice: Trying to get property of non-object in /app/novo/index.php on line 906
Notice: Trying to get property of non-object in /app/novo/index.php on line 913 Warning: property_exists() expects exactly 2 parameters, 1 given in /app/novo/index.php on line 913 0
  • With the following foreach ($json['issues'] as $item) { code I can make the first structure work but the second one still fails foreach ($json['issues']['updates'] as $item) {

  • 1

    From the code you presented, json is being saved as a string, and $item does not exist in this context, because foreach is not realized if the $json variable is a string and not an array. Try to use json_decode() to use foreach in your json data.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.