php抽獎程式(抽取三名幸運觀眾)的核心代碼

來源:互聯網
上載者:User
  1. /**

  2. * php抽獎程式核心代碼
  3. * edit bbs.it-home.org
  4. */
  5. $sql = “select distinct(***),username from *** where daystatus=0 and ***=’{$yesterday}’ and daystatus=0″ ;
  6. $res = mysql_query( $sql, $g_mysql ) or sys_exit( “系統忙,請稍候再試”, mysql_error() ) ;
  7. $users = mysql_fetch_all( $res ) ; //擷取所有參與者資訊,每個人的資訊唯一
  8. $award_users = array() ;//建立中獎數組
  9. if ( count( $users ) else {
  10. $nums = get_award3( $users ) ; //執行抽獎程式,返回中獎者的下標
  11. echo “獲獎名單如下:” . ”

  12. ” ;

  13. foreach ( $nums as $key => $value ) {
  14. $award_users[] = $users[$value] ;
  15. echo “uid :” . $users[$value]['uid'] . ” 暱稱:” . $users[$value]['username'] ”
  16. ” ;
  17. $uids[$key] = $users[$value]['uid'] ;
  18. }
  19. }
  20. $_SESSION['users'] = ” ;
  21. $_SESSION['users'] = $award_users ; //後台審核抽獎名單入庫(確定)時用
  22. echo <<< EOF
  23. 抽獎資訊入庫

  24. EOF;

  25. function get_award3( $users ) {//抽獎函數,為了防止有重複,多了個判斷
  26. $count = count( $users ) ;
  27. $i = 0 ;
  28. $result = array() ;
  29. while ( $i < 3 ) {
  30. $j = rand( 0, $count – 1 ) ;
  31. if ( ! in_array( $j, $result ) ) {
  32. $result[$i] = $j ;
  33. $i++ ;
  34. }
  35. }
  36. return $result ;
  37. }
  38. ?>
複製代碼

大家可以參考下以上代碼的實現思路,有興趣的朋友,自己動手實現一個完整的php抽獎程式吧。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.