I'm making an "admin" page to upload photos to different categories, so there is an <input type="file">
and a <select>
that contains categories and subcategories organized in optgroups if a category have a subcategory, or just without the optgroup if it is just a category.
<?php echo form_open_multipart('principal/subir_foto');?>
<div class='input-group'>
<label for="img" class='control-label'>Seleccionar:</label>
<input id="img" required="true" type='file' name='imagen'/>
</div>
<div class='input-group'>
<label for="categoria" class='control-label'>Elegir Categoria:</label>
<select class='selectpicker form-control' name='cat_op' data-width='auto'>
<?php
foreach ($categorias as $row_cat) {
if($row_cat->tiene_subcat > 0){
echo "<optgroup value='".$row_cat->nom_cat ."' label='". $row_cat->nom_cat ."'>";
foreach ($subcat as $row_subcat) {
if($row_subcat->id_cat === $row_cat->id_cat){
echo "<option value=". $row_subcat->nom_subcat ."> ". $row_subcat->nom_subcat ."</option>";
}
}
echo "</optgroup>";
}
else{
echo "<option value='" . $row_cat->nom_cat . "'>". $row_cat->nom_cat . "</option>";
}
}
?>
</select>
</div>
<div class='input-group'>
<input type='submit' name='agregar' value="Agregar">
</div>
</form>
So when the code is interpreted by the server, the <select>
tag appears like this:
<label class='control-label'>Elegir Categoria:</label>
<select class='selectpicker form-control' name='cat_op' data-width='auto'>
<optgroup value='SALAS' label='SALAS'>
<option value=RAWSON> RAWSON</option>
<option value=CAPITAL> CAPITAL</option>
</optgroup>
<option value='SERVICIOS FUNERARIOS'>SERVICIOS FUNERARIOS</option>
<option value='ATAUDES'>ATAUDES</option>
<option value='FLORES'>FLORES</option>
</select>
So, when the submit button is pressed, in my Code Igniter controller named "Principal" and in the method "subir_foto" I can get access to $this->input->post('cat_op') that gives me the option selected when submit, but how can I get the optgroup value also, because I need to have the subcategory and category name.
Well, I finally made the logic in "Principal" controller so I can know if it is a category or a subcategory and know also from wich category inherits.
public function subir_foto(){
$opcion = $this->input->post('cat_op');//obtengo el valor de "option" elegido
$op_row = $this->datos_model->getWhere('sub_categorias',array('nom_subcat'=>$opcion));#busco en la tabla sub_categorias por el nombre. Si lo encuentra, busco en la tabla categoria por id_cat
if(!empty($op_row)){
foreach ($op_row as $row) {
$id_Cat = $row->id_cat;
$subcat = $row->nom_subcat;
}
$cat_row = $this->datos_model->getWhere('categorias',array('id_cat'=>$id_Cat));
foreach ($cat_row as $row) {
$cat = $row->nom_cat;
}
}
else{
$cat = $opcion;
$subcat = null;
}
Then I can Inset the photo in the table of my DB like this (Of course I have skipped the validation part in the code)
$datos = $this->upload->data();
$row = array(
'ruta'=>$datos['file_path'],
'nom_img'=>$datos['file_name'],
'categoria'=>$cat,
'subcat'=>$subcat,
'alt'=>$alt,
'height'=>$datos['image_height'],
'width'=>$datos['image_width'],'size'=>$datos['file_size']);
$this->datos_model->insertar('imagenes',$row);
$data['error'] = 'Archivo '. $datos['file_name'] .' subido exitosamente';
$this->load->view('templates/header', $data);
$this->load->view('pages/admin', $data);
$this->load->view('templates/footer');
This is the Model method "getWhere" when I call it from $this->datos_model->getWhere(table,array);
function getWhere($tabla,$arreglo){
$query = $this->db->get_where($tabla, $arreglo);
if($query->num_rows() > 0){
return $query->result();
}
else{
return null;
}
}
So basically when I get the option selected, I first make a query where "name" = option in the "Subcategory" table of mi DB. If the query is not empty it means that the option corresponds to a subcategory. So now I need to know Of which category underlies that subcat.(I have id_cat as a foreign key in "subcategory" table) So I make another query where "id_cat" = id_cat in my "categories" table of the DB. Finally I can get the name of the father category.