Remove duplicate category name

Discussion in 'PHP' started by bumbar, Apr 2, 2007.

  1. #1
    Hallo!

    Here is my simple script who fetching data from database.
    There is two tables "category" and "products".
    Here is tables structure:


    CREATE TABLE `category` (
    `id` int(4) NOT NULL auto_increment,
    `name` text NOT NULL,
    PRIMARY KEY (`id`)
    );

    CREATE TABLE `products` (
    `id` int(4) NOT NULL auto_increment,
    `category_id` int(4) NOT NULL default '0',
    `product` text NOT NULL,
    PRIMARY KEY (`id`)
    )


    
    mysql_connect ("localhost", "user", "pass");
    
    mysql_select_db ("mycatalog");
    
    $select = "
    	SELECT 
    		 name,
    		 product
    	FROM
    		category, 
    		products
    	WHERE
    		category.id = products.category_id
    ";
    
    $query = mysql_query ("$select") or die (mysql_error());
    
    echo "<table border=1>"; 
    
    while ($row = mysql_fetch_array ($query)) {
    	
    	echo "<tr><td>" . "<b>" . $row['name'] . "</b>" . "</td><td>" . $row['product'] . "</td></tr>";
    	  
    }
    
    echo "</table>";
    
    PHP:
    But data who I fetch is:

    Category products
    Apple red
    Apple green
    Pear yellow
    Pear green

    How can I remove duplicate Apple, Pear ...?
    I want this:

    Apple red green
    Pear yellow green

    Thanks!
     
    bumbar, Apr 2, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    nico_swd, Apr 2, 2007 IP
  3. bumbar

    bumbar Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    I tried with DISTINCT but nothing to result...

    I think in this piece of code need some "if" checks:

    
    
    while ($row = mysql_fetch_array ($query)) {
    	
    	echo "<tr><td>" . "<b>" . $row['name'] . "</b>" . "</td><td>" . $row['product'] . "</td></tr>";
    	  
    }
    
    
    PHP:
     
    bumbar, Apr 2, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Sure you could do that, but it's not the best way. You'd be pulling unnecessary data from the database. Give this a try.
    
    $select = "
        SELECT 
             DISTINCT name,
             product
        FROM
            category, 
            products
        WHERE
            category.id = products.category_id
    	GROUP BY
    		products.category_id
    ";
    
    
    PHP:
     
    nico_swd, Apr 2, 2007 IP
  5. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think you should GROUP your results by name
     
    manilodisan, Apr 2, 2007 IP
  6. bumbar

    bumbar Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #6
    Mens, unfortunately, I haven`t good result ...

    if I use

    
    
    $select = "
        SELECT DISTINCT 
             name,
             product
        FROM
            category, 
            products
        WHERE
            category.id = products.category_id
        GROUP BY
            products.category_id
    ";
    
    PHP:
    Here I get this

    Apple - red (Here I lose "green")
    Pear - yellow (And Here I lose "green")

    :rolleyes:
     
    bumbar, Apr 3, 2007 IP
  7. Perrow

    Perrow Well-Known Member

    Messages:
    1,306
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    140
    #7
    You should use the above with group_concat, search mysql docs for it for exact syntax, it should be something like this.

    
    
    $select = "
        SELECT DISTINCT 
             name,
             group_concat(product, ' ')
        FROM
            category, 
            products
        WHERE
            category.id = products.category_id
        GROUP BY
            products.category_id
    ";
    
    PHP:
     
    Perrow, Apr 3, 2007 IP