(这篇文章可能会有很长的篇幅,但请耐心等待,并帮助我找到解决方案)
我有一个产品表,有各种参数,主要参数是它的大小和成本。每个产品尺寸分为3个部分,即小型,中型和大型,其成本也可根据产品的尺寸分为3个部分。为此,我创建了另一个表格产品,其中包含每种产品的尺寸和成本。
prodsize表的小视图是
我有另一个购物车,它包含用户订购的产品的价值
购物车表的小视图
现在我希望将这两个表组合在一起,并有一个数组,显示每个大小的大小以及用户为特定产品的每个大小订购的数量。为此,我写了下面的代码
$sql = "SELECT catname,GROUP_CONCAT(prodsize) as sizes,GROUP_CONCAT(cost) as price,prodname,prodimg,catid,productid FROM productsize where catid='".$catid."' group by productid";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$pid=$row['productid'];
$rows['catname'] = $row['catname'];
$rows['sizes'] = $row['sizes'];
$rows['price'] = $row['price'];
$rows['prodname'] = $row['prodname'];
$rows['prodimg'] = $row['prodimg'];
$rows['catid'] = $row['catid'];
$rows['productid'] = $row['productid'];
$user_sql = "SELECT group_concat(prodsize) as purchased_size,group_concat(quantity) as purchased_quantity,catid,userid FROM `cart` where userid='$userid' and catid='$catid' and productid='$pid'";
$user_result = mysqli_query($con, $user_sql);
if (mysqli_num_rows($user_result) > 0)
{
while($user_row = mysqli_fetch_assoc($user_result))
{
$rows['purchased_size'] = $user_row['purchased_size'];
$rows['purchased_quantity'] = $user_row['purchased_quantity'];
$rows['userid'] = $user_row['userid'];
}
}
$post_rows[]=$rows;
}
echo "<pre>";
print_r($post_rows);
echo "</pre>";
}
我通过print_r($ post_rows)获得的数组是
Array
(
[0] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 130,170,200
[prodname] => pies
[prodimg] =>
[catid] => 2
[productid] => 13
[purchased_size] => small,large
[purchased_quantity] => 1,1
[userid] => 1
)
[1] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 50,90,110
[prodname] => Squash
[prodimg] =>
[catid] => 2
[productid] => 14
[purchased_size] => medium
[purchased_quantity] => 1
[userid] => 1
)
[2] => Array
(
[catname] => Main Course
[sizes] => small,medium
[price] => 70,110
[prodname] => corns
[prodimg] =>
[catid] => 2
[productid] => 15
[purchased_size] =>
[purchased_quantity] =>
[userid] =>
)
)
现在,我想要的更改位于[purchased_size]
和[purchased_quantity]
。我要那个
如果用户尚未为特定产品购买任何内容,那么[purchased_size]
和[purchased_quantity]
中的值应为0,0,0,如果用户购买了一种尺寸的产品,就像壁球一样,用户选择媒介然后[purchase_size]和[purchase_quantity]中的其他值shouuld转0,0如果他订购了2个尺寸,那么[purchase_size]和[purchase_quantity]中的第3个值应该为0
所以新数组应该看起来像下面给出的数组(IMP点,尺寸和成本的顺序应该是固定的,即小,中,大)
Array
(
[0] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 130,170,200
[prodname] => pies
[prodimg] =>
[catid] => 2
[productid] => 13
[purchased_size] => small,0,large
[purchased_quantity] => 1,0,1
[userid] => 1
)
[1] => Array
(
[catname] => Main Course
[sizes] => small,medium,large
[price] => 50,90,110
[prodname] => Squash
[prodimg] =>
[catid] => 2
[productid] => 14
[purchased_size] => 0,medium,0
[purchased_quantity] => 0,1,0
[userid] => 1
)
[2] => Array
(
[catname] => Main Course
[sizes] => small,medium
[price] => 70,110
[prodname] => corns
[prodimg] =>
[catid] => 2
[productid] => 15
[purchased_size] => 0,0,0
[purchased_quantity] => 0,0,0
[userid] =>
)
)
答案 0 :(得分:0)
这里的问题在于你做事的方式,最终你会遇到一些麻烦,因为你正在处理从你的数据库中获取的数据,并重新格式化它以适应目的的细微差别,而不仅仅是存储&amp;首先以正确的方式检索数据。
对于您正在处理的每种产品/尺寸,理想情况下,产品表中应该有单独的产品ID,因此您可以跟踪库存水平等,并在每个有形商品的购物车/购物篮场景中处理数量......无论如何直接回答您的问题,此代码为您的特定目的进行所需的转换:
$rows['sizes'] = 'small,medium,large';
$available_sizes = explode(',', $rows['sizes']);
$rows['purchased_size'] = 'medium';
$purchased_sizes = explode(',', $rows['purchased_size']);
$rows['purchased_quantity'] = 1;
$purchased_quantity = explode(',', $rows['purchased_quantity']);
echo '<pre>';
echo '$purchased_sizes: ' . print_r($purchased_sizes, true);
echo '</pre>';
echo '<pre>';
echo '$purchased_quantity: ' . print_r($purchased_quantity, true);
echo '</pre>';
$rows['purchased_size'] = '';
$rows['purchased_quantity'] = '';
foreach ($available_sizes as $key => $size) {
$rows['purchased_size'] .= in_array($size, $purchased_sizes) ? $size : 0;
$rows['purchased_quantity'] .= in_array($size, $purchased_sizes) ? array_shift($purchased_quantity) : 0;
if ($key != count($available_sizes)-1) {
$rows['purchased_size'] .= ',';
$rows['purchased_quantity'] .= ',';
}
}
echo '<pre>';
echo '$rows[purchased_size]: ' . print_r($rows['purchased_size'], true);
echo '</pre>';
echo '<pre>';
echo '$rows[purchased_quantity]: ' . print_r($rows['purchased_quantity'], true);
echo '</pre>';
以上输出:
$purchased_sizes: Array
(
[0] => medium
)
$purchased_quantity: Array
(
[0] => 1
)
$rows[purchased_size]: 0,medium,0
$rows[purchased_quantity]: 0,1,0