1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Please help me with a loop. (serious noob)

Discussion in 'PHP' started by x0x, Feb 1, 2009.

  1. #1
    I usually try avoiding loops, but this time I can't.


    <?
    $disabledusrs= mysql_query("SELECT username FROM $tab[user] WHERE receiveletter='no'");
    while($pullthem = mysql_fetch_array($disabledusrs)){
    
    
    // pulls top 3 players
    $gettop3 = mysql_query("SELECT * FROM $tab[topguys] WHERE username != '' AND username != '$pullthem[username]' ORDER BY win DESC, played DESC, lost ASC LIMIT 3");
    
    
    $keyr = 1;
    $winner = array();
    
    while($rowin = mysql_fetch_array($gettop3)){
    $winner[$keyr] = $rowin;
    $keyr++;
    
    }
    }
    ?>
    PHP:

    It was working before, but then I added another loop (the first one) and tried to connect them...

    The point is that there are 2 tables. 1st one is users, second one is topguys. Both of the tables have data with the same usernames... I want gettop3 to pull only these usernames from table topguys where username != disabledusrs[username].

    So it looks at one table and finds out where the user has receiveletter='no', then the other loop takes the username and skips all users with receiveletter='no'.

    I did my best explaining the issue.

    :confused::confused:
     
    x0x, Feb 1, 2009 IP
  2. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Dont go for loop. Just use inner join.
     
    mrmaf, Feb 2, 2009 IP
  3. InovvativeTech

    InovvativeTech Banned

    Messages:
    32
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3


    You can use subquery

    SELECT * FROM $tab[topguys] WHERE username != '' AND username NOT IN(SELECT username FROM $tab[user] WHERE receiveletter='no') ORDER BY win DESC, played DESC, lost ASC LIMIT 3
     
    InovvativeTech, Feb 2, 2009 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    I didn't know such thing existed! Oh boy, I've wasted so much time when I could just have used that trick. Thanks, I'm going to give it a try!
     
    x0x, Feb 2, 2009 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Okay, it works. THANK YOU.

    It takes about 3 seconds to load on my local server to open up the page. Is that normal?
     
    x0x, Feb 2, 2009 IP
  6. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    OK, I think I definitely need a new solution. it takes about 20 seconds to load on the production server... Plus the queries are probably killing the server. Any ideas guys?
     
    x0x, Feb 2, 2009 IP