Prova questi. Aggiorna la tabella dopo il drop. Il nome del database è testi7.Devi trascinare gli elementi da P1 a P quello che vuoi e vice versa.
Dimenticavo. Funziona con Firefox non con IE.
File : test9023.php
Codice PHP:
<?php
$connexion = new mysqli("localhost","root","", "testi7");
$query = "select id, value, level from tabella1 order by id";
$result = mysqli_query($connexion,$query);
$array = array();
while (list($id,$value,$level) = mysqli_fetch_array($result)) {
$array[$level][] = array($id,$value,$level);
}
function lista($level,$array) {
reset($array);
if (isset($array[$level])) {
foreach ($array[$level] as $key => $value) {
list($id,$value,$level) = $value;?>
<div id="id_<?php print $id;?>" draggable= 'true' ondragstart= 'drag(event)'><?php print $value;?></div><?php
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<style type="text/css">
<!--
.drop1{
background-color:yellow;
}
.drop2{
background-color:green;
}
-->
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
var dragFrom = null;
function allowDrop(ev){
ev.preventDefault();
}
function drag(ev){
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(anObject,ev){
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
//if (anObject.id != ev.target.id) {
anObject.appendChild(document.getElementById(data));
// Ajax
$.ajax({
url : 'test9023b.php',
type : 'POST',
data : 'id=' + document.getElementById(data).id + '&level=' + document.getElementById(data).parentNode.id,
dataType : 'html'
}) ;
//}
}
//-->
</script>
</head>
<body>
<table summary="">
<tr style="border:1px solid black">
<td width="150">P1</td><td width="150">P2</td><td width="150">P3</td><td width="150">P4</td>
</tr>
<tr style="border:1px solid black">
<td width="150" class= 'drop1' id= 'p1' ondrop='drop(this,event)' ondragover='allowDrop(event)' >
<?php lista(1,$array);?>
</td>
<td width="150" class= 'drop2' id= 'p2' ondrop='drop(this,event)' ondragover='allowDrop(event)'>
<?php lista(2,$array);?>
</td>
<td width="150" class= 'drop1' id= 'p3' ondrop='drop(this,event)' ondragover='allowDrop(event)'>
<?php lista(3,$array);?>
</td>
<td width="150" class= 'drop2' id= 'p4' ondrop='drop(this,event)' ondragover='allowDrop(event)'>
<?php lista(4,$array);?>
</td>
</tr>
</table>
<div id="log"></div>
</body>
</html>
File : test9023b.php
Codice PHP:
<?php
$connexion = new mysqli("localhost","root","", "testi7");
$id = $_POST['id'];
$id = str_replace('id_','',$id);
$level = str_replace('p','',$_POST['level']);
$query = "update tabella1 set level=$level where id='$id'";
mysqli_query($connexion,$query);
echo "ok";
?>
Tabella SQL :
codice:
-- phpMyAdmin SQL Dump
-- version 4.5.5.1
-- http://www.phpmyadmin.net
--
-- Client : 127.0.0.1
-- Généré le : Dim 09 Octobre 2016 à 22:22
-- Version du serveur : 5.7.11
-- Version de PHP : 7.0.4
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Base de données : `testi7`
--
-- --------------------------------------------------------
--
-- Structure de la table `tabella1`
--
CREATE TABLE `tabella1` (
`id` bigint(20) NOT NULL,
`value` varchar(35) NOT NULL,
`level` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Contenu de la table `tabella1`
--
INSERT INTO `tabella1` (`id`, `value`, `level`) VALUES
(1, 'A100', 1),
(2, 'A101', 1),
(3, 'A102', 1),
(4, 'A103', 1),
(5, 'A104', 1),
(6, 'A105', 1),
(7, 'A106', 1),
(8, 'A107', 1),
(9, 'A108', 1),
(10, 'A109', 1),
(11, 'A110', 1),
(12, 'A111', 1);
--
-- Index pour les tables exportées
--
--
-- Index pour la table `tabella1`
--
ALTER TABLE `tabella1`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT pour les tables exportées
--
--
-- AUTO_INCREMENT pour la table `tabella1`
--
ALTER TABLE `tabella1`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;