dongyan7172 2017-06-20 20:51
浏览 38
已采纳

如何从codeigniter中选择有关“optgroup”的信息?

Im 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') thats 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. I hope I have expressed my problem well

This is how the selectpicker is displayed

Thanks, is my first question.

  • 写回答

2条回答 默认 最新

  • doujing1967 2017-06-22 15:56
    关注

    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.

    I hope I made myself clear. Thanks

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器