List String in a PHP / jQuery select

1

I have the following string:

  

1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24 ; 25; 26; 27; 28; 29; 30

Stored in the BD in a single field varchar 300 , Can this string be broken into separate variables to feed a select if possible in JavaScript or JQuery?

Example 30 fields in the select:

<select id="numero_cono">
<option disabled="" selected="" value="VACIO">                                                 Seleccione                                                                                        </option>                                                                                       <option value="1">
  1                                                                                             </option>                                                                                   <option value="2">
 2                                                                                             </option>
<option value="3">
 3                                                                                       </option>
                                                                                           

I use this ajax method to send a value to the database and bring a response in json format, in what way can I send the "number_cono" field to a select individually?

   function NumeroConos(id_cono) {
      var ParamObjSend = {
        'id_cono' :id_cono,
      };

  $.ajax({
    type: "POST",
    url: "<?= base_url() ?>AgregarOTController/NumeroConos",
    data: ParamObjSend,
    dataType: 'json',
    success: function(objView) {
      alert(var_dump(objView.NumeroConos.numero_cono));
      var items            = objView.NumeroConos.split(';');   
      alert(var_dump(items));
      $.each(items, function () {
        var option         = document.createElement('option');
        option.value       = $(this);
        option.textContent = $(this);
        $('#numero_conos').append(option);
      });   
    }
  });
}

This is the method that brings the data from the bd and transforms the response to JSon

public function NumeroConos(){
 $Where['id_cono']       =$this->input->post('id_cono');
 $NumeroConos            =$this->MainModel->_sql('Conos',$Where,'');

 $this->output
        ->set_content_type('application/json')
        ->set_output(
            json_encode(array(
                'success'=>true,
                'NumeroConos'=>$NumeroConos

            ))  
    );
  }

This is the json that I send back

  

{"success": true, "NumeroConos": [{"id_cono": "1", "descripcion_cono": "sdfasdf", "color_cono": "Rojo", "numero_conos": "1; 2; 3 ; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28 ; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50 "," token ": "GcKegUhEh7kQAsf35fefkPmBMhwyKQvBFBcJ1W5z720xk9uegy", "State": "0"}]}

This select sends the id to the controller via onchange

<div class="input-group">
<span class="input-group-addon">Color cono</span>
<label class="select">
<select id="id_conos" onchange="NumeroConos(this.value);">
<option disabled="" selected="" value="0">
Seleccione                                                                                    </option>
<?php foreach ($conos as $key =>$value) {?>
<option value="<?= $value->id_cono?>">
<?= $value->color_cono?>
</option>
<?php }?>
</select>
<i>
</i>
</label>
</div>
</section>

This is the select that receives the data

<section class="col col-6">
<div class="input-group">                                                                                    <span class="input-group-addon">Color cono</span>
<label class="select">
<select id="numero_conos">
</select>
<i>
</i>
</label>
</div>
</section>

when I run I get the following error from the console

  

Uncaught TypeError: objView.NumeroConos.split is not a function       at Object.success ((index): 5701)       at l (jquery-2.0.2.min.js: 4)       at Object.fireWith [as resolveWith] (jquery-2.0.2.min.js: 4)       at k (jquery-2.0.2.min.js: 6)       at XMLHttpRequest. (jquery-2.0.2.min.js: 6)

Return

    
asked by Javier Antonio Aguayo Aguilar 23.03.2017 в 15:57
source

3 answers

2

It is enough to divide the text by means of the separator ( ; ) to obtain an array with the elements, iterate them and add them to the select.

What is interesting is to extract numero_conos that is inside the array NumeroConos .

function NumeroConos(id_cono) {
  var ParamObjSend = {
    'id_cono' :id_cono,
  };

  $.ajax({
    type: "POST",
    url: "<?= base_url() ?>AgregarOTController/NumeroConos",
    data: ParamObjSend,
    dataType: 'json',
    success: function(objView) {
      var items = objView.NumeroConos[0].numero_conos.split(';');

      $.each(items, function () {
        var option = document.createElement('option');
        option.value = $(this);
        option.textContent = $(this);
        $('#id_cant_combustible').append(option);
      });
    }
  });
}

First we access NumeroConos , then the first and only object ( [0] ) and finally the property numero_conos .

    
answered by 23.03.2017 в 16:23
1

You can use the function explode in php that returns an array with the data separated by your delimiter

$array = explode(";", $cadena);

and then fill the select by sweeping the array in a cycle

    
answered by 23.03.2017 в 16:01
1

You need to use the split () method:

var resultado = "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30";
var array = resultado.split(";");

You'll get an array of 30 items. I hope it helps you to do the SELECT you need.

    
answered by 23.03.2017 в 16:02