PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

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

 April 28, 2022     codeigniter, curl, html, php, warnings     No comments   

Issue

I have build simple application for view data on table with call rest service in java eclipse, use curl in php native mvc, I have trying to call rest service use curl but show messages error in the table, how can I fix it? I will appreciate your help :))

this is my controller in 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;

    } 

This is my code in 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>  

This is my message error

Warning: Invalid argument supplied for foreach() 

Solution

if($dataresult == NULL)

Looks wrong. Shouldn't it be:

if($dataresult !== NULL)

? You can't loop through null!

That's not all though. You overwrite $row with your inner loop!

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


Answered By - delboy1978uk
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing