Home  >  Article  >  Backend Development  >  The core code of the PHP lottery program (to draw three lucky viewers)

The core code of the PHP lottery program (to draw three lucky viewers)

WBOY
WBOYOriginal
2016-07-25 08:58:411643browse
  1. /**

  2. * php lottery program core code
  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( “The system is busy, please try again later”, mysql_error() ) ;
  7. $users = mysql_fetch_all( $ res ) ; //Get all participant information, each person's information is unique
  8. $award_users = array() ;//Create a winning array
  9. if ( count( $users ) else {
  10. $nums = get_award3( $users ) ; / /Execute the lottery program and return the subscript of the winner
  11. echo "The list of winners is as follows:" . ”

  12. ” ;

  13. foreach ( $nums as $key => $value ) {
  14. $ award_users[] = $users[$value] ;
  15. echo "uid:" . $users[$value]['uid'] . " Nickname: " . $users[$value]['username'] "
  16. " ;
  17. $uids[$key] = $users[$value]['uid'] ;
  18. }
  19. }
  20. $_SESSION['users'] = ” ;
  21. $_SESSION['users'] = $award_users ; //Backend Use
  22. echo <<Lottery information to enter the database

  23. EOF;

  24. function get_award3( $users ) {//Lottery function, for To prevent duplication, there is an additional judgment
  25. $count = count( $users ) ;
  26. $i = 0 ;
  27. $result = array() ;
  28. while ( $i < 3 ) {
  29. $j = rand( 0, $count – 1 ) ;
  30. if ( ! in_array( $j, $result ) ) {
  31. $result[$i] = $j ;
  32. $i++ ;
  33. }
  34. }
  35. return $result ;
  36. }
  37. ?>
Copy the code

You can refer to the implementation ideas of the above code. Friends who are interested can implement a complete PHP lottery program by themselves.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn