1

I have a table in which I have some products and from them I want to get just unique values. I have a database in which I have values like Kia Picanto, Toyota Corolla, Kia Sportage, BMW Series 6, BMW Series 7 etc. Now I want to show values like Kia, Toyota, BMW. I am trying to search over the internet but didn't find any possible solutions so that's why I ma posting my question here to get the solution.I am using

SQL:

$sql = $db_con->prepare("SELECT `post_title` FROM `panel_product` WHERE `product_type` LIKE '%vehicle%' ORDER BY `post_title`");
$sql->execute();
$menufectures = $sql->fetchAll();
if (count($menufectures) > 0) {
    foreach ($menufectures as $key) {
        $brand = explode(' ', $key['post_title']);
        $brand = $brand[0];
        echo $brand.'<br />';
    }
}

But I am getting Kia, Kia, Toyota, BMW, BMW. Please let me know if you have any solution for this.

Thanks

1 Answer 1

2

You can store the ones you've displayed in an array that you can check while iterating:

// Initialize a new variable
$brands = [];

foreach ($menufectures as $key) {
    $brand = explode(' ', $key['post_title']);
    $brand = $brand[0];

    if (!isset($brands[$brand])) {
        // It doesn't exist in the array so echo it and add it to the array
        $brands[$brand] = true;
        echo $brand.'<br />';
    }
}
Sign up to request clarification or add additional context in comments.

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.