我正在尝试将php数组链接在一起。
dbTable 数组中的值为:- rfq 和 quote
dbColumns 数组中的值为:- qid、item、price、id 和 item
dbTData 数组中的值为:- 1 球、200、2 和球棒。
输出为:-
qid 1
item ball
price 200
id 2
item bat
0 => quote
1 => rfq
如图所示,数组 dbColumns 和 dbTData 正在链接,但是我如何让 dbTable 与它们链接?
所以输出将是:-
quote
qid 1
item ball
price 200
rfq
id 2
item bat
到目前为止的代码是:-
// form variables (arrays) passed over
$dbTData = $_POST["tupleData"];
$dbColumns = $_POST["column"];
$dbTable = $_POST["tableNames"];
$dbName = $_POST["dbName"];
// combining two arrays
$combineCT = array_combine($dbTData, $dbColumns);
// debugging echo database name
echo "The database in use = " . $dbName . "</br> ";
// database connection
$dbConnectionT = mysqli_connect($host, $user, $password, $dbName);
if ($dbConnectionT ->connect_error){
die ("database connection failed " . $dbConnection->connect_error);
}
//loop through associative array
foreach($combineCT as $message => $answer)
{
echo $answer . " " . $message;
echo "</br>";
}
echo "</br>";
foreach($dbTable as $key => $val)
{
echo "$key => $valn";
echo "</br>";
}
提前感谢,希望我已经解释了自己
上一页:-
$result = $dbConnectionT->query("SHOW TABLES");
echo "<form action='algone.php' method='POST'>";
while ( $row = $result->fetch_row() )
{
// printing out the table name
echo '<h3>' . $row[0] . '</h3>';
// echo "here table ";
echo '' . "<input type='hidden' value='$dbName' name ='dbName' > ";
echo '<th>' . "<input type='hidden' value='$row[0]' name ='tableNames[]' > ";
$table = $row[0];
$result1 = $dbConnectionT->query("SELECT * FROM $table");
if($result1)
{
echo '<table cellpadding="1" cellspacing="0" >';
$column = $dbConnectionT->query("SHOW COLUMNS FROM $table");
echo '<tr>';
while($row3 = $column->fetch_row() )
{
echo '<th> ' . "<input type='hidden' value='$row3[0]' name ='column[]' > ";
echo ''.$row3[0]. " <input type ='text' name='tupleData[]'></th>" ;
echo "</br>";
}
echo '</tr>';
while($row2 = $result1->fetch_row() )
{
echo '<tr>';
foreach($row2 as $key=>$value) {
echo '<td>',$value . " here " .'</td>';
echo "here row2 ";
}
echo '</tr>';
}
echo '</table><br />';
}
}
echo "<input type='submit' name='submit' value='submit'> ";
echo "</form>";
$dbConnectionT->close();
下面的新代码
****** foreach ($AllData as $sigleData)
{
$table = $sigleData['name'];
$columns = $sigleData['columns'];
$columnData = $sigleData['data'];
$combineCT = array_combine($columns , $columnData);
foreach($combineCT as $colData => $tupleData)
{
$tableS = implode(" ", $table);
echo $tableS;
echo "</br>";
echo $colData. " " . $tupleData;
echo "</br>";
$sqlTuples = "INSERT INTO " . $tableS . " (id, " . $colData . ") VALUES ('1', '" . $tupleData . "')";
if ($dbConnectionT->query($sqlTuples) == TRUE)
{
echo "database updated";
echo "</br>";
}
}
}
输出
quote
qid 1
quote
item bin
rfq
id 3
rfq
item bat
创建表单时,应将tableNames[]
(名称设置输入)更改为 table[$i][name]
,然后将column[]
更改为 table[$i][columns][]
,将tupleData[]
更改为table[$i][data][]
就足够了。 您应该在执行查询之前添加一个$i=0;
,并在关闭 while 循环while ( $row = $result->fetch_row() ){}
之前添加$i++;
当你事后阅读它们时:
$AllData = $_POST["table"];
foreach ($AllData as $sigleData){
$table = $sigleData['name'];
$columns = $sigleData['columns'];
$columnData = $sigleData['data'];
$combineCT = array_combine($columns , $columnData );
}
现在,您可以使用或回显表的列和数据以及名称。我认为现在总体思路应该很清楚了。