如何从Php JSON中将模型数据从模型中拉出到控制器

时间:2016-09-08 12:01:47

标签: php arrays json

我试图从model中提取数据,其结果是数组。我试图将返回对象传递给我的controller,并在JSON中回显结果,但似乎无法接收该对象。

我制作的另一个php文件不是MC模式。它的工作原理。

这是我获取objects的代码(这个有效 - 顺序方法):

if($_SERVER['REQUEST_METHOD'] == 'GET'){

     require('dbConnect.php');

     $SQLi_ORG_FEEDS = "SELECT  organizationName,
                                organizationDescription,
                                organizationCategory,
                                organizationCurrentMembers,
                                organizationMaxMembersNo,
                                organizationType,
                                organizationLogo,
                                organizationLogoPath 
                        FROM org_information";

     $query = mysqli_query($dbConnect,$SQLi_ORG_FEEDS) or die("Error".mysqli_error($dbConnect));

     $checkRow = mysqli_num_rows($query);

     $response = array();

     if($checkRow > 0){
         while ($getRecord = mysqli_fetch_array($query)) {
            $response[] = $getRecord;

         }                
            echo json_encode($response);
     }
     else {
            $response['failed'] = 'failed';
            echo json_encode($response);
     }

结果POSTMAN

sample

此方法来自我的model

 public function getAllOrgRegistered(){

            //STATEMENT
                $sql_show_org = "SELECT  organizationName,
                                         organizationDescription,
                                         organizationCategory,
                                         organizationCurrentMembers,
                                         organizationMaxMembersNo,
                                         organizationType,
                                         organizationLogo,
                                         organizationLogoPath 
                                 FROM    org_information";

            //EXECUTE
                $result       = mysqli_query($this->db->connect,$sql_show_org) or die(mysqli_error($this->db->connect));
                $checkResults = mysqli_num_rows($result);

                 $response = array();
            //CHECK FOR RESULTS
                if ($checkResults > 0) {
                    while ($getRecord = mysqli_fetch_array($result)) {
                            $response[] = $getRecord;
                    }

                    return $response;
                }else{
                    return FALSE;
                }

        }

我回应json对象。 如何从model到我的controller收到此回复?

if($_SERVER['REQUEST_METHOD'] == 'GET'){

    //INCLUDE CLASS
         require_once($_SERVER['DOCUMENT_ROOT'] .'/yayongeppn/OOP/models/orgCreateOrganization.php');

    //NEW INSTANCE OF AN OBJECT
        $orgCreate = new orgCreateOrganization();

    //TAG
        $tag = $_POST['tag'];

        if($tag == 'org'){


                    $response = array();

                    $result = $orgCreate->getAllOrgRegistered();
                        if ($result != FALSE) {

                            //  while ($getRecord = mysqli_fetch_array($result)) {
                            //          $response[] = $getRecord;
                            //  }



                            echo json_encode($result);

                        }else{
                            $response['error'] = "Failed to fetch data.";
                            echo json_encode($response);
                        }

        }

    }//END SERVER

我真的需要你的帮助。

0 个答案:

没有答案