将php数组连接在一起

时间:2015-04-13 14:57:46

标签: php sql arrays

我正在尝试将php数组链接在一起。

dbTable数组中的值为: - rfq和quote

dbColumns数组中的值为: - qid,item,price,id和item

dbTData数组中的值为: - 1 ball,200,2和bat。

输出为: -

  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 => $val\n";
            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

1 个答案:

答案 0 :(得分:0)

创建表单时,您应将tableNames[](设置输入的名称)更改为table[$i][name],然后将column[]更改为table[$i][columns][]tupleData[]table[$i][data][]就足够了。您应该在执行查询之前添加$i=0;,并在关闭while循环之前添加$i++; while ( $row = $result->fetch_row() ){}

之后阅读时:

$AllData = $_POST["table"];

foreach ($AllData as $sigleData){

$table = $sigleData['name'];
$columns = $sigleData['columns'];
$columnData = $sigleData['data'];
$combineCT = array_combine($columns , $columnData );

}

现在您可以使用或回显表格的列和数据以及名称。我认为现在应该清楚一般的想法。