Welcome Guest, Not a member yet? Register   Sign In
HELP! Pass query to varible
#1

[eluser]anakkampung[/eluser]
Hi, i wanna asking something..

if i have a mysql table like this
=======================
id || NAME || Value ||
=======================
1 || title || Welcome to My site
2 || admin || myemail@localhost.com
3 || etc1 || etcvalue1
==========================

and i want to make view in my views page with varible like $title, $admin, $etc1... how can i make the controllers, models and or library and views?

thx
#2

[eluser]davidbehler[/eluser]
This might need some adjustment but basically it goes like this:
Controller in /application/controllers/welcome.php
Code:
class Welcome extends CI_Controller {
  function index ()
  {
    $this->load->model('Welcome_model');
    $data = array();
    $data['entries'] = $this->Welcome_model->get_entries();
    $this->load->view('welcome', $data);
  }
}
Model in /application/models/welcome_model.php
Code:
class Welcome_model extends CI_Model {
  function get_entries()
  {
    $this->db->from('table_name');
    $result = $this->db->get();

    if($result->num_rows() > 0) {
      return $result->result_array();
    } else {
      return FALSE;
    }
  }
}
View in /application/views/welcome.php
Code:
if(is_array($entries)) {
    foreach($entries as $entry) {
      echo $entry['id'].' - '.$entry['name'].' - '.$entry['value'];
    }
  } else {
    echo 'No entries found.';
  }

Try reading the User Guide, that might make things clearer for you.




Theme © iAndrew 2016 - Forum software by © MyBB