将数据库数据放置在多维数组中

时间:2018-08-20 12:42:14

标签: php mysql arrays database multidimensional-array

对于我的网上商店,我有1个数据库和3个相关表。 为了显示数据,建议我将这些数据库中的数据放置在3个二维数组中,以便我可以可视化自己在做什么。 但是,我唯一成功的将数据放入数组的尝试导致代码本身被覆盖,仅显示表中的最后一个数据。

有人可以给我一些有关如何以自动填充方式设置阵列的建议吗?

<?php
        //Create the Multiarrays. 2D, one per table.

        // Create connection
        $conn = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        //Open the database
        $sql = "SELECT * FROM productwindow";
        $result = $conn->query($sql);

        //Multiarray locatie 1. When the array starts here, the browser chokes.
            if ($result->num_rows > 0) {
                //multiarray locatie 2. When the array starts here, the browser also chokes.
                while($row = $result->fetch_assoc()) {

                    $product = array( //multiarray locatie 3. The problem is that the array seems to overwrite itself.
                        array(  //I want PHP to repeat this bit of code for every product in my database, so that I can use it later on.
                            "ID" => $row["ID"],
                            "Name" => $row["Productname"],
                            "Price" => $row["Pricetag"],
                            "Supply" => $row["Productsupply"],
                            "Tags" => $row["Tags"],
                            "Materials" => $row["Materials"],
                        ),
                    );
                }
                //Multiarray locatie 2 end woul be placed here
            } else {
                echo "0 results found. Please check database connection.";
            }
        //Multiarray locatie 1 end woul be placed here. 

        $conn->close();
        //      
        echo "<br/>";
        $searchby = "Pluimstaart";//The keyword that I used in earlier concepts to filter on specific products. I now need a for loop to procedurally run through the multiarray.
        $array_subjected_to_search = $product;
        $key = array_search($searchby, array_column($array_subjected_to_search, "Name")); //If it cannot be done via a foreloop, I would need an alternative to "array_search" to show the contents of my array.
        var_dump($array_subjected_to_search[$key]);//Used to see if I actually fill my array with data. It only shows the latest data.

        //From here on out I build the website like follows;
        echo "<br/>product data: " . $product[$key["Supply"]] . ".";
    ?> 

非常感谢您的反馈!

3 个答案:

答案 0 :(得分:2)

$product变量在每个循环中都会被覆盖,因为您每次都使它等于一个全新的数组:

$product = array(...

相反,您应该使用array_push或以下缩写将元素附加到数组的末尾:

$product[] = [
    "ID" => $row["ID"],
    "Name" => $row["Productname"],
    "Price" => $row["Pricetag"],
    "Supply" => $row["Productsupply"],
    "Tags" => $row["Tags"],
    "Materials" => $row["Materials"]
];

答案 1 :(得分:1)

尝试用此代码替换if else代码,然后尝试

NSAttributedString

答案 2 :(得分:0)

我不确定这是否是您的意思...

$i = 0; 
$product = array();

while($row = $result->fetch_assoc()) {

   $product[$i] = array(

    "ID" => $row["ID"],
    "Name" => $row["Productname"],
    "Price" => $row["Pricetag"],
    "Supply" => $row["Productsupply"],
    "Tags" => $row["Tags"],
    "Materials" => $row["Materials"],

  );
$i++;
}