Rosamunda
Rosamunda

Reputation: 14980

How can this be a string and not an array?

[I´ve edited this whole post in order to get the whole script printed. I´ve posted a similar shorter version because I thought it could be easier because of the spanish... but of course... it wouldn´t...]

I have this form:

<b>3. Selecciona una opción</b><br>
<input type ="checkbox" name="servicios[]" value="uno">Un sólo servicio<br>
<input type ="checkbox" name="servicios[]" value="dos">Dos servicios<br>
<input type ="checkbox" name="servicios[]" value="tres">Tres o más<br>

And to get what the users selected I use:

$servicios = $_POST['servicios'];
   if(isset($servicios)){
      foreach ($servicios as $servicio) {
         echo $x."<br>";
         }
   }

Now, I have a database, and with a switch, I get the database equivalent of the element the user selected:

$consulta=mysqli_query($conexion,$query)
if($_SERVER['REQUEST_METHOD']=='POST') {
   while($datosdelabase=mysqli_fetch_assoc($consulta)){
      if(isset($servicios)){
         foreach ($servicios as $servicio) {
         switch ($servicio) {
            case 'uno':
               $servicios=$datosdelabase['uno'];
               break;
            case 'dos':
               $servicios=$datosdelabase['dos'];
               break;
            case 'tres':
               $servicios=$datosdelabase['tres'];
               break;
            }             
            echo "Servicios:".$servicio.": ".$servicios."<br>";
         }
}
...

Inside the database, I´ve got each column with integer values.

That way if the user selects ie. uno. I get "Un sólo servicio" with $servicio and 1 with $servicios (because that´s what I´ve got from the database.

Now when I try to get the type, I get a string and not an array? Why is that?

echo gettype($servicios); // string

var_dump($servicios); // returns: string(2) "Two"

Upvotes: 0

Views: 102

Answers (2)

rccoros
rccoros

Reputation: 590

Because lines like this overwrite the value of $servicios

switch ($servicio) {
   case 'uno':
      $servicios=$datosdelabase['uno'];
      ...
      $servicios=$datosdelabase['dos'];
      ....
      $servicios=$datosdelabase['tres'];

What you should do is to use another variable to get the value of those from $datosdelabase. You should not use $servicios since that's the array you are traversing.

You can try this.

$consulta=mysqli_query($conexion,$query)
if($_SERVER['REQUEST_METHOD']=='POST') {
   while($datosdelabase=mysqli_fetch_assoc($consulta)){
      if(isset($servicios)){
         foreach ($servicios as $servicio) {
         switch ($servicio) {
            case 'uno':
               $serviciosVal=$datosdelabase['uno'];
               break;
            case 'dos':
               $serviciosVal=$datosdelabase['dos'];
               break;
            case 'tres':
               $serviciosVal=$datosdelabase['tres'];
               break;
            }             
            echo "Servicios:".$servicio.": ".$serviciosVal."<br>";
         }
}
...

Where I've just used $serviciosVal instead of $servicios inside the case.

Upvotes: 1

superphonic
superphonic

Reputation: 8074

Because you are setting $servicios here:

$servicios=$datosdelabase['uno'];

or:

$servicios=$datosdelabase['dos'];

etc...

EDITED TO REMOVE ERRONEOUS STAEMENT:

You are doing echo gettype($servicios); which you set in the case, which is a single string from the database

EDIT: This is the code to change:

foreach ($servicios as $servicio) {
     switch ($servicio) {
        case 'uno':
           $servicios=$datosdelabase['uno']; <--- CHANGE THIS to $servicios_2=$datosdelabase['uno'];
           break;
        case 'dos':
           $servicios=$datosdelabase['dos']; <--- CHANGE THIS to $servicios_2=$datosdelabase['dos'];
           break;
        case 'tres':
           $servicios=$datosdelabase['tres']; <--- CHANGE THIS to $servicios_2=$datosdelabase['tres'];
           break;
        }             
        echo "Servicios:".$servicio.": ".$servicios."<br>";
     }

Upvotes: 3

Related Questions