Search in the database with select

Asked

Viewed 415 times

-6

I am developing a website to display registered videos, for this I did with a select to select the desired discipline of the user. Soon after I put a button to display these registered videos pulling from MYSQL (based on the selected discipline).

How can I get the id of select and search, based on this id of the selected discipline, the uploaded videos?

I have a part of a previous code, but it was used to identify the logged in user, can serve as a basis for solving the problem.

Search code in mysql:

   <?php

    $disciplina = $_SESSION['disciplina_prof'];
    $servidor = 'localhost';
    $usuario  = 'root';
    $senha    = 'root';
    $banco    = 'onteach';
    $conexao  = mysqli_connect($servidor, $usuario, $senha, $banco);

    $consulta = "SELECT
                    *
                FROM
                    video_monitor
                WHERE
                    video_monitor.disciplina     = '$disciplina'
                    AND video_monitor.pendente = 1";

    $resultados       = mysqli_query($conexao, $consulta);
    $videos_pendentes = array();

    while ($video = mysqli_fetch_assoc($resultados))
        $videos_pendentes[] = $video;

    mysqli_free_result($resultados);
    mysqli_close($conexao);

    $videos_pendentes = json_encode($videos_pendentes);

?>

Script code to select the data that will appear in the Carousel: (modify to the DISCIPLINE ID, 1 for "Physics", 2 for "Mathematics") The select of this discipline is in html like this:

<div class="box">
                        <select name="disciplina_prof" id="disciplina_prof">
                          <option value="" selected=selected>Selecione uma disciplina</option>
                          <?php
                          if($num_logar > 0) {
                              do {
                              echo "<option value='".$fet_logar['disciplina_id']."'>".$fet_logar['disciplina_nome']."</option>";
                              }while($fet_logar = mysqli_fetch_assoc($exe_logar));
                          }
                          ?>          
                        </select>
                        <?php print_r($consulta);?>
                      </div>

An example that after mysql selected the discipline these would be displayed in Carousel:

 $('#btn-lista-videos-carousel').click(function() {
            let itens       = '';
            let indicadores = '';

            if (videos_pendentes.length > 0) {

                videos_pendentes.forEach(function(video, indice) {
                    indicadores += '<li data-target="#carouselExampleIndicators" data-slide-to="' + indice + '" class="' + (indice == 0? 'active' : '') + '"></li>'

                    itens += '<div class="carousel-item ' + (indice == 0? 'active' : '') + '">';
                    itens += '    <iframe  width="100%" height="350" src="https://www.youtube.com/embed/'+ video.link_video +'" frameborder="0" allowfullscreen></iframe>' 
                    itens += '</div>';

                });

                console.log(indicadores);

                $('#carouselExampleIndicators').show();
                $('#carousel-indicadores').html( indicadores );
                $('#carousel-itens').html( itens );

            } else {

                alert('Nenhum vídeo pendente');

            }
        });


        $('.carousel').carousel();

    });

</script>
  • Probably the table should have a field id, just call this field in the query as well

  • You need to improve the way you are exposing the problem... To do this dynamically you need Ajax, but first you must take the id... So what’s the problem in question, getting the id or do or Ajax?

  • 2

    You cannot keep changing the question except to improve details, when you invalidate answers given the question was bad and the solution is not to do another.

  • 2

    That’s why you have to work on the posts, the system punishes repeated bad questions. There was a sign from you that the answer was no longer valid for the question and it doesn’t seem to be the same question.

  • @Matheus You need to make an Ajax call based on select, there are several examples on the site gives a search

  • @Matheus does not skip steps, otherwise you will be trapped in a loop of copying and pasting codes that will never consciously take you to your destination. Stop your code a bit and understand the foundation of how Ajax technology works so you can implement it in your code.

  • Matheus@ and then brother already managed to solve the problem ?

  • @Michaelcosta not yet :(

  • @Matheus, at this moment, what is the problem ? ta giving error ?

  • As far as I understand it, you want the person to select which discipline they want to see, after that, you want a button to send the chosen discipline of select to store in your Sesssion['disciplina_prof']; that’s it ?

  • That would be right @Michaelcosta

  • But I will edit to put the discipline select for you

Show 8 more comments

2 answers

1


Bro, from what I understand, you don’t have to do those two actions. you can do it like this. using a select that sends straight to Session when selected. I think that’s what you want

this php part, you will receive the data on the page itself.

$_SESSION['disciplina_prof'] = '';

if(isset($_GET['disciplina']))
{
    $_SESSION['disciplina_prof'] = $_GET['disciplina'];
    $disciplina = $_SESSION['disciplina_prof'];
} else {
    $_SESSION['disciplina_prof'] = '';
    $disciplina = $_SESSION['disciplina_prof'];
}

$selected = function($check) {
if($_SESSION["disciplina_prof"] == $check) {
return 'selected';
}
};

?>

example of use:

<select name="forma" onchange="location = this.value;">
 <option <?php echo ($_SESSION['disciplina_prof'] == 0)  ? 'selected' : '' ; ?> value="index.php?disciplina=null"></option>
 <option <?php echo ($_SESSION['disciplina_prof'] == 1)  ? 'selected' : '' ; ?> value="index.php?disciplina=1">Física</option>
 <option <?php echo ($_SESSION['disciplina_prof'] == 2)  ? 'selected' : '' ; ?> value="index.php?disciplina=2">Matemática</option>
</select>

adapted to your select code.

<select name="disciplina_prof" id="disciplina_prof" onchange="location = this.value;">
    <option <?php echo ($_SESSION['disciplina_prof'] == 0)  ? 'selected' : '' ; ?> value="professor.php?disciplina=null">Selecione uma disciplina</option>
    <?php
    if($num_logar > 0) {
        do {
        echo "<option ". $selected($fet_logar['disciplina_id']) ." value=professor.php?disciplina=".$fet_logar['disciplina_id'].">".$fet_logar['disciplina_nome']."</option>";
        }while($fet_logar = mysqli_fetch_assoc($exe_logar));
    }
    ?>
</select>

  • There is a small change friend, these disciplines, can not be pre-selected, as in the question above, it pulls by mysql. From a look there, how could I change to go automatic?

  • ready adapted in your code. and now solved ?

  • The disciplines that appeared in mysql no longer appear. Take a look at this print: https://prnt.sc/s0th0s

0

Hello, at this point where you say:

I’m developing a website to display registered videos, to this I did with a select to select the desired discipline of user.

I’m seeing the video table query, but the user table query is missing since you want to show the user information;

example:

SELECT * FROM video_monitor a
inner join usuario b on a.usuarioid = b.ID
WHERE video_monitor.disciplina = '$disciplina'
AND video_monitor.pendente = 1
AND B.ID = 1
  • @Matheus, "I can do to get the id of select"within your $results, you have all the table fields, you can get the id by returning in the query

  • in the query will already bring everything you need, take the contents of the $results and see

  • I edited the question, if you could take a look

  • Anyway, I want to show you the information regarding the selected discipline in select. There at the beginning of the code, as an example $discipline = $_SESSION['disciplina_prof'];

Browser other questions tagged

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