Welcome Guest, Not a member yet? Register   Sign In
[solved]query->result_array(); returns $var['0']['field'] and not $var['field']
#1

[eluser]Erocanti[/eluser]
Hey! New to CodeIgniter,
I need some help find my mistake here, my view is not displaying the variables that I hope i'm passing correctly. Below is my code.
Thanks in advance for any assistance and advice.


Controller
Code:
function edit()
    {
        $id = $this->uri->segment(3, 0);
        $data = array();
        if ($id !== 0)
        {
            
            $this->load->model('dashboard_m');
            $data['fields'] = $this->dashboard_m->getRecord($id);
            $data['fields'] = $data['fields']['0']; //This seems to solve the situation for mean time, I've used this type of query before and this is the first time this has responded this way.            
            $this->load->view('edit', $data);
        }
        else
        {
            $this->index();
        }
    }

Updated
Model
Code:
function getRecord($id)
    
    {        
        $this->db
                   ->from('calls')
                   ->where('logid', $id);
        $query = $this->db->get();
        
    return $query->result_array();
    }

View
Code:
<div class="clearfix">
    <label>Incident Number</label>
    &lt;input type="text" name="incnumber" id="incnumber" value="&lt;?php echo $fields['0']['incnumber']; ?&gt;" /&gt;
</div>

Error
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: incnumber
Filename: views/edit.php
Line Number: 99

print_r($fields)
Code:
Array ( [0] => stdClass Object ( [logid] => 43 [type_of_incident] => Slip/Fall [dispatcher] => test
[txdate] => 2011-06-30 [supervisornotified] => 910 [post] => 123 [txtime] => 21:18
[related_calls] => None [status] => Active [incidentclass] => Other [priority] => Normal
[description] => Lorem... [comments] => Lorem... [incnumber] => 110603B43 [station] => Alpha
[location] => Escalator [pdnotified] => checked [frnotified] => checked [pdcase] => 000000
[fralarm] => 000000 [sector] => 3 [locationinfo] => Lorem... ) )
#2

[eluser]InsiteFX[/eluser]
This is your error
Code:
&lt;input type="text" name="incnumber" id="incnumber" value="&lt;?php echo $fields['incnumber']; ?&gt;" /&gt;
$fields['incnumber']; // incnumber is not defined!

InsiteFX
#3

[eluser]Erocanti[/eluser]
[quote author="InsiteFX" date="1309514977"]This is your error
Code:
&lt;input type="text" name="incnumber" id="incnumber" value="&lt;?php echo $fields['incnumber']; ?&gt;" /&gt;
$fields['incnumber']; // incnumber is not defined!

InsiteFX[/quote]

I thought that by passing the $data variable when loading the view the $fields['array'] would be accessible at the view.
i looked at the documentation again, and I'm not understanding why its not working. Thanks again for any input.
#4

[eluser]InsiteFX[/eluser]
If your passing it to the view then try this:
Code:
$fields[$incnumber];

InsiteFX
#5

[eluser]Erocanti[/eluser]
No, that doesn't work
#6

[eluser]marjune[/eluser]
your problem was solve here
Code:
foreach($fields as $fields){
  $incnumber = $fields['incnumber'];
}
&lt;input type="text" name="incnumber" id="incnumber" value="&lt;?php echo $incnumber; ?&gt;" /&gt;
#7

[eluser]Erocanti[/eluser]
Code:
[Fri Jul 01 09:16:17 2011] [error] [client XXX.XXX.XXX.XXX] PHP Fatal error: Cannot use
object of type stdClass as array

That didn't do it. It refers to this line
Code:
$incnumber = $fields['incnumber'];
#8

[eluser]Erocanti[/eluser]
Update looking the the result of the print_r

i tried
Code:
$fields['0']['incnumber']
and its populating, but how do I remove the ['0'] now so its just
Code:
$fields['incnumber']
?
#9

[eluser]Erocanti[/eluser]
Code:
function edit()
    {
        $id = $this->uri->segment(3, 0);
        $data = array();
        if ($id !== 0)
        {
            
            $this->load->model('dashboard_m');
            $data['fields'] = $this->dashboard_m->getRecord($id);
            $data['fields'] = $data['fields']['0'];
            //This seems to solve the
            //situation for mean time, I've used this type of query before and this is the        
            //first time this has responded this way.            
            $this->load->view('edit', $data);
        }
        else
        {
            $this->index();
        }
    }
#10

[eluser]Mirge[/eluser]
If you only wanted the first result, you could have used $query->row_array() instead of $query->result_array() in your model.




Theme © iAndrew 2016 - Forum software by © MyBB