Unable to initialize datatables in html within a php file

Unable to initialize datatables in html within a php file

nospipinospipi Posts: 5Questions: 2Answers: 0
edited March 2021 in Free community support

i transfered my entire html to a php file,to be able to work with a mysql database i already have,and i cant seem to be able to initialize properly,its not working,any ideas ?

<?php
header('Access-Control-Allow-Origin: *');

//connect to db
$conn = mysqli_connect('localhost','*****','********','***********');

//check connection
if(!$conn){
    echo 'Connection error: '  . mysqli_connect_error();
}

//WRITE QUERY FOR ALL RECORDS
$sql = 'SELECT * FROM forms ORDER BY systemdate DESC';

//MAKE QUERY AND GET RESULTS
$result = mysqli_query($conn, $sql);

//FETCH THE RESULTING ROWS AS AN ARRAY
$forms = mysqli_fetch_all($result, MYSQLI_ASSOC);

////INSERT INTO DATABASE
//if(isset ($_REQUEST["name"]) ){
//$name = $_REQUEST["name"];
//$content = $_REQUEST["content"];
//$datetime = $_REQUEST["datetime"];
//    
//$sql1 = "INSERT INTO forms (name, contents, date)
//VALUES ('$name', '$content','$datetime')";
//    
//if ($conn->query($sql1) === TRUE) {
//  echo "New record created successfully";
//  exit;
//  }else {
//  echo "Error: " . $sql1 . "<br>" . $conn->error;
//  exit;
//  }
//};
//
////DELETE DB RECORD
//if(isset ($_REQUEST["delete"]) ){
//$id = $_REQUEST["delete"];
//$sqlId = "DELETE FROM forms WHERE id=$id ";
//if ($conn->query($sqlId) === TRUE) {
//  echo "Form deleted from database succesfully";
//  exit;
//  }else {
//  echo "Error: " . $sqlId . "<br>" . $conn->error;
//  exit;
//  }
//};

////FREE RESULT FROM MEMORY
//mysqli_free_result($result);
    
////CLOSE CONNECTION
//mysqli_close($conn);


<?php
>
```
?>



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>Material Design for Bootstrap</title>
<!-- MDB icon -->
<link rel="icon" href="img/mdb-favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.2/css/all.css" />
<script src="https://kit.fontawesome.com/2608ace323.js" crossorigin="anonymous"></script>
<script src="main.js"></script>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap"
/>
<!-- MDB -->
<link rel="stylesheet" href="css/mdb.min.css" />
<script type="text/javascript">

  </script>

</head>
<body>
<!-- Start your project here-->

TEST <?php foreach ($forms as $form){?> <?php }?>
ID NAME DATE CREATED
<?php print htmlspecialchars($form['id']); ?> <?php print htmlspecialchars($form['name']); ?> <?php print htmlspecialchars($form['date']); ?>
<!-- End your project here-->

<style>

#tablewrap{
position: absolute;

border: 1px solid grey;
padding: 2px;
top: 50px;
left: 50px;

}

.dataTables_filter{
    float: right!important;
    margin-bottom: 2px;
}
.dataTables_filter input {

margin-left: 5px;
height: 23px;
}

</style>
<!-- MDB -->




$(document).ready(function () { $('#dtVerticalScrollExample').DataTable({ "paging": false, "bInfo" : false, "scrollY": "100px", "scrollCollapse": true }); $('.dataTables_length').addClass('bs-select'); }); $(document).on('click', '#testbutton', function() { alert("poutsa"); });
    <!-- Custom scripts -->

</body>
</html>
```

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

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    What exactly happens? Do you get errors in the browser's console?

    Kevin

  • nospipinospipi Posts: 5Questions: 2Answers: 0

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954
    edited March 2021 Answer ✓

    In the future you can search the forum for error messages like this and find threads with the solution. In your case the problem is you defined 3 columns in your thead but have 4 columns in your rows. For Datatables to work and add its controls like sorting the table needs to have the same number of columns in the thead as is in the table rows.

    Kevin

  • nospipinospipi Posts: 5Questions: 2Answers: 0

    understood,thank you.

This discussion has been closed.