【问题标题】:Message: Invalid argument supplied for foreach() [duplicate]消息:为 foreach() 提供的参数无效 [重复]
【发布时间】:2017-12-26 15:48:48
【问题描述】:

请帮帮我是死胡同检查我的图片在视图codeigniter消息错误在类别打开 enter image description here

但是对于类别结束和所有完成的工作都没有错误; 请帮忙

控制器

$limits=5;
$xx=$this->db->query('select * from posting')->row_array();
$x=$this->db->query('select * from kategori')->row_array();
in_array($x['kategori'],$dataArray=unserialize($xx['kategori'])) ?  $x['kategori']    : '' ;

foreach ($dataArray as $keys => $value) {
    if (($value == $link))  {
        $data['view']=$this->Cek_level->view6($limits,$link)->result();
    }
}

在视图中

<?php foreach($view as $row){?> 

【问题讨论】:

  • 已经尝试不工作
  • var_dump($view) 它包含什么?
  • 已经尝试过什么?这些错误消息非常有用。 (顺便说一句,将错误消息文本直接包含在您的帖子中而不是链接到文本图像被认为是更好的做法。)
  • @aynber C:\wamp64\www\blog\application\views\categories.php:136:null 但属于 Ending work C:\wamp64\www\blog\application\views\categories 类别。 php:136: 数组 (size=3) 0 => object(stdClass)[23] public 'id' => string '2' (length=1) public 'pengunjung' => string '2' (length=1) public 'username' => string 'fathul' (length=6) public 'judul' => string 'Roys - Can't you say (Single) Ending Koi to
  • 已在数据库类别中打开

标签: php database codeigniter


【解决方案1】:

你的代码只是做了很多假设,好像变量总是被设置并且是正确的类型。

$limits=5;

// First query
$xx = $this->db->query('select * from posting');

// Make sure you have a row before you try to use it.
if( $xx->num_rows() == 1 )
{
    $y = $xx->row_array();

    // Unserialize can always fail
    $dataArray = @unserialize( $y['kategori'] );
}

// Second query
$x = $this->db->query('select * from kategori');

// Make sure you have a row before you try to use it.
if( $x->num_rows() == 1 )
{
    $z = $x->row_array();
}

/*
It's impossible for me to know what you are trying to do here

in_array( $y['kategori'], $dataArray ) 
    ? $z['kategori'] 
    : '' ;
*/

// Make sure that dataArray is available as an array
if( isset( $dataArray) && is_array( $dataArray ) )
{
    foreach( $dataArray as $keys => $value )
    {
        // Make sure that $link exists
        if( isset( $link ) && $value == $link )
        {
            $data['view'] = $this->Cek_level->view6( $limits, $link )->result(); // Why not pass back the result ??
        }
    }
}

// Here we make sure view is available, no matter what happened
if( ! isset( $data['view'] ) )
    $data['view'] = [];

此代码显然未经测试,需要您对其进行更改才能使其正常工作,但它显示了您需要如何检查数据和变量是否存在。

【讨论】:

  • 确实如此。始终假定数组为空并通过num_rows() 进行检查。但是你真的应该事先定义你的变量,比如$z = array(),因为如果条件从未满足,它将是未定义的。
猜你喜欢
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 2018-08-02
  • 2014-01-30
  • 2012-11-13
相关资源
最近更新 更多