如何修复警告:为 Php 本机 MVC 中的 foreach() 提供的参数无效

How To Fix Warning: Invalid argument supplied for foreach() in Php Native MVC

我在 table 上构建了用于查看数据的简单应用程序,并在 java eclipse 中调用休息服务,在 php 原生 mvc 中使用 curl,我试图调用休息服务使用curl 但在 table 中显示消息错误,我该如何解决?我将感谢你的帮助:))

这是我在 php

中的控制器
<?php

class App_UserManagement_Control_UserManagement extends UserControl{

    public function usermanagement(){

        Template::setTitle('User Management');

        $result = $this->getUser();      

        $dataresult = json_decode($result, true);
        if($dataresult === NULL) {
            echo "<script language = \"javascript\">window.alert(\"\Tidak Ada Data\");";
            echo "javascript:history.back();</script>";
            return false;
        }
        $data['user'] = $dataresult;
        return $data;
    }

    public function getUser(){

        Template::setTitle('User Management');

        $user_comp_code = Session::get('pyrCode');
        /*API URL */
        $url = "http://localhost:8585/usermaster/$user_comp_code";

        /*init cURL resource */
        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPGET, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec ($curl);

        //echo $url;echo $result;  die;
        if ($result === FALSE){

            die('Curl failed: ' . curl_error($curl));
        }
        curl_close($curl);
        return $result;

    } 

这是我在 html

中的代码
<?php
$main_controll = new App_UserManagement_Control_UserManagement();
$data_from_ctr = $main_controll->usermanagement();
?>

<table cellspacing="2" cellpadding="2" border="0" align="left" id="tablecontent">                      
    <thead style="background-color:#eee;">
        <th width="25">#</th>
        <th width="80">Username</th>
        <th width="117">Name</th>
        <th width="117">Company Code</th>
        <th width="117">Company Name</th>
        <th width="80">User Access</th>
        <th width="80">Login Status</th>
        <th width="80">User Status</th>
        <th width="200">Action</th>

    </thead>
    <tbody>
        <?php if($dataresult == NULL): ?>
        <?php $i = 1; foreach ($dataresult as $row): ?>

        <tr>
        <td><?php echo $i++; ?></td>
        <td><?php echo $row ['_user'] ?></td>
        <td><?php echo $row ['_fullName'] ?></td>
        <td><?php echo $row ['_pyrCode'] ?></td>
        <td><?php echo $row ['_desc'] ?></td>
        <td><?php echo $row ['group_user'] ?></td>
        <td><?php echo $row ['_status'] ?></td>
        <td><?php echo $row ['Active'] ?></td>
        <td></td>
        </tr>
          <?php endforeach; ?>
          <?php endif; ?>
    </tbody>
        </table>
        </fieldset>  

这是我的消息错误

Warning: Invalid argument supplied for foreach() 
if($dataresult == NULL)

看起来不对。不应该是:

if($dataresult !== NULL)

?你不能循环 null!

但这还不是全部。你用你的内循环覆盖 $row

<?php $i = 1; foreach ($dataresult as $row): ?>
        $No=0;
        foreach($data_from_ctr['user'] as $row) {