Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function resultToArray($result) {
- $rows = array();
- while($row = $result->fetch_assoc()) {
- $rows[] = $row;
- }
- return $rows;
- }
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "discgolf";
- $mysqli = new mysqli("$servername", "$username", "$password", "$dbname");
- if ($mysqli->connect_error){
- die("Connection failed: " . $mysqli->connect_error);
- }
- if(isSet($_POST["action"])){
- switch($_POST["action"]){
- case "removeRow":
- $row_id = $_POST["ID"];
- $stmt = $mysqli->prepare("DELETE FROM discideas WHERE id = ?");
- $stmt->bind_param("i", $row_id);
- $stmt->execute();
- exit();
- break;
- case "saveRows":
- $input_values = $_POST["input_values"];
- foreach($input_values as $input_row){
- if($input_row[0] == "no"){
- $type = $input_row[1];
- $brand = $input_row[2];
- $name = $input_row[3];
- $plastic = $input_row[4];
- $stmt = $mysqli->prepare("INSERT INTO discideas(type, brand, name, plastic) VALUES(?,?,?,?)");
- $stmt->bind_param("ssss", $type, $brand, $name, $plastic);
- $stmt->execute();
- }else{
- $id = $input_row[0];
- $type = $input_row[1];
- $brand = $input_row[2];
- $name = $input_row[3];
- $plastic = $input_row[4];
- $stmt = $mysqli->prepare("UPDATE discideas SET type=?, brand=?, name=?, plastic=? WHERE id = ?");
- $stmt->bind_param("ssssi", $type, $brand, $name, $plastic, $id);
- $stmt->execute();
- }
- }
- $results = $mysqli->query("SELECT id, type, brand, name, plastic FROM discideas;");
- $newResults = resultToArray($results);
- echo json_encode($newResults);
- exit;
- break;
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <!-- Jquery -->
- <script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
- <!-- Bootstrap -->
- <script src="smart/node_modules/bootstrap/dist/js/bootstrap.js"></script>
- <link rel="stylesheet" href="../disc/smart/node_modules/bootstrap/dist/css/bootstrap.css">
- <link rel="stylesheet" href="../disc/smart/node_modules/bootstrap/dist/css/bootstrap-theme.css">
- <style type="text/css">
- #discideas caption
- {
- text-align: center;
- font-weight: bold;
- }
- #discideas
- {
- width: 500px;
- }
- #edit
- {
- text-align: center;
- width: 500px;
- }
- #wat
- {
- width: 500px;
- }
- </style>
- <script>
- $(document).ready(function(){
- $(document).on("click", "#edittable", function(e){
- e.preventDefault();
- $(this).val("Save");
- $(this).attr("id", "saveBtn");
- $("#add").show();
- $("#cancel").show();
- $("table tbody tr:not(.empty)").each(function(){
- $(" td", this).each(function(){
- var cell_content = $(this).text();
- $(this).html("<input type='text' value='"+cell_content+"'>");
- });
- $(this).append("<td class='removeCell'><a href='#' class='deleteRow' data-id='"+ $(this).data("id") +"'>Kustuta</a></td>");
- });
- return false;
- });
- $(document).on("click", "#cancel", function(e){
- e.preventDefault();
- $("#saveBtn").val("Edit");
- $("#saveBtn").attr("id", "edittable");
- $("#add").hide();
- $("#cancel").hide();
- $("table tbody tr:not(.empty)").each(function(){
- var isEmpty = 0;
- $(" td input", this).each(function(){
- if($(this).val() != ""){
- isEmpty++;
- }
- var cell_content = $(this).val();
- $(this).parent("td").html(cell_content);
- });
- if(isEmpty == 0){
- $(this).remove();
- }
- });
- $(".removeCell").remove();
- return false;
- });
- $(document).on("click", ".deleteRow", function(e){
- e.preventDefault();
- var that = this;
- var DBID = $(this).data("id");
- $.ajax({
- "url": "randomtest.php", // Muuda faili nime
- "method": "POST",
- "data": {
- "action": "removeRow",
- "ID": DBID,
- },
- "success": function(response){
- $(that).parent("td").parent("tr").remove();
- }
- });
- return false;
- });
- $(document).on("click", "#saveBtn", function(e){
- e.preventDefault();
- $(this).val("Edit");
- $(this).attr("id", "edittable");
- $("#add").hide();
- $("#cancel").hide();
- $(".removeCell").remove();
- var input_values = [];
- $("table tbody tr").each(function(rowKey, rowEl){
- var single_row = [];
- if(typeof $(this).data("id") !== "undefined"){
- single_row.push($(this).data("id"));
- }else{
- single_row.push("no");
- }
- $("td input", this).each(function(key, el){
- single_row.push($(this).val());
- });
- input_values.push(single_row);
- });
- $.ajax({
- "url": "randomtest.php", //Muuda faili nimi
- "method": "POST",
- "dataType": "JSON",
- "data": {
- "action": "saveRows",
- "input_values": input_values
- },
- "success": function(response){
- var newHTML = "";
- response.forEach(function(el){
- newHTML += '<tr data-id="'+el.id+'">';
- newHTML += '<td>'+el.type+'</td>';
- newHTML += '<td>'+el.brand+'</td>';
- newHTML += '<td>'+el.name+'</td>';
- newHTML += '<td>'+el.plastic+'</td>';
- newHTML += '</tr>';
- });
- $(".table tbody").html(newHTML);
- }
- });
- return false;
- });
- $(document).on("click", "#add", function(e){
- e.preventDefault();
- $(".empty").remove();
- var inputEl = "<tr>";
- inputEl += "<td>";
- inputEl += "<input type='text' placeholder='Type...'>";
- inputEl += "</td>";
- inputEl += "<td>";
- inputEl += "<input type='text' placeholder='Brand...'>";
- inputEl += "</td>";
- inputEl += "<td>";
- inputEl += "<input type='text' placeholder='Name...'>";
- inputEl += "</td>";
- inputEl += "<td>";
- inputEl += "<input type='text' placeholder='Plastic...'>";
- inputEl += "</td>";
- inputEl += "</tr>";
- $("table tbody").append(inputEl);
- return false;
- });
- });
- </script>
- </head>
- <body>
- <?php
- // $stmt = $mysqli->prepare("SELECT * FROM discideas;");
- // $stmt->bind_result($id, $type, $brand, $name, $plastic);
- // $stmt->execute();
- $results = $mysqli->query("SELECT * FROM discideas;");
- ?>
- <table class="table" border="1" id="discideas">
- <caption>Disc ideas</caption>
- <thead>
- <tr>
- <th>Type</th>
- <th>Brand</th>
- <th>Name</th>
- <th>Plastic</th>
- </tr>
- </thead>
- <tbody>
- <?php
- if($results->num_rows === 0){
- ?>
- <tr class='empty'>
- <td colspan='5' align='center'>
- <b>Ühtegi ideed ei leitud!</b>
- </td>
- </tr>
- <?php
- }else{
- foreach($results as $result){
- if(!empty($result)){
- ?>
- <tr data-id="<?php echo $result["id"]; ?>">
- <td><?php echo $result["type"]; ?></td>
- <td><?php echo $result["brand"]; ?></td>
- <td><?php echo $result["name"]; ?></td>
- <td><?php echo $result["plastic"]; ?></td>
- </tr>
- <?php
- }
- }
- }
- $mysqli->close();
- ?>
- </tbody>
- </table>
- <div id="edit">
- <input type="button" id="add" value="Add row" style="display:none;">
- <input type="button" id="edittable" value="Edit">
- <input type="button" id="cancel" value="Cancel" style="display:none;">
- </div>
- <br>
- <p id="wat">
- Vajutad Edit nuppu, Edit nupu asemele tekib sama koha peale nupp Save.
- Tabeli ridadele, kus on tekst, tekib teksti asemele input field ja sinna sisse jääb see sama väärtus, mis enne tabelis oli. Muudad teksti input fieldis ja vajutad nuppu save, uuendab baasis andmed ära ja kuvab uusi andmeid.
- </p>
- <p>Panin siia mingi asja, mis tegime kunagi Romiliga, aga ma ei suuda läbi hammustada ja järgi teha.</p>
- <a href="recipes.7z">recipes.7z</a>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement