CodeIgniter Forums
Object of class CI_DB_mysql_result could not be converted to string - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Object of class CI_DB_mysql_result could not be converted to string (/thread-10703.html)



Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-09-2008

[eluser]vickel[/eluser]
Hallo everybody,

I'm building a shopping cart and get the above error on a certain occasion:

I have in a controller the function refresh() which contains:

$this->db->select('*');
$this->db->where('sessionID', $guestSessionID);

$data['cartNow'] = $this->db->get('ticket');

$this->load->view('cart_view', $data);

in the view I use:

<?php foreach($cartNow->result() as $row):

//get variables from tables products corresponding to the ticket
$this->db->select('*');
$this->db->from('products');
$this->db->join('ticket', 'products.prodID = ticket.prodID');
$this->db->where('ticket.prodID', $row->prodID);
$query = $this->db->get();
$row = $query->row();
?>
...
...
<?php endforeach?>


the view has a checkbox, when it is checked the item is deleted from the database, this WORKS FINE if I have 2 items and remove the first, but DOESN'T WORK if i have 2 items and remove the second.


PS.: if I use after that error the above function (refresh) again, all is OK. In the database everything is OK

thank you for any help on that


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-10-2008

[eluser]vickel[/eluser]
Hallo everybody,

I'm building a shopping cart and get the above error on a certain occasion:

I have in a controller the function refresh() which contains:

Code:
$this->db->select('*');
$this->db->where('sessionID', $guestSessionID);
$data['cartNow'] = $this->db->get('ticket');
$this->load->view('cart_view', $data);

in the view I use:

Code:
<?php foreach($cartNow->result() as $row):

   //get variables from tables products corresponding to the ticket
   $this->db->select('*');
   $this->db->from('products');
   $this->db->join('ticket', 'products.prodID = ticket.prodID');
   $this->db->where('ticket.prodID', $row->prodID);
   $query = $this->db->get();
   $row = $query->row();
   ?>
   //...
   //...
<?php endforeach?>


the view has a checkbox, when it is checked the item is deleted from the database, this WORKS FINE if I have 2 items and remove the first, but DOESN'T WORK if i have 2 items and remove the second.


PS.: if I use after that error the above function (refresh) again, all is OK. In the database everything is OK all the time

thank you for any help on that[/quote]


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-10-2008

[eluser]Jilani Jidni[/eluser]
[quote author="vickel" date="1218390780"]Hallo everybody,

I'm building a shopping cart and get the above error on a certain occasion:

I have in a controller the function refresh() which contains:

Code:
$this->db->select('*');
$this->db->where('sessionID', $guestSessionID);
$data['cartNow'] = $this->db->get('ticket');
$this->load->view('cart_view', $data);

in the view I use:

Code:
<?php foreach($cartNow->result() as $row):

   //get variables from tables products corresponding to the ticket
   $this->db->select('*');
   $this->db->from('products');
   $this->db->join('ticket', 'products.prodID = ticket.prodID');
   $this->db->where('ticket.prodID', $row->prodID);
   $query = $this->db->get();
   $row = $query->row();
   ?>
   //...
   //...
<?php endforeach?>


the view has a checkbox, when it is checked the item is deleted from the database, this WORKS FINE if I have 2 items and remove the first, but DOESN'T WORK if i have 2 items and remove the second.


PS.: if I use after that error the above function (refresh) again, all is OK. In the database everything is OK all the time

thank you for any help on that[/quote][/quote]

Hi vickel

Its not clear how you send the request to the server for delete.

And one suggestion from myself please don't do any query from the view page. use model for to do database related job. controller will do all kind of business logic and view will just display the output. please read the manual for details.


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-11-2008

[eluser]vickel[/eluser]
Thank you Jilani, for your reply, there is no problem with the delete operation at all, its just that the line

Code:
$data[’cartNow’] = $this->db->get(’ticket’);

causes the error, but only if I delete the second record of two (or for example the third of 4) ????


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-11-2008

[eluser]Jilani Jidni[/eluser]
[quote author="vickel" date="1218465367"]Thank you Jilani, for your reply, there is no problem with the delete operation at all, its just that the line

Code:
$data[’cartNow’] = $this->db->get(’ticket’);

causes the error, but only if I delete the second record of two (or for example the third of 4) ????[/quote]

Sorry its not clear for me. why you are facing this problem. can you send the whole code?


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-11-2008

[eluser]vickel[/eluser]
Hi Jilani, thank you for your time, her comes the code

