简体   繁体   中英

linking php arrays together

I am trying to link php arrays together.

The values in dbTable array is:- rfq and quote

The values in dbColumns array is:- qid, item, price, id and item

The values in dbTData array is:- 1 ball, 200, 2 and bat.

Outputted as:-

  qid 1
  item ball
  price 200

  id 2
  item bat

  0 => quote
  1 => rfq 

As shown the array dbColumns and dbTData is linking up, but how do i get dbTable to link with these please?

So the output would be:-

 quote
 qid 1
 item ball
 price 200

 rfq
 id 2
 item bat

The code so far is:-

    // 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>";
        }

Thanks in advance, I hope i have explained myself enough

Previous page:-

    $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();

New Code below **********************

    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>";
                        }

            }
    }

Output

 quote
 qid 1
 quote
 item bin
 rfq
 id 3
 rfq
 item bat

When you create the form you should change tableNames[] (the name set the input) to table[$i][name] , afterwards changing the column[] to table[$i][columns][] and tupleData[] to table[$i][data][] suffices. you should add a $i=0; before executing the query and $i++; before closing the while cycle while ( $row = $result->fetch_row() ){}

When you read them afterwards:

$AllData = $_POST["table"];

foreach ($AllData as $sigleData){

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

}

And you can now use or echo the column and data of a table as well as the name. I think the general idea should be clear now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM