我如何以正确的格式获取这些数据?
好的,我有这个查询
select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id from system_step ss
join system as s on s.system_id=ss.system_id
join category_description as cd on cd.category_id=ss.sub_category_id
join system_step_product as ssp on ss.system_step_id=ssp.system_step_id
join product as p on p.product_id=ssp.product_id where s.system_id = 41
order by ss.step_number, ssp.class_id;
which yields this result
7 1 Screens 808 115.0000 1
7 1 Screens 809 381.9000 2
7 1 Screens 810 441.9000 3
8 2 Printers 811 112.3200 1
8 2 Printers 812 201.0400 2
8 2 Printers 813 202.8700 3
9 3 Cash Drawers 814 135.7000 1
9 3 Cash Drawers 815 86.5400 2
9 3 Cash Drawers 816 135.7000 3
有没有办法将其转换为包含三个元素的 php 数组,如下所示
Array
(
[0] => Array
(
[name] => "Screens"
[standard_product] => Array ([id] => 808, [price] => '115.0000')
[business_product] => Array ([id] => 809, [price] => '381.9000')
[premium_product] => Array ([id] => 810, [price] => '441.9000')
)
[1] => Array
(
[name] => "Printers"
[standard_product] => Array ([id] => 811, [price] => '112.3200')
[business_product] => Array ([id] => 812, [price] => '201.0400')
[premium_product] => Array ([id] => 813, [price] => '202.8700')
)
[2] => Array
(
[name] => "Cash Drawers"
[standard_product] => Array ([id] => 814, [price] => '135.7000')
[business_product] => Array ([id] => 815, [price] => '86.5400')
[premium_product] => Array ([id] => 816, [price] => '135.7000')
)
)
$sql = "select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id, pd.name as product_name, pd.description from system_step ss join system as s on s.system_id=ss.system_id join category_description as cd on cd.category_id=ss.sub_category_id join system_step_product as ssp on ss.system_step_id=ssp.system_step_id join product as p on p.product_id=ssp.product_id join product_description as pd on pd.product_id=p.product_id where s.system_id = {$system['system_id']} order by ss.step_number, ssp.class_id;";
$result = mysql_query($sql);
$steps = array();
while($row_r = mysql_fetch_assoc($result)){
$steps[] = $row_r;
}
,步骤是包含 9 个元素的完整数组
正如您所看到的,唯一值得注意的是 class_id 1 是 standard_product class_id 2 是Business_product 和 class_id 3 是 premium_product
ok so i have this query
select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id from system_step ss
join system as s on s.system_id=ss.system_id
join category_description as cd on cd.category_id=ss.sub_category_id
join system_step_product as ssp on ss.system_step_id=ssp.system_step_id
join product as p on p.product_id=ssp.product_id where s.system_id = 41
order by ss.step_number, ssp.class_id;
which yields this result
7 1 Screens 808 115.0000 1
7 1 Screens 809 381.9000 2
7 1 Screens 810 441.9000 3
8 2 Printers 811 112.3200 1
8 2 Printers 812 201.0400 2
8 2 Printers 813 202.8700 3
9 3 Cash Drawers 814 135.7000 1
9 3 Cash Drawers 815 86.5400 2
9 3 Cash Drawers 816 135.7000 3
Is there a way to turn this into a php array of three elements like this
Array
(
[0] => Array
(
[name] => "Screens"
[standard_product] => Array ([id] => 808, [price] => '115.0000')
[business_product] => Array ([id] => 809, [price] => '381.9000')
[premium_product] => Array ([id] => 810, [price] => '441.9000')
)
[1] => Array
(
[name] => "Printers"
[standard_product] => Array ([id] => 811, [price] => '112.3200')
[business_product] => Array ([id] => 812, [price] => '201.0400')
[premium_product] => Array ([id] => 813, [price] => '202.8700')
)
[2] => Array
(
[name] => "Cash Drawers"
[standard_product] => Array ([id] => 814, [price] => '135.7000')
[business_product] => Array ([id] => 815, [price] => '86.5400')
[premium_product] => Array ([id] => 816, [price] => '135.7000')
)
)
$sql = "select ss.system_step_id, ss.step_number, cd.name, ssp.system_step_product_id, p.cost, ssp.class_id, pd.name as product_name, pd.description from system_step ss join system as s on s.system_id=ss.system_id join category_description as cd on cd.category_id=ss.sub_category_id join system_step_product as ssp on ss.system_step_id=ssp.system_step_id join product as p on p.product_id=ssp.product_id join product_description as pd on pd.product_id=p.product_id where s.system_id = {$system['system_id']} order by ss.step_number, ssp.class_id;";
$result = mysql_query($sql);
$steps = array();
while($row_r = mysql_fetch_assoc($result)){
$steps[] = $row_r;
}
so steps is the the full array with 9 elements
As you can see the only thing of note is the class_id 1 is standard_product class_id 2 is business_product and class_id 3 is premium_product
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在获取 SQL 结果时获取填充多维数组,或者拆分为多个 SQL 命令,从而构建数组(首先获取名称,然后获取具有每个名称值的产品)。
我不知道你更喜欢哪一个,但由于你有一个大的连接分割,就代码的可读性而言,SQL 可能不是最差的。性能影响可能会有所不同。
You can fetch fill a multi-dimensional array while fetching the SQL result or split into several SQL commands and therefore build up your array (1st fetching the names, than the products with values of every name).
I do not know which you prefer but as you have a large join splitting the SQL might not be the worst in terms of readability of your code. Performance impact may vary.
我想这样就可以了,
希望我把所有的列名都写对了。
这就是我测试的结果,我得到了正确的输出 http://codepad.org/yjU3gxbB
您可能需要更改的是动态创建名称数组。
但是,当您对同一个查询执行两次
mysql_fetch_assoc
时,PHP 不喜欢它。您可能想要执行另一个查询,其中仅选择cd.name
Methinks this will do it
Hopefully I got all the column names right.
This is what I tested it off of and I get the correct output http://codepad.org/yjU3gxbB
The only thing you may what to change is creating the name array dynamically
However, PHP doesn't like it when you do a
mysql_fetch_assoc
twice on the same query. You might want to do another query where you only select thecd.name