0

I want to display my database record in a table, but I don't get it. I don't know whats wrong in my code but it displays empty result. I have attached my model and view. I am using codeigniter framework. Any help would be much appreciated..

Here is my model:

public function display_data(){     
    $query = $this->db->query("SELECT * FROM sales_rep_tbl;");              
    if($query->num_rows > 0){
            $this->table->set_heading(
            "SR_Code",
            "SR_Fname"
            );
    foreach($query->result() as $r){

        $bg = "black";
            $this->table->add_row(
                    "<div class = '".$bg."'>".$r->SR_Code."</div>",
                    "<div class = '".$bg."'>".$r->SR_Lname."</div>",
                    "<div class = '".$bg."'>".$r->SR_Fname."</div>"         
            );
        $data.= $this->table->generate().br();
        }   
    return $data;
    }  
}

And my view:

<div class = "container" >
<div class = "row" style = " height:400px; width:auto; margintop:20px;" >
            <div class = "col-xs-12">

                        <?php echo $this->Main_Page_Model->display_data();?>

            </div>
        </div>
    </div>
3
  • Why are You displaying data direct from the model? If you are using a Framework then you should follow the rules. Commented Mar 22, 2017 at 3:39
  • its only a week since im using codeigniter framework, and it really confusing aout mvc. thats why i didnt know what really is correct. can you teach me ? Commented Mar 22, 2017 at 3:41
  • Please see my answer. For Codeigniter, I will say you must see the user guide first. It's all there. It's too easy to understand. Follow this link. codeigniter.com/user_guide Commented Mar 22, 2017 at 3:49

1 Answer 1

2

Please Follow the MVC structure of CI.

You Controller Should be:

 public function veiw_table() {
    $data= $this->yourModel->yourfunction();
    $this->load->view('yourViewPage', ['data' => $data]);
}

Your Model Should Be:

 public function yourfunction() {

    $query = $this->db-
     ->select('*')        
    ->get('yourTableName');

    return $query ->result();

 }

IN View:

foreach ($data as $singleData){
echo $singleData->columnNam;

}

Hope This will Help You

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.