My question is why posts are not showing after updating or creating in codeigniter? for example if I create a post without assigning a category in the select html tag it creates the post in the database but is not showing in the application and same if update the post without select a category in select html tag. But if I assign a category it shows the post.
Here is my code
Model category
public function pelicula_categoria()
{
$this->db->order_by('categoria_nombre');
$query = $this->db->get('categorias');
return $query->result_array();
}
Model post
public function listar_peliculas()
{
$this->db->select();
$this->db->join('categorias', 'categorias.categoria_id = peliculas.categoria_id');
$this->db->from('peliculas');
$this->db->order_by('peliculas.pelicula_id', 'desc');
$query = $this->db->get();
return $query->result_array();
}
Controller add post
public function agregar_pelicula()
{
$data['error'] = null;
$data['titulo_agregar'] = 'Agregar pelicula';
$data['categorias'] = $this->Categorias_Model->pelicula_categoria();
if ($this->input->post())
{
$this->form_validation->set_rules('pelicula_titulo', 'Titulo', ` 'required');
$this->form_validation->set_rules('pelicula_sinopsis', 'Sinopsis', 'required');
$this->form_validation->set_rules('pelicula_caratula', 'Caratula', 'required');
$this->form_validation->set_rules('pelicula_estreno', 'Estreno', 'required');
$this->form_validation->set_rules('pelicula_puntuacion', 'Puntuacion', 'required');
$this->form_validation->set_rules('pelicula_director', 'Director', 'required');
$this->form_validation->set_rules('pelicula_duracion', 'Duracion', 'required');
$this->form_validation->set_rules('pelicula_pais', '', 'required');
$this->form_validation->set_rules('categoria_id', 'Categoria', 'required');
$this->form_validation->set_rules('pelicula_enlace', 'Enlace', 'required');
if ($this->form_validation->run() == FALSE)
{
$data['error'] = "Todos los campos son requeridos";
}
else
{
$this->Peliculas_Model->insert_pelicula();
$this->session->set_flashdata('mensaje', 'Pelicula agregada correctamente');
redirect(base_url() . 'admin');
}
}
$this->load->view('admin/layouts/header', $data);
$this->load->view('admin/modules/create', $data);
$this->load->view('admin/layouts/footer');
}
And finally showing the categories from database in create view and update view
<select class="custom-select" name="categoria_id">
<option selected>Categoria</option>
<?php foreach ($categorias as $categoria): ?>
<option value="<?php echo $categoria['categoria_id']; ?>">
<?php echo $categoria['categoria_nombre']; ?>
</option>
<?php endforeach; ?>
</select>
Did you check in
print_r();
function?