read 1 fields of the same type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pagegen
Forum Commoner
Posts: 32
Joined: Sat May 31, 2008 6:38 am

read 1 fields of the same type

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: read 1 fields of the same type

Post by jaoudestudios »

Use GROUP BY or DISTINCT in your sql query
pagegen
Forum Commoner
Posts: 32
Joined: Sat May 31, 2008 6:38 am

Re: read 1 fields of the same type

Post 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?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: read 1 fields of the same type

Post 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.
pagegen
Forum Commoner
Posts: 32
Joined: Sat May 31, 2008 6:38 am

Re: read 1 fields of the same type

Post 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
Post Reply