Added to a PHP table

Added to a PHP table

danjesamadanjesama Posts: 3Questions: 1Answers: 0
edited March 2017 in Free community support

Hi I have a site with a table already made and I was trying to add datatables to it. here is the code of my whole page:
(I have followed the instructions beforehand)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
    <link rel="stylesheet" type="text/css" href="css/style.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>

<title>View Records</title>

</head>

<body>
    
    <div class="page-wrap">
    
    <center><a href="https://repo-project-danielmurran.c9users.io/view.php"><img src="/images/logo.png" alt="what image shows" height="90" width="300"></a></center>
    
    <h1>GCS Reproduction Environment</h1></br>
    
    <hr/>

    <h2>All Reproductions</h2>

<?php

/*

VIEW.PHP

Displays all data from 'players' table

*/


// connect to the database

include('php/connect-db.php');

// ordering the ID function

$ascdesc = "";
$order = isset($_GET['sort'])?$_GET['sort']:'id';

$ascdesc = isset($_GET['ascdesc'])?$_GET['ascdesc']:'DESC';
switch(strtoupper($ascdesc))
  {
  case 'DESC': $ascdesc = 'ASC'; break;
  case 'ASC': $ascdesc = 'DESC'; break;
  default: $ascdesc = 'DESC'; break;
  }


// get results from database

$result = mysql_query("SELECT * FROM players ORDER BY $order $ascdesc")

or die(mysql_error());



// display data in table


echo "<table id='#myTable' align='center' border='1' cellpadding='2'>";
echo "<thead>";
echo "<tbody>";
echo "<tr> <th>ID</th> <th>Administration Console</th> <th>Product Version</th> <th>Platform</th> <th>Database</th> <th>Owner</th> <th>Status</th> <th></th> <th></th></tr>";
echo "</thead>";
echo "</tbody>";


// loop through results of database query, displaying them in the table

while($row = mysql_fetch_array( $result )) {



// echo out the contents of each row into a table

echo "<tr>";

echo '<th>' . $row['id'] . '</th>';

echo '<td><a href="'.$row['curl'].'">'.$row['curl'].'</a></td>';

echo '<td>' . $row['pversion'] . '</td>';

echo '<td>' . $row['platform'] . '</td>';

echo '<td>' . $row['dversion'] . '</td>';

echo '<td><a href="mailto:'.$row['email'].'@informatica.com">' . $row['email'].'</a></td>';

echo '<td>' . $row['status'] .'</td>';

echo '<td><a href="php/edit.php?id=' . $row['id'] . '">Edit</a></td>';

echo '<td><a onclick="javascript:confirmationDelete($(this));return false;" href="php/delete.php?id=' . $row['id'] . '"onclick="return confirm("Do you want to delete this?")">Delete</a></td>';

echo "</tr>";

}





// close table>

echo "</table>";


<?php
>
?>


<script>
    function confirmationDelete(anchor)
{
   var conf = confirm('Are you sure want to delete this record?');
   if(conf)
      window.location=anchor.attr("href");
}


$("tr").not(':first').hover(
  function () {
    $(this).css("background","yellow");
  },
  function () {
    $(this).css("background","");
  }
);

$(document).ready(function(){
    $('#myTable').DataTable();
});

</script>



<p><a href="php/new.php">Add a new record</a></p>


</div>
</body>

</html>

Answers

  • danjesamadanjesama Posts: 3Questions: 1Answers: 0
    edited March 2017

    Thanks in advance

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

    It looks like your code is creating the table incorrectly:

    echo "<table id='#myTable' align='center' border='1' cellpadding='2'>";
    echo "<thead>";
    echo "<tbody>";
    echo "<tr> <th>ID</th> <th>Administration Console</th> <th>Product Version</th> <th>Platform</th> <th>Database</th> <th>Owner</th> <th>Status</th> <th></th> <th></th></tr>";
    echo "</thead>";
    echo "</tbody>";
    

    The header should come before the tbody and the rows should be within the tbody.

    Kevin

  • danjesamadanjesama Posts: 3Questions: 1Answers: 0

    Hi Kevin
    I have edited so it is like below, as its PHP its confusing me a little where to put everything:

    echo "<table id='#myTable' align='center' border='1' cellpadding='2'>";
    echo "<thead> <tr> <th>ID</th> <th>Administration Console</th> <th>Product Version</th> <th>Platform</th> <th>Database</th> <th>Owner</th> <th>Status</th> <th></th> <th></th></tr> </thead>";
    
    
    
    
    // loop through results of database query, displaying them in the table
    
    while($row = mysql_fetch_array( $result )) {
    
    
    
    // echo out the contents of each row into a table
    echo "<tbody>";
    
    echo "<tr>";
    
    echo '<th>' . $row['id'] . '</th>';
    
    echo '<td><a href="'.$row['curl'].'">'.$row['curl'].'</a></td>';
    
    echo '<td>' . $row['pversion'] . '</td>';
    
    echo '<td>' . $row['platform'] . '</td>';
    
    echo '<td>' . $row['dversion'] . '</td>';
    
    echo '<td><a href="mailto:'.$row['email'].'@informatica.com">' . $row['email'].'</a></td>';
    
    echo '<td>' . $row['status'] .'</td>';
    
    echo '<td><a href="php/edit.php?id=' . $row['id'] . '">Edit</a></td>';
    
    echo '<td><a onclick="javascript:confirmationDelete($(this));return false;" href="php/delete.php?id=' . $row['id'] . '"onclick="return confirm("Do you want to delete this?")">Delete</a></td>';
    
    echo "</tr>";
    
    echo "</tbody>";
    
    }
    
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Your tbody tags should be outside your while loop.

This discussion has been closed.