1
$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

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

12 Comments

This is helpful, when I unpack how can I just get unique/distinct results though?
What do you mean by distinct results? You unpack each row seperately. The variable names will correspond to the different fields.
If I echo the width (echo $width;), the results are not distinct(unique): 235 225 235 245 245 225 225 245 255 255 265 How would I achieve this, been messing with array_unique, but with not success.
Your code works great. I was so so so close, but not close enough for it to be right! Not sure how I am going to make the menus relational.. any ideas? I have three unique arrays now, $ar_widths, $ar_sidewalls and $ar_rims.
Tomas, I would like to Paypal you some beer/coffee money. What address can I send it to?
|
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.