0

I'm using two functions here, get_user_set1 and get_user_set2.

On my page I have div1 that displays 12 users, and then div2 which also displays 12 users. I have 24 users all together.

What I am trying to do is display all 24 users across the 2 divs so 12 in each. at the moment I have Limit 0, 12 for the first function and then 12, 24 for the second which displays the first 12 users in the first div and then the second function which displays the next 12 users in the second div.

However, I am trying to figure out how I can make the order in which the 24 users are displayed random without creating a duplicate user and whilst still displaying each of the 24 users and still limiting it to 12. can someone please show me how could I do this?

Thanks.

function 1: 

function get_user_set1() {
            global $connection;
            $query = "SELECT *
                        FROM ptb_users, ptb_profiles
                        WHERE ptb_users.account_type = \"User\"
                        AND ptb_users.account_status = \"Active\"
                        AND ptb_profiles.user_id = ptb_users.id
                        AND ptb_users.subscription = \"Free\"
                        ORDER BY RAND()
                        LIMIT 0 , 12";
            $get_user_set1 = mysql_query($query, $connection);
            confirm_query($get_user_set1);
            return $get_user_set1;
        }
function 2:

        function get_user_set2() {
            global $connection;
            $query = "SELECT *
                        FROM ptb_users, ptb_profiles
                        WHERE ptb_users.account_type = \"User\"
                        AND ptb_users.account_status = \"Active\"
                        AND ptb_profiles.user_id = ptb_users.id
                        AND ptb_users.subscription = \"Free\"
                        ORDER BY RAND()
                        LIMIT 12 , 24";
            $get_user_set2 = mysql_query($query, $connection);
            confirm_query($get_user_set2);
            return $get_user_set2;
        }
4

5 回答 5

1

EveryORDER BY RAND()为您提供了一组不同的随机排序的用户,因此使用第二个查询来询问“下一个”12 个用户是没有意义的——您从不同的随机顺序中获得下一个 12 个用户。

如果您真正想要做的是在两组 12 个中显示 24 个用户,请SELECT使用 a 做一个LIMIT 24,然后将集合切成两半。

于 2013-02-09T22:50:19.777 回答
0

您只需要一个查询,当您执行循环以显示每个查询时,您可以在循环内使用“IF”来控制迭代器是否小于 12,并将其打印在正确的 DIV 中。如果您需要更多详细信息,请告诉我。

于 2013-02-09T22:38:15.960 回答
0

我想知道为什么你有两个功能,你可以只做一个查询并通过限制变量控制它。

         "SELECT *
                    FROM ptb_users, ptb_profiles
                    WHERE ptb_users.account_type = \"User\"
                    AND ptb_users.account_status = \"Active\"
                    AND ptb_profiles.user_id = ptb_users.id
                    AND ptb_users.subscription = \"Free\"
                    ORDER BY RAND()
                     $limit ";
                        ^---------limit variable here

像这样的东西

 if (something here ){$limit = 'LIMIT 0,12'; }
 if (something else){$limit = 'LIMIT 12,24';}
于 2013-02-09T22:43:06.577 回答
0

如果我正确理解了这个问题,那么您有两个用户部门。您希望从每个中随机选择 12 个,然后您希望随机呈现它们。

下面通过对每个组使用子查询然后将它们联合在一起并随机排序来做到这一点:

select *
from ((SELECT *
       FROM ptb_users join
            ptb_profiles
            on ptb_profiles.user_id = ptb_users.id
       WHERE ptb_users.account_type = \"User\" and
             ptb_users.account_status = \"Active\" and
             ptb_users.subscription = \"Free\" and
             DIVISION = DIV1
       ORDER BY RAND() 
       limit 12
      ) union all
      (SELECT *
       FROM ptb_users join
            ptb_profiles
            on ptb_profiles.user_id = ptb_users.id
       WHERE ptb_users.account_type = \"User\" and
             ptb_users.account_status = \"Active\" and
             ptb_users.subscription = \"Free\" and
             DIVISION = DIV2
       ORDER BY RAND() 
       limit 12
      )
    ) t
ORDER BY RAND()
于 2013-02-09T23:20:10.963 回答
0

不要进行两次查询,而是进行一次以下查询:

SELECT *
FROM ptb_users, ptb_profiles
WHERE ptb_users.account_type = \"User\"
AND ptb_users.account_status = \"Active\"
AND ptb_profiles.user_id = ptb_users.id
AND ptb_users.subscription = \"Free\"
ORDER BY RAND()
LIMIT 0 , 24

使用 PHP 在一个 div 中显示结果的前 12 行,在另一个 div 中显示后 12 行。

于 2013-02-09T22:52:28.473 回答