[mysql/php] help showing records from 2 tables in a while

Discussion in 'PHP' started by mordimi, Jul 2, 2008.

  1. #1
    hi, heres my situation:
    i have 2 table in the db
    1- annunci
    2-we_onlineusers

    in annunci theres ID and regione(its the state in italian) , in we_onlineusers USERID and IS_ONLINE and GUEST, where userid take the id of the user online, then is_online became 1 and guest is 0 when the user is not a guest.

    i have some record in annunci, where users insert their data to shows up in a ad page.

    so the page will show the users pickin their usernames from annunci where regione==$regione.

    i would like to check if the id from annunci is equal with the id in we_onlineusers so that will indicate the user is online.

    thats my code
    	$regione="Lazio";
      
      $query= "select * from annunci, we_onlineusers WHERE annunci.regione = '$regione' ORDER BY annunci.giorno DESC";
    $result = mysql_query($query, $con);
    
                      while ($fu=mysql_fetch_assoc($result)) { 
                      	 {extract ($fu);
    
      if ($id==$userid){echo "<font color=green>online</font>";}
       else {echo "<font color=pink>offline</font>";}
    PHP:
    that works, BUT, if there are guests online (so the we_onlineusers is populated by more records) the page will print a duplicated ad from the same person, were only one is online, and the other "clones" are marked as offline.
    thats because the parsing of we_onlineusers is included in the while.
    i tried making a "select * from we_onlineusers" outside the while, and leaving only the "select * from annunci" in the while (2 distinct select pratically) but it gave me error..

    any suggestion will be appreciated.
    thx, and i hope you understand my problem
     
    mordimi, Jul 2, 2008 IP
  2. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you do a dump of 2-3 records from each table and show it here? It would be alot easier if we could see that.
     
    Christian Little, Jul 2, 2008 IP
  3. mordimi

    mordimi Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    heres the record in the annunci table (1 ad made by stefy, id 130), and in we_onlineusers table, u can see 2 records in wE_onlineusers, one is the user logged in (with userid 130)the other one is a guest with id 0. in this case, , theres one "clone" in the ad section.


    [​IMG]
    annunci



    [​IMG]
    we_onlineusers
     
    mordimi, Jul 2, 2008 IP
  4. mordimi

    mordimi Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    help im stuck! :(
     
    mordimi, Jul 3, 2008 IP
  5. mordimi

    mordimi Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    up anyone? im goin insane :|
     
    mordimi, Jul 5, 2008 IP
  6. Chaos King

    Chaos King Peon

    Messages:
    88
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am still somewhat confused...

    In the table, we_onlineusers; is the userid column unique? Because I don't see how you can have guests all posting with the same userid = 0, I think that is why you are getting duplicate results.
     
    Chaos King, Jul 5, 2008 IP
  7. ralvez

    ralvez Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think this is what you are looking for:
    <sql code>
    select `regione` from annunci inner join we_onlineusers on userid = annunci.id and
    we_onlineusers.userid = annunci.id;
    </sql code>

    That should return the region if the records userid and id match in the tables annunci and we_onlineusers

    Hope this helps and that understood what you need. ;)

    R.
     
    ralvez, Jul 5, 2008 IP
  8. mordimi

    mordimi Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #8
    oh i just saw now the replies (the notify didnt work)..
    i just figured it out now using a function
    function is_online($id,$conny)
    {
    	if (mysql_query("SELECT userid FROM we_onlineusers WHERE userid=$id",$conny))
    	return"<font color=\"green\">online</font>";
      else return "<font color=\"pink\">offline</font>";
    	
    }
    PHP:
    then recalling it
    echo(is_online($id,$con));
    PHP:
    and it workslike a charm....

    i didnt now the "inner join"
    im goin to try i later

    thx both for the replies.
     
    mordimi, Jul 5, 2008 IP
  9. mordimi

    mordimi Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #9
    false victory, it shows always "online" als if the users are offline! damn


    edit-

    i saw the "inner join" i dont need it cause with that, if theres a user logged in, it shows the ad, but if the user is NOT logged in, it DOESNT shows the ad..i want instead a OFFLINE if the user is not online, and an online if the user is online :(


    @Chaos_king= the id is not unique cause guests CANT post, so i identify the guests by their id==0
     
    mordimi, Jul 5, 2008 IP
  10. mordimi

    mordimi Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #10
    resolved with this function
    function is_online($id,$conny)
    {
       if (mysql_num_rows(mysql_query("SELECT userid FROM we_onlineusers WHERE userid=$id",$conny))>0)
       return"<font color=\"green\">online</font>";
     else return "<font color=\"pink\">offline</font>";
       
    }
    
    PHP:
     
    mordimi, Jul 7, 2008 IP