Welcome Guest, Not a member yet? Register   Sign In
Invalid argument supplied for foreach()
#1

[eluser]Corbee[/eluser]
Hi,

I kept getting error in my controller

Code:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/register.php

Line Number: 316

but I can't seem to find the error. I used print_r to check if the model is functioning and I got the results

Code:
Array ( [0] => Array ( [vid] => 5 [sessionid] => 035153f140ea45767702b3c88ecc4d21 [regname] => asdf [regaddress] => asdf asdf asd asfd [year] => 2010 [brand] => 23 [model] => 831 [platenum] => asf-123 [motornum] => fasdf-12f3212 [chassisnum] => fdsa124123asd [mvfnum] => 4151-123412 [color] => red [aog] => no [tpl] => yes [delivery] => gma [mortgator] => 0 [insurance] => 2 [carType] => cv [price] => 6715000.00 [coverage] => 75000 ) )

Here is my controller
Code:
function vedit($vid=0)
    {
        $sessionid = getSessionID();        
        
        //get the info of the vid
        $q = $this->MRegister->getguestvid($sessionid,$vid);        
        if (count($q > 0))
        {
            foreach ($q as $key => $list)
            {        
                $data['pabrandID'] = $list['brand'];
                $data['pamodelID'] = $list['model'];
                $year = $list['year'];
                $brandID = $list['brand'];
            }
        }
        $data['vinfo'] = $this->MRegister->getguestvid($vid,$sessionid);
        $data['qyear'] = $this->MCars->getcaryear();
        $data['qbrand'] = $this->MCars->getcarbrands($year);
        $data['qmodel'] = $this->MCars->getcarmodel($year,$brandID);
        //$data[''] = $this->MCars->getcoverage();
        $this->load->view('reg_editcar',$data);
    }

my model
Code:
function getguestvid($vid='',$sessionid='')
    {
        $this->db->where('vid',id_clean($vid));
        $this->db->where('sessionid',db_clean($sessionid,32));
        $q = $this->db->get('tmp_guestvinfo');
        
        if ($q->num_rows() > 0)
        {
            foreach ($q->result_array() as $row)
            {
                $data[] = $row;    
            }    
            $q->free_result();
            print_r($data);
            return $data;
            
        }        
    }

Does anyone have an idea where it went wrong. Thanks
#2

[eluser]Corbee[/eluser]
I tried print_r on the controller's $q, and got nothing. What is causing the returned data not to be received by the variable?
#3

[eluser]Corbee[/eluser]
I found that it is failing to return the data in the model.

When I return anything from the model, it just won't work.

Where did I go wrong?
#4

[eluser]Corbee[/eluser]
I made a temporary solution by putting the model into the controller instead of having to let the model return the data

Code:
function vedit($vid=0)
    {
        $sessionid = getSessionID();        
        //get the info of the vid
        $this->db->where('vid',id_clean($vid));
        $this->db->where('sessionid',db_clean($sessionid,32));
        $q = $this->db->get('tmp_guestvinfo');
        foreach ($q->result_array() as $row)
            {
                $val[] = $row;    
            }    
        $q->free_result();
        
        if (count($val) > 0)
        {
            foreach ($val as $key=>$list)
            {        
                $data['pabrandID'] = $list['brand'];
                $data['pamodelID'] = $list['model'];
                $year = $list['year'];
                $brandID = $list['brand'];
            }
        }

        $data['vinfo'] = $this->MRegister->getguestvid($vid,$sessionid);
        $data['qyear'] = $this->MCars->getcaryear();
        $data['qbrand'] = $this->MCars->getcarbrands($year);
        $data['qmodel'] = $this->MCars->getcarmodel($year,$brandID);
        //$data[''] = $this->MCars->getcoverage();
        $this->load->view('reg_editcar',$data);
    }

Strange isn't it? it fails to return, but it works when you directly call the database in the controller.

Have anyone encountered such problem? Does anyone know the possibilities causing the model to fail in returning data to the controller?
#5

[eluser]crikey[/eluser]
I'm not sure if this is your problem, but two things jump out at me:

First, are you passing the parameters to your model function in the right order? From the way they're named, it looks wrong.

Second, I think
Code:
if (count($q > 0))

should be
Code:
if (count($q) > 0)

Hope that helps,
Grant

Edit: This applied to the code from your first post... I see that you've changed it.
#6

[eluser]Corbee[/eluser]
Thanks,

I just found the problem, it appears that I was unknowingly calling the same model function twice, that's why it didn't work. the $q and $data['vinfo'] are both the same functions




Theme © iAndrew 2016 - Forum software by © MyBB