How to print each row value one by one in codeigniter?

by tressie.damore , in category: PHP Frameworks , a month ago

How to print each row value one by one in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , a month ago

@tressie.damore 

To print each row value one by one in CodeIgniter, you can follow these steps:

  1. Retrieve the data from the database using a model and query method. For example, if you have a model named "MyModel" with a function named "getRows" that fetches the data, you can use the following code:
1
2
3
4
public function getRows() {
    $query = $this->db->get('your_table_name');
    return $query->result(); // Returns an array of objects representing each row
}


  1. In your controller, call the function to get the data and pass it to the view. For example, if you have a controller named "MyController" with a function named "index" that loads a view named "my_view", you can use the following code:
1
2
3
4
5
public function index() {
    $this->load->model('MyModel');
    $data['rows'] = $this->MyModel->getRows();
    $this->load->view('my_view', $data);
}


  1. In your view file, iterate over the rows array and print each row value. For example, if you want to print the "name" column of each row, you can use the following code:
1
2
3
<?php foreach ($rows as $row): ?>
    <?php echo $row->name; ?><br>
<?php endforeach; ?>


This will print each row value one by one. Adjust the code according to your specific requirements, including the table name and the column you want to print.