Welcome Guest, Not a member yet? Register   Sign In
Getting PHP errors but I thought I had everything right!
#1

[eluser]Torch Media[/eluser]
Hi, first off I'll just say this is my first CI app so be kind Smile Here's the error being generated:
Code:
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: admin/edit_category.php

Line Number: 19

Here is my controller excerpt:
Code:
function cat_edit($id)
    {
        $this->load->model('admin_categories');
        $data['main_content'] = 'admin/edit_category';
        $data['category'] = $this->admin_categories->getCat($id);
        $data['parents'] = $this->admin_categories->getTopLevel($id);
        $this->load->view('includes/template', $data);        
    }

My model excerpt:
Code:
function getCat($id)
    {
        $query = $this->db->query('SELECT * FROM categories WHERE (id = '.$id.')');
        var_dump($query);
        var_dump($result);
        return $query->result();
    }

    function getTopLevel($id)
    {
        $query = $this->db->query('SELECT
  categories.id,
  categories.parentid,
  categories.libraryid,
  categories.active,
  categories.name,
  categories.description
FROM
categories
INNER JOIN libraries ON (categories.libraryid=libraries.id)
WHERE
  (parentid = 0)');
        return $query->result();
    }

And finally, my view:

Code:
<?php
    echo form_open('cat_save');
    echo form_input('name', $category->name);
    echo form_dropdown('active', $active, $category->active);
    echo form_textarea('description', $category->description);
    echo form_submit('submit', 'Save');
    echo form_hidden('id', $category->id);
    echo form_close();
    ?>

I hope this is a no-brainer for someone!

Thanks..
#2

[eluser]BrianDHall[/eluser]
What's line 19 that throws the error? According to the error, at some point you are accessing an array in a context that needs a string, and of course changing an array to a string doesn't work so good.
#3

[eluser]Torch Media[/eluser]
Line 19 is:
Code:
echo form_input('name', $category->name);

Of course, it gives the same errors for lines 20,21, and 23.

I added a var_dump for $category and got this:
Code:
array(1) { [0]=>  object(stdClass)#17 (6) { ["id"]=>  string(1) "3" ["parentid"]=>  string(1) "0" ["libraryid"]=>  string(1) "1" ["active"]=>  string(1) "1" ["name"]=>  string(9) "Even More" ["description"]=>  string(0) "" } }

So yeah, it looks like all the data is coming through, but in an array. This really shows how green I am, but I have no idea why it's in an array, and how I can get it into usable data for my edit form that I'm trying to populate.
#4

[eluser]rogierb[/eluser]
If your result is empty, it will not find and return an array.

Code:
if($query->num_rows()>0)
{
  return $query->result();
}

Furthermore it can return more then one record. So if you want only one record, limit your query and use $query->row();

A better way:
Code:
//in model
return $query;

//in view
if($category->num_rows()>0)
{
echo form_open('cat_save');
foreach ( $category->result() as $row )
{
    
    echo form_input('name', $category->name);
    echo form_dropdown('active', $active, $category->active);
    echo form_textarea('description', $category->description);
    echo form_submit('submit', 'Save');
    echo form_hidden('id', $category->id);

}
echo form_close();
}

Above example is for multi records. If you have just one record use $category->row() instead of foreach.
#5

[eluser]Torch Media[/eluser]
Thank you for the reply!

I actually 'discovered' the user guide this morning as well, and I have to say, it's the most well put together documentation I've ever used.

These tutorial guys really shouldn't be saying stuff like "just delete that folder, you won't need that".. Smile




Theme © iAndrew 2016 - Forum software by © MyBB