0

[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"
16
  • 1
    What is $s? How did you create it? Commented Jan 8, 2014 at 13:43
  • 1
    I believe it is: $s = $_POST['s'] Commented Jan 8, 2014 at 13:44
  • Yes, it´s $s=$_POST['s'];. I´ve just updated the question. Thanks. Commented Jan 8, 2014 at 13:45
  • 3
    what is var_dump($s) ? Commented Jan 8, 2014 at 13:46
  • 1
    There is something else in you code your are not showing us... Are you doing the gettype outside of the foreach loop? Commented Jan 8, 2014 at 13:51

2 Answers 2

3

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>";
     }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply! But what should I do to get an array? I thought I´ve got one with name="servicios[]", and it was still an array because I´ve used foreach and worked...
I mean, how to get an array again
Not set the variable $servicios to a string. Just change the name of the variable in the 3 CASE statements.
Yes but WITHIN the foreach you are setting it to a string.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.