-3

i am receiving this error

"Illegal string offset 'code_room'"

when i get data code_room from api. and I realized this section of code in the file is wrong. however I'm not that great in PHP yet and I am wondering if someone can help me re-write this section to eliminate the error. Thanks!

My code

            <?php
            $id_class = $post['id_room_class'];
            $response7 = \Unirest\Request::get($uri.'/room/'.$id_class, $headers); //API URI
            $data7 = $response7->raw_body;    // Unparsed body
            $roomtypeclass = json_decode($data7, true);
            foreach ($roomtypeclass as $get) { ?>
            <button class="btn m-btn--square btn-success" data-dismiss="modal" id="btnRoom" onclick="showData2();">
            <?php echo $get['code_room']; ?>
            </button>
            <?php } ?>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
Sayyid Mahdy
  • 47
  • 2
  • 9

1 Answers1

1

The illegal offset means that the index your are referencing does not exist. So, in this case the 'code_room' index of the array is probably not defined. To prevent the error, change this line:

<?php echo $get['code_room']; ?>

to below code:

<?php
    if (isset($get['code_room']) && !empty($get['code_room'])) {
        echo $get['code_room'];
    }
?>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35