从数据库输出用户状态,但是以其他方式输出用户个人资料图片

时间:2013-11-18 18:27:34

标签: php sql database while-loop

我的网站一个接一个地提取用户状态更新。

[状态] [状态] [状态] [状态] [状态] [状态] [状态] [状态]

但我希望它有时会输出一个用户个人资料图片,但不是以统一的方式,更多的是随机方式。所以,有时像这样:

[status] [status] [picture] [status] [status] [status] [status] [picture]

和其他时间可能是这样的:

[图片] [图片] [状态] [状态] [图片] [状态] [状态]

我不确定如何实现这一点,这是我的代码目前的样子:

    $result = mysql_query(
                "SELECT 

                tbl_status.id as statID, 
                tbl_status.from_user as statFROM, 
                tbl_status.status as statSTATUS, 
                tbl_status.deleted as statDEL, 
                tbl_status.date as statDATE,

                tbl_users.id as usrID, 
                tbl_users.name as usrNAME,
                tbl_users.location as usrLOCATION,
                tbl_users.postcode as usrPOSTCODE,

                tbl_photos.profile as photosPROFILE,
                tbl_photos.photo_link as photoLINK,
                tbl_photos.default_photo as photoDEFAULT 

                FROM tbl_status 
                LEFT JOIN tbl_users ON tbl_status.from_user = tbl_users.id

                LEFT JOIN tbl_photos ON tbl_photos.profile = tbl_users.id 
                WHERE tbl_status.deleted = '0' AND tbl_photos.default_photo IS NULL OR tbl_photos.default_photo = '1'
                ORDER BY tbl_status.date desc
                LIMIT 100

                ");

  while($row = mysql_fetch_array($result))
    {echo' <div class="statusUsr">' . $row['statSTATUS'] . '</div>'}

2 个答案:

答案 0 :(得分:0)

你可以使用php的rand函数随机显示状态或照片:

  while($row = mysql_fetch_array($result)){        

        $rand = rand(1,2); // get a random number between 1 and 2

        if($rand%2 == 0) { // check if the random number is odd or even 

            // case 2
            echo' <div class="statusUsr">' . $row['statSTATUS'] . '</div>';
        } else {

            // case 1
            echo' <div class="imageUsr">' . $row['photoLINK'] . '</div>';
        }
  }

参考文献:

http://www.php.net/manual/en/function.rand.php

http://php.net/manual/en/language.operators.arithmetic.php

答案 1 :(得分:0)

也许你会有更多的随机性。

while($row = mysql_fetch_array($result))
{
    $nb = rand()%100; // get a random number from 0 to 100

    if ($nb&1) // check if it's odd or even
       echo' <div class="statusUsr">' . $row['statSTATUS'] . '</div>';
    else
       echo' <div class="imageUsr">' . $row['photoLINK'] . '</div>';
}