Problem with Dynamic Input

Asked

Viewed 45 times

0

I am developing a dynamic input that checks whether the database contains a value if there is such value should not appear in the input, but I am experiencing problems with my logic:

<?php
session_start();
require_once('library.php');
$hoje = date('d/m/Y');
$amanha = date("d/m/Y", mktime(0,0,0,date("m"),(date("d")+1),date("Y"))); 
$damanha = date("d/m/Y", mktime(0,0,0,date("m"),(date("d")+2),date("Y")));



$predata = $_POST['predata'];
 //Realizo a consulta no banco  de dados
$sql = "SELECT Hora FROM tbl_agenda WHERE Data = '$predata'";

 //Atualizo algumas informações
$atualizahoje = "UPDATE tbl_agenda SET Data='". $hoje ."' WHERE id=33";
$atualizaamanha = "UPDATE tbl_agenda SET Data='". $amanha ."' WHERE id=34";
$atualizadamanha  = "UPDATE tbl_agenda SET Data='". $damanha ."' WHERE id=35";
$qr1 = mysql_query($atualizahoje) or die(mysql_error());
$qr2 = mysql_query($atualizaamanha) or die(mysql_error());
$qr3 = mysql_query($atualizadamanha) or die(mysql_error());
$qr = mysql_query($sql) or die(mysql_error());
$ln1 = mysql_fetch_assoc($qr1);
$ln2 = mysql_fetch_assoc($qr2);
$ln3 = mysql_fetch_assoc($qr3);


    echo '<option value="">Selecione..</option>';

// Realizo o while verificando os critérios e adicionando o select ao campo
           while($ln = mysql_fetch_assoc($qr)){

           if($ln['Hora'] != "00:00") {
           echo '<option value="00:00">00:00</option>';


            }
           else {
            echo '<option hidden value="00:00">00:00</option>';
           } 

            if($ln['Hora'] != "01:00") {
              echo '<option value="01:00">01:00</option>';

            }
            else {

             echo '<option hidden value="01:00">01:00</option>';
                }


                 if($ln['Hora'] != "02:00") {
                   echo '<option value="02:00">02:00</option>';
            }
            else {
              echo '<option hidden value="02:00">02:00</option>';
                }



                 if($ln['Hora'] != "03:00") {
                  echo '<option value="03:00">03:00</option>';

            }
            else {
               echo '<option hidden value="03:00">03:00</option>';
                }



                 if($ln['Hora'] != "04:00") {
                 echo '<option value="04:00">04:00</option>';

            }
            else {
        echo '<option hidden value="04:00">04:00</option>';
                }



                 if($ln['Hora'] != "05:00") {

                 echo '<option value="05:00">05:00</option>';

            }
            else {
            echo '<option hidden value="05:00">05:00</option>';
                }



                 if($ln['Hora'] != "06:00") {
            echo '<option value="06:00">06:00</option>';
            }
            else {
             echo '<option hidden value="06:00">06:00</option>';
                }



        if($ln['Hora'] != "07:00") {
            echo '<option value="07:00">07:00</option>';
            }
            else {
             echo '<option hidden value="07:00">07:00</option>';
                }



                 if($ln['Hora'] != "08:00") {
            echo '<option value="08:00">08:00</option>';
            }
            else {
             echo '<option hidden value="08:00">08:00</option>';
                }



                  if($ln['Hora'] != "09:00") {
            echo '<option value="09:00">09:00</option>';
            }
            else {
             echo '<option hidden value="09:00">09:00</option>';
                }



                 if($ln['Hora'] != "10:00") {
            echo '<option value="10:00">10:00</option>';
            }
            else {
             echo '<option hidden value="10:00">10:00</option>';
                }

In the database I have the record Data = 28/06/2016 e Hora = 00:00, in my input then when selecting the day 28/06/2016 the value of 00:00 should not be displayed, I already made the test of $_POST and the value is coming correctly, I believe the error is inside While.

  • In the database, what is the type of the date field: DATE, DATETIME or VARCHAR?

  • Good morning, the field is TEXT.

  • You want to list all the times and not show what’s in the bank, that’s it?

  • This I want to list from 00:00 to 23:00, but if in the database has the value 01:00 I want the input to look like this 00:00 - 02:00 - 03:00 [...]

  • You are using "Hidden" would not be better not to print?

  • Already tried Ivan, Hidden was the last attempt, the problem is that in the bank I have several values, today for example I have 4, in the first round the time loop is = 00:00 in the second is = 01:00 (At that moment appears the 00:00 and erases the 01:00. I need to create a logic that does not occur this problem.

Show 1 more comment

1 answer

1


I think that’s what you need (Obs: made up to 24, correct?)...

$horas_hidden = array();
while ($ln = mysql_fetch_assoc($qr)) {
    $horas_hidden[] = $ln['Hora'];
}
echo montarOptions($horas_hidden, 24);

function montarOptions($horas_hidden, $total_horas)
{
    $html='<option value="">Selecione..</option>';
    for ($i=0; $i <= $total_horas; $i++) {
        $hora = ($i < 10) ? '0'.$i : $i;
        $hora .=':00';

        if (in_array($hora, $horas_hidden)) {
            //hidden só para HTML5
          $html.= "<option hidden value=\"{$hora}\">{$hora}</option>";
        } else {
          $html.= "<option value=\"{$hora}\">{$hora}</option>";
        }
   }
   return $html;
}
  • It is printing the values thus : "1,2,3,4,5,6,7,8,9,10...1,2,3,4,5,6,7,8,9,10" and repeating, I am looking and trying to adjust.

  • edited, it was the += signal changed to .=

  • The problem of showing the value in the database persisted.

  • But isn’t that supposed to be for every piece of data in the collection? If not, then you can filter using "group by" in the database, or instead of doing while, take only the first record of the array.

  • What’s coming from this consultation? $sql = "SELECT Hora FROM tbl_agenda WHERE Data = '$predata' ";

  • Just one observation: then read this

  • 00:00 / 01:00 / 03:00 / 10:00

  • See now if that’s what you want?

  • Apparently solved, I will perform some more tests and I return to confirm as solved, thanks friend.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.