Welcome Guest, Not a member yet? Register   Sign In
data display - problem
#1

[eluser]penta997[/eluser]
Helo. I have a problem with displaying data from model. Instead subcategory list, controller displaying only the last record from subcategorys. This is my model code:
Code:
<?php


class Kategorie_model extends Model
{
    function Kategorie()
    {
        parent::Model();
    }
    
    function get_category()
    {
        
        
            $this->db->query("Select * from category order by CAT_ID");
           return $this->db->get('category');
    }



      
        function get_sub_category($id)
        {
            $result = $this->db->query("Select SUBC_Name from sub_category where CATEGORY_CAT_ID = '".$id."'");

          
            return $result;
        }



      
    
}

?>

and this is controller:
Code:
<?php


class Ksiegarnia extends Controller
{

        
    function Ksiegarnia()
    {
        parent::Controller();
                $this->load->model('Kategorie_model');
                $this->response = array();
                
                
    }


  
        function main()
        {





            $view['left'] = $this->get_category();
            $view['right'] = $this->load->view('Ksiegarnia/right', '', true);




            $view['center'] = $this->load->view('Ksiegarnia/main', array('pierwszy' => 'pierwszy post'), true);
          
            
            $this->load->view('Ksiegarnia/index', $view);

            
        }
      
          
        function news()
        {
            $view['left'] = $this->get_category();
            $view['right'] = $this->load->view('Ksiegarnia/right', '', true);




            $view['center'] = $this->load->view('Ksiegarnia/main', array('first' => '2nd  click'), True);
          
            $this->load->view('Ksiegarnia/index', $view);
        }


        function get_category()
        {
          
            $query = $this->Kategorie_model->get_category();
            $this->response['subcategory'] = '';
            $this->response['category'] = '';
            $subcategory = '';



            if($query->num_rows() > 0)
            {
                foreach($query->result() as $item)
                {
                    $subcategory = $this->get_sub_category($item->CAT_ID);
                    
                    $this->response['category'] .=  $this->load->view('Ksiegarnia/left', array('category' =>$item, 'subcategory'=>$subcategory), true);
                }
            }

            $data = $this->response['category'];
            return $data;

        }





         function get_sub_category($id)
        {

            
             $this->response['result'] = '';

          
                $query = $this->Kategorie_model->get_sub_category($id);
              
                foreach($query->result() as $row)
                {
                $this->response['result'] = $row;
                }

          
                return $this->response['result'];
        }


      
}




?>

I need help, please.
#2

[eluser]cideveloper[/eluser]
in function get_sub_category change

Code:
$this->response['result'] = $row;

to

Code:
$this->response['result'] .= $row;
#3

[eluser]penta997[/eluser]
It doesnt work, now i have a php error:

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: controllers/ksiegarnia.php

Line Number: 95
#4

[eluser]Cristian Gilè[/eluser]
in your controller:
Code:
function get_sub_category($id)
        {

            
             $this->response['result'] = '';

          
                $query = $this->Kategorie_model->get_sub_category($id);
              
                foreach($query->result() as $row)
                {
                $this->response['result'] .= $row; //here object property is missing. Should be something like $row->cat_name
                }

          
                return $this->response['result'];
        }

Cristian Gilè
#5

[eluser]penta997[/eluser]
now i have an error: Trying to get property of non-object. Maybe is something wrong in my view, where i trying return my data?
This is view left.php

Code:
<div class="cat_name">&lt;?php echo $category->CAT_Name; ?&gt;</div>                        
<div class="subcat_name"><a href="#">&lt;?php echo $subcategory->SUBC_Name; ?&gt;</a></div>
#6

[eluser]Cristian Gilè[/eluser]
If you use string concatenation (.=) subcategory is a string, so you have to echo it directly:
Code:
<div class="subcat_name"><a href="#">&lt;?php echo $subcategory; ?&gt;</a></div>


Cristian Gilè
#7

[eluser]penta997[/eluser]
You're right. Thank you very much for help.




Theme © iAndrew 2016 - Forum software by © MyBB