(这个帖子可能会变得很长,但请大家忍耐一下,帮我找到解决的办法)
我有一个产品表,里面有各种参数,主要参数是产品的尺寸和成本。每个产品的尺寸分为3个部分,即小、中、大,其成本也根据产品的尺寸分为3部分。为了这个目的,我创建了另一个表productsize,它承载了每个产品的尺寸和成本。
产品尺寸表的小图是
我有另一个购物车,它保存了用户订购的产品的价值。
小车台的小景是
现在,我希望把这两个表结合起来,并有一个数组来显示每个尺寸的尺寸和用户为某个特定产品的每个尺寸所订购的数量,为此,我写了以下代码
$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,如果他已经订购了两个尺寸,那么[购买的尺寸]和[购买的数量]中的第三个值应该是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] =>
)
)
这里的问题是在你做事情的方式,最终你会遇到一些麻烦,因为你正在玩弄你的数据库的数据,并重新格式化,以适应目的的细微差别,而不是只是存储&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