Code:
function refresh()
    {

    
    //check if 'username' exists
    // it doesn't exist after cleaning cache and going back by bookmark
    // to http://www.ericeiraglass.com/shop/index.php/products/refresh
    
    $guestSessionID = $this->session->userdata('username');
    
    if ($guestSessionID!="")
        {
    
        $query = $this->db->get_where('ticket', array('sessionID' => $guestSessionID));
        $myRows = $query->num_rows();
        $myR=$myRows;
        
        if ($myRows!=0)
        {
            for ($i=1; $i<=$myRows; $i++)
                {    
                $data = $this->input->post('remove'.$i);     // remove1,2... is the checkbox value ('checked')
                
                if ($data=='checked')
                    {
                    $prodID=$this->input->post('myID'.$i);        // the value of product ID
    
                    
                    $this->db->delete('ticket', array('prodID' => $prodID));
                    $myR=$myR-1;
                    
                    }
                        
                
                else
                    {
                    $quant = $this->input->post('quantity'.$i);        // update the Textarea in each row
                    $prodID=$this->input->post('myID'.$i);            // the value of product ID
                    
                    $this->db->where('prodID', $prodID);
                    $this->db->update('ticket', array('quantity' => $quant));
                    }
                        
                }    //ends the for loop
                
            
            //output views
            //get the ticket data and send it to the cart_view
            
            
            if ($myR==0)
                {
                // no ticket exist
                $this->load->view('emptyCart');                        
                }
            else
                {    
                
                $ticketdata = array(
                                   'sessionID' => $guestSessionID ,
                                   'prodID' => $prodID ,
                                   'last_change' => time(), //date('yy-m-d'),
                                   'quantity' => 1);    
                
                //$data['cartNow'] = $this->db->get_where('ticket', 'sessionID' = $guestSessionID);
                //doesn't work !!!
                                    
                // data query to populate with shoppingcart items
                // from the current ticket
        

                $this->db->select('*');
                $this->db->where('sessionID', $guestSessionID);
                //$this->db->order_by("last_change", "desc");
            
            
                $query = $this->db->get('ticket');
                $row = $query->row_array();  
                
                //return $query->row_array();
                //echo "rowID= " . $row['prodID'];
                

                
                //build an array
                //var_dump($query);
                $data['cartNow'] = $query;

$data['cartNow'] = $query; is the line which causes the error
Code:
//var_dump($data['cartNow']);
                //$data['cartNow']=$query->result_array();
                
                

                
                $this->load->view('cart_view', $data);
                
                
                //get the product from ticket, which was latest changed
                //this is to get the $data array to build the left_products view
                
                $mySql="
                SELECT Max(ticket.last_change) AS mylastchange, productCat.catName, productCat.catID
                FROM (ticket
                INNER JOIN products ON ticket.prodID = products.prodID)
                INNER JOIN productCat ON products.prodCat = productCat.catID
                GROUP BY productCat.catName, ticket.sessionID, productCat.catID
                HAVING (((ticket.sessionID)='" . $guestSessionID . "'))";  
                
                $query = $this->db->query($mySql);    
                $row = $query->row_array();
                
                $prodID = $row['catID'];
                
                $data['catID'] = $this->db->get_where('productCat', array('catID' => $prodID));            
                $data['subcatID'] = $this->db->get_where('productSubCat', array('catID' => $prodID));
                
                $this->load->view('leftbar_products', $data);
    
                if ($this->session->userdata('logged_in') == TRUE)
                    {
                    $this->load->view('rightbar_loggedIn', $data);
                    }
                else
                    {
                    $data['usern'] = array('id' => 'usern', 'name' => 'usern');
                    $data['passw'] = array('id' => 'passw', 'name' => 'passw');                    
                    $this->load->view('rightbar_loggedOut', $data);
                    }

                $this->load->view('menu', $data);        

                }
            }
        else
        {
        // no ticket exist
        $this->load->view('emptyCart');    
        }
        }    
    else
        {
        // implement session destroy (must be last)
        
                $this->session->sess_destroy();
        
        //add userinfo to new session cookie
        $guestID = "guest" . mt_rand();
        $newdata = array(
           'username'  => $guestID,
           'logged_in' => FALSE);

        $this->session->set_userdata($newdata);    
        $this->load->view('emptyCart');
        }        

    }    //end of functiom refresh



Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-11-2008

[eluser]Jilani Jidni[/eluser]
sorry vickel I am unable to answer your problem. I think I could not read your code properly.


Object of class CI_DB_mysql_result could not be converted to string - El Forum - 08-11-2008

[eluser]vickel[/eluser]
thanks for your time anyway, if you want to see what's happening goto www.ericeiraglass.com/shop, add 2 items to the shopping cart and remove the first, everything is fine. add another item and this time delete the second: here then comes the error

:::
:::

just put the database stuff into a model, now it works Smile

thanks everybody