0

乍一看,我认为这很简单,只需通过内爆数组来构造一个 sql 查询,但我就是想不通如何达到这一点。基本上我有一个程序可以将用户输入分解为我的表所需的方式,现在我需要将该数据放入 SQL 命令中,你可以看到我是如何尝试这样做的,不用说我在我的束手无策。

HTML:

<form action="go.php" method="post">
<h1>Enter port data to view how your table will look</h1>
<label for="ports">Number of Ports:</label><input type="text" id="ports" name="ports" size="30"><br>
<label for="super">Super Columns:</label><input type="text" id="super" name="super"  size="30"><br>
<label for="columns">Columns:</label><input type="text" id="columns" name="columns" size="30"><br>
<label for="rows">Rows:</label><input type="text" id="rows" name="rows" size="30"><br>


PHP:

//post vars
$port = (int)$_POST['ports'];
$super = (int)$_POST['super'];
$col = (int)$_POST['columns'];
$row = (int)$_POST['rows'];
//rest of vars
$halfPort=$port/2;//12
$colHalf = $col / $super;//6
$half=$colHalf-1;//5
$i=1;
$count=1;
$and=1;
$one=1;
$s=1;

echo "<h1>here goes nothen</h1><br><br>";
//sperate port
$finalArray = array();
for ($i = $port; $i >= 1; $i -= $colHalf) {
    $tempArray = array();
    for ($j = 0; $j < $colHalf; $j++) {
        $tempArray[] = $i - $j;
    }
    $tempArray[]= sort($tempArray);
    $finalArray[] = implode(",", $tempArray);
}
$finalArray = array_reverse($finalArray);
echo "<pre>" . print_r($finalArray, true) . "</pre>";

echo "<br><br>";
//sql for insert
$sqlinsert='
        INSERT INTO '.$tbl_name2.'
        (PortNumber) VALUES ';
$start='(';
$end=')';
    $c=1;
$b=0;
//while to finish SQL
$queryStart=array();
$queryEnd=array();
while($c<$half){
    echo "<br>c:".$c."<br>";
$queryStart[]=explode(',',$finalArray[$b]);
    echo "<pre>" . print_r($queryStart, true) . "</pre>";
    $queryEnd[]=

$c+=$and;
$b+=$and;
unset($queryStart);
}

这是我第一次打印的输出:

Array
(
[0] => 1,2,3,4,5,6,1
[1] => 7,8,9,10,11,12,1
[2] => 13,14,15,16,17,18,1
[3] => 19,20,21,22,23,24,1
)

第二个:

c:1
Array
(
[0] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
        [6] => 1
    )

)

c:2
Array
(
[0] => Array
    (
        [0] => 7
        [1] => 8
        [2] => 9
        [3] => 10
        [4] => 11
        [5] => 12
        [6] => 1
    )

)

c:3
Array
(
[0] => Array
    (
        [0] => 13
        [1] => 14
        [2] => 15
        [3] => 16
        [4] => 17
        [5] => 18
        [6] => 1
    )

)

c:4
Array
(
[0] => Array
    (
        [0] => 19
        [1] => 20
        [2] => 21
        [3] => 22
        [4] => 23
        [5] => 24
        [6] => 1
    )

)

我觉得我很接近,只是不能让自己打最后一点我想。有任何想法吗?

另外作为旁注,请忽略数组末尾的尾随 1,它似乎来自运行sort()

4

0 回答 0