PHP、MYSQL、爆炸和数组
$query_posts = "
SELECT DISTINCT meta_value
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Product'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";
$result = mysql_query($query_posts) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['meta_value'];
}
上面生成的结果列表如下:
235/40/R18/95/ZR(Y)/XL £180.00
225/45/R17/94/W/XL £180.00
235/45/R17/97/Y/XL £180.00
245/45/R17/99/Y/XL £180.00
245/40/R17/91/Y £180.00
225/40/R18/92/W/XL £180.00
etc etc
如果您没有猜到的话,它们是轮胎尺寸。我需要将前三个值分解为不同的关系菜单: Trye Width / Sidewall / Rim
我发现如果我使用:
$query_posts = "
SELECT DISTINCT meta_value
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Product'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";
$result = mysql_query($query_posts) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$tyres_widths = (explode("/",$row['meta_value']));
echo ($tyres_widths[0]);
}
我可以获得第一个 ($tyres_widths[0])、第二个 ($tyres_widths[1]) 或第三个列($tyres_widths[2]),但这些值并不不同,当然我还没有达到它们在菜单中相关的程度。
我的做法正确吗?一定有更好的方法吗?有人可以帮忙吗。我很高兴奖励正确的答案,当然其他人也可以受益,这就是重点!
预先感
谢斯图
$query_posts = "
SELECT DISTINCT meta_value
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Product'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";
$result = mysql_query($query_posts) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['meta_value'];
}
The above produces a list of results as below:
235/40/R18/95/ZR(Y)/XL £180.00
225/45/R17/94/W/XL £180.00
235/45/R17/97/Y/XL £180.00
245/45/R17/99/Y/XL £180.00
245/40/R17/91/Y £180.00
225/40/R18/92/W/XL £180.00
etc etc
They are tyre sizes if you didn't guess. I need to break the first three values into distinct relational menus: Trye Width / Sidewall / Rim
I worked out that if I use:
$query_posts = "
SELECT DISTINCT meta_value
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'Product'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";
$result = mysql_query($query_posts) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$tyres_widths = (explode("/",$row['meta_value']));
echo ($tyres_widths[0]);
}
I can get the first ($tyres_widths[0]), second ($tyres_widths[1]) or third column ($tyres_widths[2]), but the values are not distinct and of course I have still not got to the point when they are relational in menus.
Is my approach correct? There has got to be a better method? Can someone assist. I am happy to reward for correct answer and of course someone else can benefit which is the whole point!!
Thanks in advance
Stu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的做法是正确的。请注意,您可以使用
list
来“解压”数组:名为
$sidewall
的变量比名为$tire_widths[1]
的变量更清晰。编辑
更整洁的是,您可以使用以下方法将字段放回到关联数组中:
编辑
根据注释中的请求,为特定字段创建不同值的列表,您可以必须创建一个单独的数组并使用 array_unique:
Your approach is correct. Note that you can use
list
to "unpack" the array:A variable named
$sidewall
is clearer than a variable named$tire_widths[1]
.EDIT
Even tidier, you can put the fields back in an associative array, using:
EDIT
Per request in comment, to create a list of distinct values for a specific field, you will have to create a seperate array and use
array_unique
:另一种方法是正则表达式,但我不推荐它,因为它相当“吃”资源,你可以做的是爬升数组并在爬升时命名数组
使用函数作为first、next、end,
您可以在php.net/next alt 找到不同的函数行为。 名称
php.net/函数 希望它能帮助
等继续攀爬数组并根据需要命名它们,如果没有更多内容可获取,则返回 false
another approach would be regular expressions, I wouldn't recommend it though because it quite resource "eating", what you could do is climb the array and name the array as it climbs
using functions as first, next, end
you can find the diffrent functions behavior at php.net/next alt. php.net/function name
p.s. hope it helped
etc continue climbing the array and name them as you please it returns false if there is nothing more to get
您可以使用“array_unique”函数从数组中删除重复元素。
You can use "array_unique" function to remove duplicate elements from the array.