Welcome Guest, Not a member yet? Register   Sign In
Can't display data in views.
#1

I can't display the value in my views.

model code
PHP Code:
function get_subcat(){
        
$s_sub_category = array();
        
$sql "SELECT DISTINCT Component_Type_ID FROM component_list WHERE 20x20_Rail = '1' OR 20x40_Rail = '1' OR 20x60_Rail ='1';";
        
$query $this->db->query($sql);

        if (
$query->num_rows() > 0){
            
$result $query->result();
                foreach (
$result as $key => $value) {
                    
$get_compo_id_value $value->Component_Type_ID;
                        
$sql "SELECT Component_Type_Name FROM component_type_list WHERE Component_Type_ID = $get_compo_id_value";
                        
$query $this->db->query($sql);
                        
$s_sub_category[] = $query->result();
                }
            return 
$s_sub_category;
        }
        else{
            return 
'';    
        }
        
    } 

controller
PHP Code:
        $data['sub_category'] = $this->Component_model->get_subcat();

        
//load the view
        
$this->load->view('component_page',$data); 

View
PHP Code:
<?php 
                                                                       
foreach ($sub_category as $key => $value) {
                     
                                                    echo '<li><a href="#">'.$value->Component_Type_Name.'</a></li>';
                     
                                               }    
                     
                                            ?>

i get this error:
Trying to get property of non-object

i tried to check if there is a data in my array using print_r and display the data.

thanks in advance
Reply
#2

(09-20-2015, 08:32 PM)rhyszz Wrote: I can't display the value in my views.

model code

PHP Code:
function get_subcat(){
        
$s_sub_category = array();
        
$sql "SELECT DISTINCT Component_Type_ID FROM component_list WHERE 20x20_Rail = '1' OR 20x40_Rail = '1' OR 20x60_Rail ='1';";
        
$query $this->db->query($sql);

        if (
$query->num_rows() > 0){
            
$result $query->result();
                foreach (
$result as $key => $value) {
                    
$get_compo_id_value $value->Component_Type_ID;
                        
$sql "SELECT Component_Type_Name FROM component_type_list WHERE Component_Type_ID = $get_compo_id_value";
                        
$query $this->db->query($sql);
                        
$s_sub_category[] = $query->result();
                }
            return 
$s_sub_category;
        }
        else{
            return 
'';    
        }
        
    } 

controller

PHP Code:
        $data['sub_category'] = $this->Component_model->get_subcat();

        
//load the view
        
$this->load->view('component_page',$data); 

View

PHP Code:
<?php 
                                                                       
foreach ($sub_category as $key => $value) {
                     
                                                    echo '<li><a href="#">'.$value->Component_Type_Name.'</a></li>';
                     
                                               }    
                     
                                            ?>

i get this error:
Trying to get property of non-object

i tried to check if there is a data in my array using print_r and display the data.

thanks in advance

Perhaps try with


Code:
$result = $query->result_array();
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#3

i still got the same error.

i conduct some test with my model

PHP Code:
foreach ($result as $key => $value) {
                    
$get_compo_id_value $value->Component_Type_ID;
                        
$sql "SELECT Component_Type_Name FROM component_type_list WHERE Component_Type_ID = $get_compo_id_value";
                        
$query $this->db->query($sql);
                        
//$s_sub_category[] = $query->result_array();
                        
return $query->result();
                } 

i return the value inside the forloop but i gives me 1 record only.
Reply
#4

(09-20-2015, 08:32 PM)rhyszz Wrote: I can't display the value in my views.

model code

PHP Code:
function get_subcat(){
        
$s_sub_category = array();
        
$sql "SELECT DISTINCT Component_Type_ID FROM component_list WHERE 20x20_Rail = '1' OR 20x40_Rail = '1' OR 20x60_Rail ='1';";
        
$query $this->db->query($sql);

        if (
$query->num_rows() > 0){
            
$result $query->result();
                foreach (
$result as $key => $value) {
                    
$get_compo_id_value $value->Component_Type_ID;
                        
$sql "SELECT Component_Type_Name FROM component_type_list WHERE Component_Type_ID = $get_compo_id_value";
                        
$query $this->db->query($sql);
                        
$s_sub_category[] = $query->result();
                }
            return 
$s_sub_category;
        }
        else{
            return 
'';    
        }
        
    } 

controller

PHP Code:
        $data['sub_category'] = $this->Component_model->get_subcat();

        
//load the view
        
$this->load->view('component_page',$data); 

View

PHP Code:
<?php 
                                                                       
foreach ($sub_category as $key => $value) {
                     
                                                    echo '<li><a href="#">'.$value->Component_Type_Name.'</a></li>';
                     
                                               }    
                     
                                            ?>

i get this error:
Trying to get property of non-object

i tried to check if there is a data in my array using print_r and display the data.

thanks in advance

can you post the print_r of $data['sub_category'] before sending to your view ? 
Reply
#5

From your original model code:

PHP Code:
$s_sub_category[] = $query->result(); 

That line should create a new entry in the $s_sub_category array which contains an array containing 0 or more objects (the rows returned by your query). So, if you leave your model the way it was, you would just need an additional foreach() loop in the view:

PHP Code:
foreach ($sub_category as $query_result) {
    foreach (
$query_result as $key => $value) {
        echo 
"<li><a href='#'>{$value->Component_Type_Name}</a></li>";
    }


However, it would make more sense to use a join in the original query to select the Component_Type_Name alongside the Component_Type_ID in a single query, rather than performing an additional query for each row returned by the original query. Something like this should work:

PHP Code:
$sql "SELECT DISTINCT Component_Type_ID, Component_Type_Name 
FROM component_list 
LEFT OUTER JOIN component_type_list ON component_list.Component_Type_ID = component_type_list.Component_Type_ID
WHERE 20x20_Rail = '1' OR 20x40_Rail = '1' OR 20x60_Rail ='1';"


Then you wouldn't need the foreach() loop in your model and the original code you supplied for the view should work fine.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB