如何从这个复杂的数组系统中编译SQL查询

时间:2013-01-23 03:28:00

标签: php mysql sql

乍一看,我原本以为这会非常简单,只是通过破坏数组构建一个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()

0 个答案:

没有答案