Skip to content
Advertisement

Fade in SQL results with jQuery

I have a php file that I have pulling in SQL elements from my database. My goal is that for each row of data that the div ‘moredetails’ would fade in on click (one at a time). However, the current script I am using only fades in the first result. So any row after that does not carry the function.

Here is my code:

<?php
    session_start();
    // If the user is not logged in redirect to the login page...
    if (!isset($_SESSION['loggedin'])) {
        header('Location: index.html');
        exit();
    }   

    $userLoggedIn = $_SESSION['name'];

    $con=new mysqli("IPHERE","USERNAMEHERE","PASSWORDHERE","DATABASEHERE");
    if($con->connect_error){
        echo 'Connection Failed: '.$con->connect_error;
        }else{
            $sql="select * from mytable WHERE customer LIKE'%$userLoggedIn%'";

            $res=$con->query($sql);

            while($row=$res->fetch_assoc()){
            echo '<table width="50%" class="tabledetails"><tr><td width="20%">Date: </td><td width="20%">Order: </td><td width="20%">Amount:  </td>
            <tr><td>'.$row["date"], '</td><td>'.$row["num"],'</td><td>'.$row["total"],'</td></tr>
            <tr><td><div class="hidden moredetails">'.$row["description"],'</div></td></tr>


            </table>'

            ;   
            echo "<br><br>";


            }       

        }
?>


<script>
$(document).ready(function(){
  $(this).on("click", function(){
       $(".tabledetails").click(function(){
    $(".moredetails").fadeIn("slow");
       });
  });
});
</script>

Does anyone know if perhaps I need to rework the jQuery here with some sort of .each function or have I missed a step in the SQL/php side?

Note that the class “hidden” is in a css file that says: display:none

Advertisement

Answer

Like i mentioned in the above comment, use classes: https://jsfiddle.net/wwWaldi/3xy4woq7/54/

$('.tabledetails').on('click', function() {
  $(this).find('.moredetails').toggle('slow');
});
1 People found this is helpful
Advertisement