Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In a WordPress environment I'm creating pages that represent a company page. These company pages sell certain brands. So I created a custom form in a metabox that let's the user dynamically add fields to add the amount of brands they sell. These values are stored in an array in my database.

The problem I'm having now is that I want to create some sort of filter. I created a page that shows all images of the possible brand logos. These logos are having a link with a custom value added to it (brandname). For example:

<a href="somepage.php?brand='brandname'"><img src="brandname.jpg"></a>

On the page that follows I do a get request to retrieve the value added to the url. With this value I would like to show all the companies that are selling the brand.

The last part of this is the problem. I don't know how to search in an array with the value added to the URL.

So I figured I need an SQL statement to SELECT * FROM $wpdb->postmeta WHERE the database meta_key field = 'brand' and the meta_value = the GET['brand'] from the url and found in the array.

-- UPDATE --
So I tried the solution from DiegoCoderPlus but give me not what I want:

global $post;
$merk = $_GET['merk'];
$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'brand'");
foreach ($results as $result){
    $post_id = $result->post_id;
    $brand = get_post_meta($post_id, 'brand', false); 
    $brandNeedle = $merk;                   
    if(in_array($brandNeedle, $brand))
        {
            echo 'true<br>';
        }else
        {
            echo 'false<br>'; 
        }
    }

This gives me as a result two times false. Which is half good because there are only two companies in my database for testing. If I use a different brand it also shows two false results.
the $brand array looks like this. Maybe it helps:

Array
(
    [0] => Array
        (
            [0] => 
        ) 

)

false

Array
(
    [0] => Array
        (
            [0] => brand 1
            [1] => brand 2
        )

)
share|improve this question

1 Answer 1

up vote 2 down vote accepted

if your array is a php one, the only thing you need is in_array

it works like this:

$brands = array('brand1', 'brand2', 'brand3', 'brand4');
$brandNeedle = 'brand2';

if(in_array($brandNeedle, $brands))
{
   // it is on the array
}else
{
   // it is not 
}
share|improve this answer
    
I'm not sure how to check but thank you. This looks simple and usefull. I'll give it a try –  Interactive 2 days ago
    
please see my update –  Interactive 2 days ago
    
It seems like you have an array inside your brand array, so, what you need is to make the in_array look like , if(in_array($brandNeedle, $brand[0])) , so it searches on the inside array, do you get me ? –  DiegoCoderPlus 2 days ago
    
challenge solved @Interactive ? –  DiegoCoderPlus 2 days ago
    
Wow yes. Thank you makes perfect sence and it works. Great job –  Interactive 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.