Obtain entire division Days

0

I am currently from a form, obtaining the days from Monday to Sunday as follows:

     <td><dl class="dropdown"> 
          <dt>
         <a href="#">
           <span class="hida">Dias</span>    
           <p class="multiSel"></p>  
         </a>
         </dt>
       
         <dd>
             <div class="mutliSelect">
                 <ul>
                     <li>
                         <input type="checkbox" name="dias[]" value="64" />Lunes</li>
                     <li>
                         <input type="checkbox" name="dias[]" value="32" />Martes</li>
                     <li>
                         <input type="checkbox" name="dias[]" value="16" />Miercoles</li>
                     <li>
                         <input type="checkbox" name="dias[]" value="8" />Jueves</li>
                     <li>
                         <input type="checkbox" name="dias[]" value="4" />Viernes</li>
                     <li>
                         <input type="checkbox" name="dias[]" value="2" />Sabado</li>
     				<li>
                         <input type="checkbox" name="dias[]" value="1" />Domingo</li>
                 </ul>
             </div>
         </dd>
     </dl></td>

The numbers are because to insert in my mysql table, I must insert a single value, and I do not want to insert, Monday, Tuesday, Wednesday, etc. So I'm inserting the total that I get this way in the POST

 //Saber la suma de los dias 
$dias = $_POST['dias'];  
$suma = 0; 
if(count($dias) > 0){   

foreach($dias as $valor){  

$suma_d += $valor;  

} 

} 

The issue is that now I need to present through html what days I chose and I must convert the number to the days, the formula would be for example if I choose, Tuesday, Wednesday and Friday

32 + 16 + 4 = 52 52 fits in 64 = No - Then it's not Monday 52 fits in 32 = Yes - Then it's Tuesday and I'm still 52 - 32 * 1 = 20 20 fits in 16 = Yes- Then it's Wednesday and I'm still 20 - 16 * 1 = 4 4 fits in 8 = No then it's not Thursday 4 fits in 4) Yes - Then it's Friday and I'm still 0 I can not continue. The days are Tuesday, Wednesday and Friday.

This is how I should do it but I do not know how to present it in PHP.

    
asked by Lusag silva 30.05.2018 в 21:56
source

1 answer

0

Without modifying your code much, what you could do is convert $suma_d to binary using the function decbin , and then add 10000000 to make sure you always have 8 digits, of which the first one is just stuffed.

<?php

// lunes $suma_d = 64
$binario = decbin($suma_d)+10000000;  // 11000000

// martes, miércoles y viernes $suma_d = 52
$binario = decbin($suma_d)+10000000;  // 10110100

You keep that binary in your database, and when you retrieve it you can go through the value by treating it as a string. In PHP the strings behave like arrays in the sense that you can choose the umpteenth character using $texto[$n] , so if you do:

$martes = (strval($binario))[2];

you will have a% or a% in% co. And with that you can reconstruct the selection.

EDIT

When you want to recover the days, you can do

$resultado=10110100; // esto lo obtienes de la BBDD
$res_str=strval($resultado);

$dias=['Lunes','Martes','Miércoles','Jueves','Viernes','Sábado','Domingo'];

for($i=1; $i<=7; $i++) {
  if($res_str[$i]) {
    echo $dias[$i-1].'<br>';
  }

}
    
answered by 31.05.2018 / 00:33
source