PHP、MYSQL、爆炸和数组

发布于 2024-09-30 02:21:09 字数 1424 浏览 3 评论 0原文

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初心 2024-10-07 02:21:09

你的做法是正确的。请注意,您可以使用 list 来“解压”数组:

list($width, $sidewall, $rim) = explode("/",$row['meta_value']);

名为 $sidewall 的变量比名为 $tire_widths[1] 的变量更清晰。

编辑

更整洁的是,您可以使用以下方法将字段放回到关联数组中:

list($tires['width'], 
     $tires['sidewall'], 
     $tires['rim'])      = explode("/",$row['meta_value']);

编辑

根据注释中的请求,为特定字段创建不同值的列表,您可以必须创建一个单独的数组并使用 array_unique:

$result = mysql_query($query_posts) or die(mysql_error());
$ar_widths = array(); // setup array of widths

while($row = mysql_fetch_array($result)){
    list($width, $sidewall, $rim) = explode("/",$row['meta_value']);

    $ar_widths[] = $width; // remember width
}

$ar_widths = array_unique($ar_widths); // remove duplicates
print_r($ar_widths);

Your approach is correct. Note that you can use list to "unpack" the array:

list($width, $sidewall, $rim) = explode("/",$row['meta_value']);

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:

list($tires['width'], 
     $tires['sidewall'], 
     $tires['rim'])      = explode("/",$row['meta_value']);

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:

$result = mysql_query($query_posts) or die(mysql_error());
$ar_widths = array(); // setup array of widths

while($row = mysql_fetch_array($result)){
    list($width, $sidewall, $rim) = explode("/",$row['meta_value']);

    $ar_widths[] = $width; // remember width
}

$ar_widths = array_unique($ar_widths); // remove duplicates
print_r($ar_widths);
尐籹人 2024-10-07 02:21:09

另一种方法是正则表达式,但我不推荐它,因为它相当“吃”资源,你可以做的是爬升数组并在爬升时命名数组
使用函数作为first、next、end,

您可以在php.net/next alt 找到不同的函数行为。 名称

php.net/函数 希望它能帮助

$array = (explode("/",$row['meta_value']));
$tires['width']= first($array);
$tires['height']= next($array);
$tires['depth']= next($array);

等继续攀爬数组并根据需要命名它们,如果没有更多内容可获取,则返回 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

$array = (explode("/",$row['meta_value']));
$tires['width']= first($array);
$tires['height']= next($array);
$tires['depth']= next($array);

etc continue climbing the array and name them as you please it returns false if there is nothing more to get

孤独患者 2024-10-07 02:21:09

您可以使用“array_unique”函数从数组中删除重复元素。

You can use "array_unique" function to remove duplicate elements from the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文