首页 > 解决方案 > 创建特定尺寸

问题描述

在此处输入图像描述我在 html 中有一个表。我想在我的数据库中添加带有 php 的表。问题出在我的桌子上。我已经这样做了

CREATE TABLE `playersrb` (
`position` numeric(24) DEFAULT NULL,
`piece_color` enum('B','R') NOT NULL,
`id` numeric(30) DEFAULT NULL,
`last_action` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
)

我有 24 个位置,我想在我的桌子上给出这 24 个是那个大小,而不是 25 个,然后继续。第二,我有 B 和 R,它是颜色(没关系)。现在我把 Id因为这就是我命名我桌子上的图像的方式(它在我的 html 中的表格),我将这些 Id:0 ,Id:1 命名为 30(因为 30 是我拥有的最大图像 - 我不想要更多的)。

我用那张桌子创建,我打开 sqlite,我去那张桌子,我开始放

position  piece_color    id   last_action
Null      | R           | 0   | here it was saying the time    
Null      | R           | 1   | the time as previous 
Null      | B           | 2   |  
Null      | B           | 3   |   
Null      | R           | 4   | 
Null      | R           |  5  |
Null      |B            | 6   |

直到 30 结束
。. . . . 我按下保存按钮,一切都好。我去 phpmyadmin 检查我的表,它不是我创建的。我该怎么做?有位置 24 ,30 id(这将是不同的图像)。保存30 Id 到 24 的位置。

已编辑:从我创建的位置和 id 的图像中可以看出。我想要的表就是这样。这个位置是属于我的形象的地方。id 是图像。我只想从一个表中传递我在该表中的位置和 id 并且我想是对的。在此处输入图像描述.当我移动这些图像时,它们会创建一个具有可变位置和 ID 的表格。该表我想传递到我的表中(在数据库表中)。如果我放置更多图像,我将拥有 Id 2在此处输入图像描述和我拖动的位置。这就是我想要做的。如您所见,我只想拥有 30 张图片。每张图片都是独一无二的。他们有其他 ID,不一样。更多细节,id =0 是图像 a,id =1 是图像 b。位置是,在图像中你可以看到它只是你看到的表格的编号我移动这些图像的地方。

已编辑

<table class="content-table">
<tr>

<th>Image</th>
<th>Position(TO)</th>
<th>New Position</th>
</tr>
</div>


  
</div>


</div>

<?php

require_once "C/dbconnect.php";
















    
    $sql = "SELECT image, position,new_position  FROM playersrb" ;
    $sql="
    
    SET @old_position = 1;
SET @new_position = 12;

SELECT image
     , position old_order
     , ROUND(CASE WHEN position NOT BETWEEN LEAST(@old_position,@new_position) AND GREATEST(@old_position,@new_position) 
                  THEN position 
                  WHEN position = @old_position THEN @new_position
                  ELSE position+(((@new_position<@old_position)-.1)*12)
                  END 
            ,0) new_order 
  FROM playersrb;
    
    ";
    
    
    
    
    
    
    
    $result = $mysqli-> query($sql);
    
    if($result-> num_rows >0) {
    while ($row = $result-> fetch_assoc()){
    echo "<tr><td>". $row["image"] ."</td><td>". $row["position"] ."</td></tr>";
    }
    echo "</table>";
    }
    else{
    echo "0 result";
    }
    $mysqli->close();


?>
</table>

标签: mysqlsqldatabase

解决方案


我不太明白你的问题,但这里有一个重新排序列表的例子......

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(image CHAR(1) NOT NULL PRIMARY KEY 
,position INT NOT NULL
);

INSERT INTO my_table VALUES
('A',1),
('B',2),
('C',3),
('D',4),
('E',5),
('F',6);

因此,假设我们要将位置 5 的图像拖动到位置 2...

SET @old_position = 5;
SET @new_position = 2;

SELECT image
     , position old_order
     , ROUND(CASE WHEN position NOT BETWEEN LEAST(@old_position,@new_position) AND GREATEST(@old_position,@new_position) 
                  THEN position 
                  WHEN position = @old_position THEN @new_position
                  ELSE position+(((@new_position<@old_position)-.5)*2)
                  END 
            ,0) new_order 
  FROM my_table;

+-------+-----------+-----------+
| image | old_order | new_order |
+-------+-----------+-----------+
| A     |         1 |         1 |
| B     |         2 |         3 |
| C     |         3 |         4 |
| D     |         4 |         5 |
| E     |         5 |         2 |
| F     |         6 |         6 |
+-------+-----------+-----------+

这是一个更完整的示例,使用一些 PHP 输出到 HTML……也许其他人可以使它变得漂亮……

<?php
//simple_sorter.php
//Preamble

/*
A simple row repositioning script.
This is using a simple $_GET to determine which row is repositioned.
So you need to supply a source and a target in the url, e.g.:

https://path/to/simple_sorter.php?old_position=5&new_position=2

There is no error checking, so it can quite easily fall apart, and because   
the SELECT comes befpre the UPDATE, you won't see any changes until the 
next time you load the page.
*/

//Data Creation Statements

/*
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(image CHAR(1) NOT NULL PRIMARY KEY
,position INT NOT NULL
);

INSERT INTO my_table VALUES
('A',1),
('B',2),
('C',3),
('D',4),
('E',5),
('F',6);
*/

require('path/to/pdo/connection/stateme.nts');

//My understanding is that the following is needed 
  in order to replace (every instance within the 
  query of) :old_position and :new_position with 
  their corresponding values

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, TRUE);

//and now to the code...

$query = "
SELECT *
  FROM my_table
 ORDER
    BY position
";
$stmt  = $pdo->prepare($query);
$stmt->execute();
$data  = $stmt->fetchAll();
print_r($data);

$query = "
UPDATE my_table x
  JOIN
     ( SELECT image
            , position old_order
            , ROUND(CASE WHEN position NOT BETWEEN LEAST(:old_position,:new_position) AND GREATEST(:old_position,:new_position)
                         THEN position
                         WHEN position = :old_position THEN :new_position
                         ELSE position+(((:old_position>:new_position)-.5)*2)
                         END
                   ,0) new_order
         FROM my_table
     ) y
    ON y.image = x.image
   SET position = new_order
";

$old_position  = $_GET['old_position'];
$new_position  = $_GET['new_position'];

$stmt  = $pdo->prepare($query);
$stmt->execute(array('old_position' => $old_position,'new_position' => $new_position));

?>

输出(例如,取决于使用的值和频率)...

Array
(
    [0] => Array
        (
            [image] => A
            [position] => 1
        )

    [1] => Array
        (
            [image] => D
            [position] => 2
        )

    [2] => Array
        (
            [image] => E
            [position] => 3
        )

    [3] => Array
        (
            [image] => B
            [position] => 4
        )

    [4] => Array
        (
            [image] => C
            [position] => 5
        )

    [5] => Array
        (
            [image] => F
            [position] => 6
        )

)

推荐阅读