generation and sending of array php by post

0

I have a mysql query:

while ($qry = mysql_fetch_array($res)){

  $id_prod = $qry["id"];
  $id_venta = $qry["id_venta"];
  $idcl = $qry["id_cl"];
  $garzon = $qry["usuario"];
  $valor_total = $qry["valor"]; 
  $prod= $qry['producto']; --> esta puede tener uno o mas valores
}

What I need with this query, generate an array of $ prod, send it and receive it by post on another page. what gives me back said variables is Array. I have tried several options but none works. among them:

$data = array(); 
$data[$qry['producto']] = $qry['producto'];

and send it in the form like this:

<input type="hidden" name="prod[]" value="<?=$data?>" >

and receiving asi (ejecutor.php)

$prod = $_POST["prod"];

$i = 1;
foreach ($prod as $e)
{
    echo "<br>".$e;
    $i++;
}

I have read and tried many alternatives and none works as it should. As mentioned above, the variable $ data can have 1 to X records, for example: Product 1 can sell 1 to X quantity. Product 2 can sell 1 quantity. and so with one or more products per sales. I hope you can guide me.

    
asked by maha1982 07.02.2018 в 15:52
source

3 answers

0

validate your query look at this is an example that I just did html send to your controller or model I do not know how you are doing

       <div class="form-group col-md-3">
            <label class="control-label">Experiencia</label>
            <select class="form-control input-sm selectpicker" name="Datos[experiencia]" data-error="Es un campo obligatorio" required="required" id="experiencia">                           
                  <option value="ninguna">ninguna</option>
                  <option value="6 meses" selected="selected">6 meses</option>
                  <option value="1 año">1 año</option>
                  <option value="2 años">2 años</option>
                  <option value="5 años">3 años</option>
            </select>
          </div>

in the contralador I receive and send again to the view

$resultado = $this->varPerfil ->guardar($_POST["Datos"]);

model I receive array parameter and I return the result of the query to the controller

public function guardar($datos){
$experiencia = $this->conexion -> real_escape_string(strip_tags(stripslashes(trim($datos["experiencia"])))); //escapar caracteres

$consulta = "INSERT INTO spartodo_rh.tblPerfiles ('experiencia') VALUES ('$experiencia')";
}

here is another one I am saving the (id) of the select as a string in the database to field skills 1,2,3,4,5

          <div class="form-group col-md-6">
            <label class="control-label">Habilidades</label>
            <select class="form-control input-sm selectpicker" name="habilidades[]" multiple data-error="Es un campo obligatorio" data-live-search="true" required="required" id="habilidades">
              <?php foreach ($habilidades as $habilidad) {?> 
              <option value="<?php echo $habilidad['idHabilidades']; ?>"><?php echo $habilidad['habilidad']; ?></option>
              <?php } ?>
            </select>
          </div>

controller

$resultado = $this->varPerfil -> guardar($_POST["habilidades"]);

model

    public function guardar($habilidad){
    $habilidad = implode(",", $habilidades);
                $habilidad = $this->conexion -> real_escape_string(strip_tags(stripslashes(trim($habilidad))));

$consulta = "INSERT INTO spartodo_rh.tblPerfiles ('habilidad') VALUES ('$habilidad')";
    }
    
answered by 07.02.2018 / 16:25
source
0

First

You have to make sure that the query is fetching data.

Second

Declare the array before while :

$data=array();

Third

Fill the array within while , like this:

while ($qry = mysql_fetch_array($res)){

  $data['id'] = $qry["id"];
  $data['id_venta'] = $qry["id_venta"];
  $data['id_cl'] = $qry["id_cl"];
  $data['usuario'] = $qry["usuario"];
  $data['valor'] = $qry["valor"]; 
  $data['producto'] = $qry['producto']; --> esta puede tener uno o mas valores
}

/*Si esto no muestra nada, tienes problemas con tu consulta*/
var_dump($data);

Fourth

You can serialize the array to send it:

<input type="hidden" name="prod[]" value="<?=htmlspecialchars(serialize($data));?>" >

Fifth

In the file you receive, you can use unserialize to recover the array.

$datos = unserialize($_POST['prod']);
print_r($datos);
//Trabaja con tus datos como quieras...
    
answered by 07.02.2018 в 16:24
0

There are many ways to do this but I will show you the common one and for me the fastest one

1.- You can make a for of the variable and save them in your hidden field

for ($i = 0;i<count($data); $i++){
    echo '<input type="hidden" name="prod[]" value="'.$data[$i].'" >';
}

2.-Serializing the array

As everything is known in the programming there are other more practical ways

<input type="hidden" name="result" value="<?php echo  base64_encode(serialize($data)); ?>">

and when you receive it, it deserializa saving you the for

$producto = ( unserialize(base64_decode($_POST['result'])); );
  

The base64 is to avoid problems with the double quote

    
answered by 07.02.2018 в 16:33