Как передать массив данных JSON MVC с PHP? - PullRequest
0 голосов
/ 01 июля 2019

Я получаю файл JSON с сервера vuforia. Я хочу показать это с концепцией MVC. Я хочу установить данные таблицы из JSON. в классе GetTarget я хочу показать JSON для моей таблицы. получить идентификатор из класса GetAlltargets. как получить results из класса GetAllTargets для помещения в переменную $targetId в классе GetTarget

Моя модель, чтобы получить маркер и объект:

class GetTarget{

    private $targetId   = "target id get from JSON GetAllTargets";

function GetTarget(){

        $this->requestPath = $this->requestPath . $this->targetId;

        $this->execGetTarget();
    }

    private function execGetTarget(){

        $this->request = new HTTP_Request2();
        $this->request->setMethod( HTTP_Request2::METHOD_GET );

        $this->request->setConfig(array(
                'ssl_verify_peer' => false
        ));

        $this->request->setURL( $this->url . $this->requestPath );

        // Define the Date and Authentication headers
        $this->setHeaders();


        try {

            $response = $this->request->send();

            if (200 == $response->getStatus()) {
                // echo $response->getBody();
                $hasil = json_decode($response->getBody());
                echo '<pre>'; print_r($hasil); echo '<pre>';
                echo "hasil" . $hasil->result_code;
            } else {
                echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
                        $response->getReasonPhrase(). ' ' . $response->getBody();
            }
        } catch (HTTP_Request2_Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }


    }

Это JSON от модели GetTarget

stdClass Object
(
    [result_code] => Success
    [transaction_id] => 5cb64dcae1ad44c0ab0f268b02ef47ca
    [target_record] => stdClass Object
        (
            [target_id] => f248175c2a6f4bcf9d9df6fe3e367ab5
            [active_flag] => 1
            [name] => markerbasil
            [width] => 1
            [tracking_rating] => 5
            [reco_rating] => 
        )

    [status] => success
)

Моя модель, чтобы получить ID:

public function GetAllTargets()
{
    $this->requestPath = $this->requestPath;
    $this->execGetAllTargets();
}
public function execGetAllTargets()
{
    $this->request = new HTTP_Request2();
    $this->request->setMethod(HTTP_Request2::METHOD_GET);
    $this->request->setConfig([
        'ssl_verify_peer' => false,
    ]);
    $this->request->setURL($this->url . $this->requestPath);
    // Define the Date and Authentication headers
    $this->setHeaders();
    try {
        $response = $this->request->send();
        if (200 == $response->getStatus()) {
            //echo $response->getBody();
            $hasil = json_decode($response->getBody());
            return $hasil;
            //echo "hasil" . $hasil->result_code;
        } else {
            echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
            $response->getReasonPhrase() . ' ' . $response->getBody();
        }
    } catch (HTTP_Request2_Exception $e) {
        echo 'Error: ' . $e->getMessage();
    }
}

Это JSON от модели GetAllTargets

stdClassObject(
    [result_code]    => Success
    [transaction_id] => 42101c8ef9eb4df4b36f15872c90cfba
    [results]        => [
        [0] => f248175c2a6f4bcf9d9df6fe3e367ab5
        [1] => 6c104d59194e40e5826b692c0d6198f1
        [2] => 9e69ab58ad6d42b68c2da25e80c0e36c]
)

Мой контроллер:

public function index()
{
    $data['id'] = $this->model('GetAllTargets');
    $this->view('templates/header');
    $this->view('home/index', $data);
    $this->view('templates/footer');
}

My View:

<tr>
    <th>ID</th>
    <th>Marker</th>
    <th>Objek</th>
    <th>Action</th>
</tr>
</thead>
<tbody>
    <tr>
        <td>
            <?php  foreach( $data['id']->results as $row) { ?> 
                <td>
                <?php print_r($row[0]); ?>
                </td>
            <?php } ?>
        </td>
        <td>Decker</td>
        <td>Regional Director</td>
        <td>Edinburgh</td>
    </tr>
</tbody>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...