When I export to excel, I just get the first row of the query

When I export to excel, I just get the first row of the query

Rick33Rick33 Posts: 2Questions: 1Answers: 0
edited May 2023 in Free community support
$con = mysqli_connect($host,$user,$password,$database);
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
} 


$sql = "SELECT `Fecha` , `Astronomo` , `Asistente1`, `Asistente2`, `Instrumento`   FROM `BitObs`  where `Fecha` Between '$datepicker' and '$datepicker2'" . " order by `Fecha` ASC";

if($result = mysqli_query($con, $sql)){
    if(mysqli_num_rows($result) > 0){
        echo "<center>";
        echo "<table class=display nowrap' id='example' style='width:100%' border = '1'>";
           echo "<thead>";
            echo "<tr>";
                echo "<th>Fecha</th>";
                echo "<th>Astronomo</th>";
                echo "<th>Asistente1</th>";
                echo "<th>Asistente2</th>";
                echo "<th>Instrumento</th>";
                
            echo "</tr>";
            echo "</thead>";        

        while($row = mysqli_fetch_array($result)){
            echo "<tbody>";
            echo "<tr>";
                echo "<td>" . $row['Fecha'] . "</td>";
                echo "<td>" . $row['Astronomo'] . "</td>";
                echo "<td>" . $row['Asistente1'] . "</td>";
                echo "<td>" . $row['Asistente2'] . "</td>";
                echo "<td>" . $row['Instrumento'] . "</td>";
               
           echo "</tr>";
           echo "</tbody>";

        }
        echo "</table>";
       
        mysqli_free_result($result);
    } else{
        echo "No se encontro ningun registro con tu criterio de busqueda.";
    }
} else{
    echo "ERROR: No se pudo ejecutar $sql. " . mysqli_error($link);
}

//mysqli_close($connection);



?>

    <script>
        
    $(document).ready(function() {
        +$('#example').DataTable( {
      searching: false,
        dom: 'Bfrtip',
        buttons: [
           { extend: 'excel', exportOptions: { modifier: { page: 'all', search: 'none' } } },
            ]
            } );
        } );
    </script>


</div>

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has accepted answers - jump to:

Answers

  • tangerinetangerine Posts: 3,349Questions: 37Answers: 394
    Answer ✓

    Your tbody tag is inside a loop. HTML only permits one tbody.

  • allanallan Posts: 61,715Questions: 1Answers: 10,106 Site admin
    Answer ✓

    Minor correction on a technical point there - HTML does actually allow multiple tbody elements inside a table. DataTables however only operates on a single tbody.

    So the fix is exactly what tangerine says!

    Allan

  • Rick33Rick33 Posts: 2Questions: 1Answers: 0

    Made corrections, working great. Thank you all!!

Sign In or Register to comment.