SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast OPW's Avatar
    Join Date
    Dec 2005
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Output column without repetition of identical fields

    Hey guys.
    On my site I have an option where users can submit comments which are stored in a MySQL table - something like this:
    User1 !! Hello
    User2 !! Hi there
    User3 !! Blah Blah
    User1 !! Blah Blah


    My question is this - how can I output in PHP the names of all of the Users who have submitted a comment, without repeting the same name twice if a user has submitted comments several times.
    Aka my output here would be:
    User1; User2; User3...
    and not
    User1; User2; User3; User1...

    I've searched around and know how to print just the column but can't find out how to avoid outputting the same name twice.
    All help is much appreciated

  2. #2
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I'm just going to have a bash here - but I'm suffering from last night Xmas party, so apologies if something does not work as intented.

    Do you have some kind of record of the users that you have? ie user table in the db. In that case you'll need to do it in 2 steps.

    ** pseudo code alert **
    Get all your users names
    SELECT username FROM users

    Then you user that info to query for comments that each user has
    SELECT comments FROM commentstbl WHERE username='username'

    Parse that, then move onto the next user.

  3. #3
    Internet Business Optimizer KCgame's Avatar
    Join Date
    Mar 2005
    Location
    Singapore
    Posts
    928
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi OPW,

    You can use DISTINCT to help you out
    Please refer to the following for an example:
    PHP Code:
    $result=mysql_query("SELECT DISTINCT username FROM usertable ",$link ) or die(mysql_error());                    

    while(
    $newarray=mysql_fetch_array($result)){
    //here will print user only if distinct
    echo"$newarray[0]<p>";


  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that this calls for a DISTINCT function, add it just after SELECT in your query.

    Edit : KCgame beat me to it.....read thread, then engage keyboard

  5. #5
    SitePoint Enthusiast OPW's Avatar
    Join Date
    Dec 2005
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot guys - DISTINCT worked great!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •