Page 1 of 1

read 1 fields of the same type

Posted: Sat Sep 20, 2008 8:51 am
by pagegen
he guys

my items table
Make
rockport
rockport
timberland
timberland
timberland
timberland
nike
nike


my code at the moment

Code: Select all

    // SQL query to select the rows
                $queryS = "SELECT * FROM items WHERE type = '$sC'";
                $resultS = mysql_query ($queryS);
                                
                while($rowS = mysql_fetch_array($resultS,MYSQL_ASSOC))
                {
                Echo '<li><a href="products.php?action=2&item=' . $rowM[Id] . '&sub=' . $rowS[id] . '">' . $rowS[sub] . '</a></li>';
                
                }
                Echo '</ul></li></ul>';
                }
Problem am facing is
it well print all the makes at once, but i only want it to print 1 make.. example blow:
rockport
timberland
nike

Thank you very much

Re: read 1 fields of the same type

Posted: Sat Sep 20, 2008 10:24 am
by jaoudestudios
Use GROUP BY or DISTINCT in your sql query

Re: read 1 fields of the same type

Posted: Sat Sep 20, 2008 11:15 am
by pagegen
jaoudestudios wrote:Use GROUP BY or DISTINCT in your sql query

Code: Select all

           
                $queryM = "SELECT * FROM TblItems WHERE cat = '$cat'";
                $resultM = mysql_query ($queryM);
 
                while($rowM = mysql_fetch_array($resultM,MYSQL_ASSOC))
                {
                if($make == $rowM[make]){
                } else {
                Echo '<tr><td><a href="products.php?action=4&item=' . $rowM[cat] . '&iMake=' . $rowM[make] . '">' . $rowM[make] . '</a></td></tr>';
                $make = $rowM[make];
                
                }
                }
 
the above works just fine 4 me,, but isit a good idea? or can it fail?

Re: read 1 fields of the same type

Posted: Sat Sep 20, 2008 11:25 am
by jaoudestudios
It can fail.

Because you are only checking the next one, what happens if the next one is different and the one after is the same. Whats going on with your IF statement it is empty and you are only using the ELSE?

Definitely do it in the sql. Even if you managed to improve your system it is a waste of resources, what is the point of bringing all the data out, then sorting it and only using part of it. SQL is designed to do all that for you, then you only ever bring out what is needed.

Re: read 1 fields of the same type

Posted: Sat Sep 20, 2008 11:52 am
by pagegen
jaoudestudios wrote:It can fail.

Because you are only checking the next one, what happens if the next one is different and the one after is the same. Whats going on with your IF statement it is empty and you are only using the ELSE?

Definitely do it in the sql. Even if you managed to improve your system it is a waste of resources, what is the point of bringing all the data out, then sorting it and only using part of it. SQL is designed to do all that for you, then you only ever bring out what is needed.
after more testing, i would like to thank you very much :)
as GROUP BY works great n is more reliable