Usted puede utilizar esto como un ejemplo
class Crud extends Model {
// selecting records by specifying the column field
function select()
{
// use $this->db->select('*') if you want to select all the records
$this->db->select('title, content, date');
// use $this->db->where('id', 1) if you want to specify what row to be fetched
$q = $this->db->get('mytable');
// to get the result
$data = array();
// for me its better to check if there are records that are fetched
if($q->num_rows() > 0) {
// by doing this it means you are returning array of records
foreach($q->result_array() as $row) {
$data[] = $row;
}
// if your expecting only one record will be fetched from the table
// use $row = $q->row();
// then return $row;
}
return $data;
}
// to add record
function add()
{
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
}
// to update record
function update()
{
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', 1);
$this->db->update('mytable', $data);
}
// to delete a record
function delete()
{
$this->db->where('id', 1);
$this->db->delete('mytable');
}
}
Algunos de esto son de codeigniter userguide.
Para ver los registros,
Si los datos de retorno es matriz de registros,
foreach($data as $row)
{
echo $row['title'] . "<br />";
}
Si los datos de retorno es un objeto (mediante el uso de $q->row
),
echo $data->title;
Esto es solo algunos ejemplos o CRUD en Codeigniter. Visite codeigniter userguide.
en la función deleteRecord(), ¿por qué usaste $ this-> db-> set(), hasta donde sé, es uso para la actualización ... – Manie
@Manie - gracias por señalar eso. Lo he corregido – vikmalhotra
para Manie y ShiVik gracias por su ayuda, estoy muy agradecido ... – marikudo