0

I have been looking for answers on stack and they do have a few examples on this but for some reason the only way I can get this to display the categories is by using the foreach statement. Doesn't seem like any one else I have read on needs this but I cannot get the DB category list to display unless the foreach statement is included.

What I am trying to do, is to pull all of the current categories in the active item listing but I only need one of each listing. If there are 100 small_cents in the inventory list I only need it to show me the category once but I also need one of all the categories that currently have active items in the inventory in order to use in a later script to show a report of total price value of items in each category.

At the moment I have it showing me the full list of categories for every category that has active items in the inventory. The problem is that it is showing me the same category more than once, as many time as there are items in the inventory for that category. Please view the code below. I appreciate any help.

    function categories($dbCon){
        if($res = $this->dbConnection->query("SELECT categoryName FROM Table")){
            $categoryName = array();
                while($data = $res->fetch_assoc()){
                    $categoryName[] = $data['categoryName'];
                }
                foreach($categoryName as $categoryName){
                    echo $categoryName."\n";
                }
        }
    }

Instead this is what I am getting:

Small_Cents Small_Cents Small_Cents Small_Cents CVC_Top_Picks Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents Small_Cents

I only need:

Small_Cents

CVC_Top_Picks

If I add LIMIT 1 in the sql query I only get Small_Cents.

On the front end all I have is this

include('class/reports-class.php');
$reportsClass = new reportsClass($dbCon);

And

<?php $reportsClass->categories($dbCon); ?>
4
  • 1
    Did you try DISTINCT? Commented Sep 23, 2015 at 2:50
  • No I did not know of distinct, I will try it now. Idk why the down vote but ok lol Programmers are picky in this platform. Commented Sep 23, 2015 at 3:08
  • Yeah it's just looking for unique instances of a value in a column despite occurances Commented Sep 23, 2015 at 3:17
  • I appreciate your time. Spent a while on this and it's nice to see it work the way I want to. Commented Sep 23, 2015 at 3:23

1 Answer 1

1

Use ...

SELECT DISTINCT categoryName FROM Table
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.