Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
数据库中的PHP数组_Php - Fatal编程技术网

数据库中的PHP数组

数据库中的PHP数组,php,Php,在我的数据库中,我得到了3行。产品、数量和费率 $rate = implode(",", $_POST["myrate"]); 这是价格。这意味着一行可以是“rate1、rate2、rate3”等等。但产品和数量都是一样的 "product1, product2, product3" "quantity1, quantity2, quantity3" "rate1, rate2, rate3" 如果我重复它,它将如上所述。但我想要的是: "product1, quantity1, rate

在我的数据库中,我得到了3行。产品、数量和费率

$rate = implode(",", $_POST["myrate"]); 
这是价格。这意味着一行可以是“rate1、rate2、rate3”等等。但产品和数量都是一样的

"product1, product2, product3"
"quantity1, quantity2, quantity3"
"rate1, rate2, rate3"
如果我重复它,它将如上所述。但我想要的是:

"product1, quantity1, rate1"
"product2, quantity2, rate2"
"product3, quantity3, rate3"
这怎么可能

这就是我正在使用的代码。刚刚添加了两个字段和两行

我的PHP代码:

<?php
    //Open a new connection to the MySQL server
    $mysqli = new mysqli('localhost','root','','invoice-generator');

    //Output any connection error
    if ($mysqli->connect_error) {
        die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }

    $capture_field_vals = "";
    if(isset($_POST["mytext"]) && is_array($_POST["mytext"])){
        $capture_field_vals = implode(",", $_POST["mytext"]); 
        $quantity = implode(",", $_POST["myquantity"]); 
        $rate = implode(",", $_POST["myrate"]); 
    }else{
        echo "error";
    }

    //MySqli Insert Query
    $insert_row = $mysqli->query("INSERT INTO invoice_rows (product, quantity, rate) VALUES ('$capture_field_vals', '$quantity', '$rate')");

    if($insert_row){
        echo 'Success!';
    }else{
        echo "error2";
    }
?>

如果所有变量的计数相同,则可以这样做

if (count($_POST["mytext"]) == count($_POST["myquantity"]) && count($_POST["myquantity"]) == count($_POST["myrate"])) {
   for ($i = 0; $i < count($_POST["mytext"]); $i++) {
    echo $_POST["mytext"][$i] . "," . $_POST["myquantity"][$i] . "," . $_POST["myrate"][$i] ."<br/>";
   }
}
if(count($\u POST[“mytext”])==count($\u POST[“myquantity”])和&count($\u POST[“myquantity”])==count($\u POST[“myrate”])){
对于($i=0;$i”;
}
}

产品
量
比率
//假设您有来自数据库的$results数据
//绕一整行
通过这种方式,您可以在一行中显示数据库表中每个id的产品、数量和行……如果您需要的话
$quantity=数组(“q1”、“q2”、“q3”、“q4”);
$product=数组(“p1”、“p2”、“p3”、“p4”);
$rate=数组(“r1”、“r2”、“r3”、“r4”);
对于($i=0;$i

将它们全部放入循环中,并将值转储到一个数组中。

显示数据库表数据(通过图)和代码您尝试了什么?很难理解你想要什么和你想说什么?非常奇怪的数据库设计。。。但是您当然可以将行保存到变量中,将它们分解成数组,然后通过php循环对它们进行迭代。为什么要生成字符串,因为您可以使用直接数组获得所需的结果。现在已使用链接更新了问题您真的需要这样存储数据吗。您可以简单地将其存储在行中,如
“product1,quantity1,rate1”
product1,quantity1,rate1”
等。因为如果您以现在的存储方式存储数据,您将无法获得这样的组合。OP只需这样做,就可以了。
<table>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Rate</th>
</tr>
</thead>
<tbody>
//Lets say you have the data from your db in $results
<?php foreach($results as $result): ?>
<tr>   //loop one whole row
<td><?=$result['product']?></td>
<td><?=$result['quantity']?></td>
<td><?=$result['rate']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

This way you can show product,quantity and row for each id in your database table in one row...if that is what you want
$quantity = array("q1","q2","q3","q4");
$product = array("p1","p2","p3","p4");
$rate = array("r1","r2","r3","r4");

for ($i = 0; $i < count($quantity); $i++) {
    $new_data[] = array($quantity[$i],$product[$i],$rate[$i]);
}