PHP, MYSQL, EXPLODE and ARRAYS
$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
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);
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
You can use "array_unique" function to remove duplicate elements from the array.
精彩评论