Welcome Guest, Not a member yet? Register   Sign In
Any idea why my method isn't returning TRUE?
#1

[eluser]esset[/eluser]
Hi

I have an XML-document I read in from my controller to my model which makes the insert into the DB. The insert call returnes TRUE but it doesn't return it back to the controller, then it's empty. Any ideas why? Thank you so much.


Controller
Code:
function _add_shows($shows)
    {
        // Shows
        foreach($shows->show as $node)
        {
            $result = $this->Show_model->add($node);

            if($result == TRUE)
            {
                echo '<b style="color: green;">Show added:</b> '. $node->showname . '<br/>';
            } else {
                echo '<b style="color: red;">Show not added:</b> '. $node->showname . '<br/>';
            }
        }
    }

Model
Code:
function add($show) {
        
        if( !$this->check_if_show_exists($show->showid) )
        {
            // Data
            $data = array(
                    'id'     => $show->showid,
                    'name'     => xml_convert($show->showname),
                    'link'     => xml_convert($show->showlink)
                    );
                    
            // Insert
            $result = $this->db->insert('shows', $data);
            
            return TRUE;
            
        } else {
        
            return FALSE;
        }
    }
    
    // --------------------------------------------------------------------
    
    /*
     * Check if show ID exists
     */
    function check_if_show_exists($id = '')
    {
        if ($id == '')
        {
            // no id, return false
            return FALSE;
        }

        $this->db->select('id');
        $this->db->where('id', $id);
        $result = $this->db->get('shows');

        if ($result->num_rows() != 1)
        {
            // no match, return false
            return FALSE;
        }
        else
        {
            //match, return true
            return TRUE;
        }
    }
#2

[eluser]InsiteFX[/eluser]
The $result is returning a $query object so in your controller it is testing
an object not true or false.

Try this:
Code:
if(is_object($result))
{
    echo '<b style="color: green;">Show added:</b> '. $node->showname . '<br/>';
} else {
    echo '<b style="color: red;">Show not added:</b> '. $node->showname . '<br/>';
}

InsiteFX
#3

[eluser]esset[/eluser]
So you mean $this->Show_model->add($node); isn't returning the row "return TRUE"?

Why isn't the function returning the "return" value, but an object instead?

Thanks
#4

[eluser]InsiteFX[/eluser]
Code:
function _add_shows($shows)

$result = $this->Show_model->add($node);

Your function _add_shows($show) is returning $result which is an object.

InsiteFX
#5

[eluser]esset[/eluser]
Oh sorry it's the "add()" function in my model that doesn't return TRUE.

I have this call to my model:
Code:
$result = $this->Show_model->add($node);

            if($result == TRUE)
            {
                echo '<b style="color: green;">Show added:</b> '. $node->showname . '<br/>';
            } else {
                echo '<b style="color: red;">Show not added:</b> '. $node->showname . '<br/>';
            }

In my model you can see that it returns TRUE after the insert, but it just returnes empty so all rows read "Show not added" even if it is.

Ideas?

Thank you




Theme © iAndrew 2016 - Forum software by © MyBB