亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

How does PHP handle the two-dimensional display and submission of colors and sizes in product management?
P粉529965851
P粉529965851 2023-06-12 12:01:22
0
1
900

Dear friends, I recently encountered a problem with the color and size attributes of a product. For example, a product has two colors and three sizes.

Product A has a color design [black, white] and a size design [ S, M, L】

I want to click on the product list to go to the product details page, enter the quantity of each sku ordered, and then save and submit to the database. As shown below, can you give me some ideas? The color and size of each product are not fixed. The color and size information are retrieved from the database and displayed in an untabled header.

1.png

P粉529965851
P粉529965851

reply all(1)
WBOY
<table>
<thead>
<tr>
<th>商品名稱</th>
<?php
//?從數(shù)據(jù)庫中獲取顏色信息
$colors?=?['黑色',?'白色'];

//?從數(shù)據(jù)庫中獲取尺碼信息
$sizes?=?['S',?'M',?'L'];

//?動態(tài)生成表頭
foreach?($colors?as?$color)?{
foreach?($sizes?as?$size)?{
echo?"<th>{$color}?-?{$size}</th>";
}
}
?>
</tr>
</thead>
<tbody>
<!--?商品列表數(shù)據(jù)?-->
</tbody>
</table>

Use the `<input>` tag to create an input box, and use the `name` attribute to identify different SKUs. For example,

<tbody>
<?php
//?從數(shù)據(jù)庫中獲取商品列表數(shù)據(jù)
$products?=?[
['name'?=>?'商品A',?'sku'?=>?'A001'],
['name'?=>?'商品B',?'sku'?=>?'B001'],
//?其他商品數(shù)據(jù)...
];

//?遍歷商品列表
foreach?($products?as?$product)?{
echo?"<tr>";
echo?"<td>{$product['name']}</td>";

//?為每個SKU創(chuàng)建輸入框
foreach?($colors?as?$color)?{
foreach?($sizes?as?$size)?{
$sku?=?"{$product['sku']}_{$color}_{$size}";
echo?"<td><input?type='number'?name='{$sku}'></td>";
}
}

echo?"</tr>";
}
?>
</tbody>

uses PHP to process form data and save it to the database. You can use the `$_POST` superglobal variable to get the form data and insert it into the database using SQL statements like

<?php
//?處理表單提交
if?($_SERVER['REQUEST_METHOD']?===?'POST')?{
//?連接數(shù)據(jù)庫
$conn?=?new?mysqli('localhost',?'username',?'password',?'database');

//?檢查連接是否成功
if?($conn->connect_error)?{
die('數(shù)據(jù)庫連接失?。??.?$conn->connect_error);
}

//?遍歷表單數(shù)據(jù)
foreach?($_POST?as?$sku?=>?$quantity)?{
//?將SKU拆分為顏色和尺碼
$parts?=?explode('_',?$sku);
$color?=?$parts[1];
$size?=?$parts[2];

//?使用SQL語句將數(shù)據(jù)插入數(shù)據(jù)庫
$sql?=?"INSERT?INTO?orders?(sku,?color,?size,?quantity)?VALUES?('$sku',?'$color',?'$size',?'$quantity')";
$result?=?$conn->query($sql);

//?檢查插入是否成功
if?(!$result)?{
echo?"插入數(shù)據(jù)失?。簕$conn->error}";
}
}

//?關閉數(shù)據(jù)庫連接
$conn->close();
}
?>

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template