Próbuję wyświetlić wynik zapytania do bazy danych w moim kontrolerze, ale nie wiem, jak to zrobić. Czy możesz mi pokazać?
Kontroler
function get_name($id){
$this->load->model("mod_names");
$data["records"]=$this->mod_names->profile($id);
// I want to display the the query result here
// like this: echo $row ["full_name"];
}
Mój model
function profile($id)
{
$this->db->select("*");
$this->db->from("names");
$this->db->where("id", $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{ return $query->row_array();
}
else {return NULL;}
}
Odpowiedzi:
6 dla odpowiedzi № 1echo "<pre>";
print_r($data["records"]);
lub
echo $data["records"][0]["fullname"];
4 dla odpowiedzi nr 2
Model:
function profile($id){
return $this->db->
select("*")->
from("names")->
where("id", $id)->
get()->row_array();
}
Kontroler:
function get_name($id){
$this->load->model("mod_names");
$data["records"]=$this->mod_names->profile($id);
print_r($data["records"]); //All
echo $data["records"]["full_name"]; // Field name full_name
}
3 dla odpowiedzi nr 3
Robisz to w widoku, w ten sposób.
Kontroler:
function get_name($id){
$this->load->model("mod_names");
$data["records"]=$this->mod_names->profile($id);
$this->load->view("mod_names_view", $data); // load the view with the $data variable
}
Widok (mod_names_view):
<?php foreach($records->result() as $record): ?>
<?php echo $record->full_name); ?>
<?php endforeach; ?>
Zmodyfikowałbym twój model na coś takiego (działało dla mnie):
function profile($id)
{
$this->db->select("*");
$this->db->from("names");
$this->db->where("id", $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query; // just return $query
}
}