PHP5 + mysql
Спойлер (Отобразить)
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- <?PHP
- class poll
- {
- private $prefix = '';
- private $mysql = false;
- public $name = '';
- private $color = array('#ffa1a1', '#a1ffbe', '#a1d8ff', '#cba1ff', '#f9ffa1', '#d2ffa1', '#a1ffe1', '#00ffff', '#ffa500', '#ffcba1');
- private $mysqli = false;
- private $charset = 'UTF8';
- '<li><input type="radio" name="answer" value="{value}">{name}</li>',
- '<input type="submit" name="submit" value="Голосовать"></ul></div></form>');
- '<li><b>{result}</b> {percent}% (голосов: {count})</li>
- <div style="width:{percent}px;background:{color};height:10px;border:1px #555555 solid;"></div>',
- '</ul></div>');
- function __construct($user, $pass = '', $dbname = '', $host = 'localhost')
- {
- {
- $this->mysql = &$user;
- $this->query('SET NAMES '.$this->charset);
- return true;
- }
- if($this->mysqli)
- {
- $this->mysql = new mysqli($host, $user, $pass, $dbname);
- }
- else
- {
- }
- $this->query('SET NAMES '.$this->charset);
- }
- function check($name = '', $ip = '')
- {
- if($this->mysql === false) return 'connect failed';
- $res = $this->fetch_object($this->query('SELECT count(*) AS `count` FROM `'.$this->prefix.'poll_result` WHERE name = "'.mysql_escape_string($this->name).'" AND ip = "'.$ip.'"'));
- }
- {
- if($this->mysql === false) return 'connect failed';
- $q = $this->query('SELECT `name` FROM `'.$this->prefix.'poll` ORDER BY rand()');
- $this->name = $this->fetch_object($q)->name;
- return $this->name;
- }
- function show()
- {
- if($this->mysql === false) return 'connect failed';
- $q = $this->query('SELECT `name` FROM `'.$this->prefix.'poll`');
- while($ob = $this->fetch_object($q)) $name[] = $ob->name;
- return $name;
- }
- function create($name, $question, $answer)
- {
- if($this->mysql === false) return 'connect failed';
- $q = $this->query("INSERT INTO `".$this->prefix."poll` (`name`, `question`, `count`, `answers`) VALUES ('".mysql_escape_string(trim($name))."', '".mysql_escape_string(trim($question))."', '".count($answer)."', '".serialize($answer)."')");
- }
- function insert($answer, $name = '', $ip = '')
- {
- if($this->mysql === false) return 'connect failed';
- if($this->check($name, $ip) === true) return false;
- echo (int)$answer;
- //if(empty((int)$answer)) return false;
- $this->query("INSERT INTO `".$this->prefix."poll_result` (`result`, `ip`, `date`, `name`) VALUES ('$answer', '$ip', NOW(), '{$this->name}')");
- }
- {
- if($this->mysql === false) return 'connect failed';
- $ob = $this->fetch_object($this->query("SELECT count(`r`.`ip`) AS `ctn`, `p`.`count` FROM `".$this->prefix."poll_result` AS `r` LEFT JOIN `".$this->prefix."poll` AS `p` USING (`name`) WHERE `name` = '{$this->name}' GROUP BY `name`"));
- $this->i['all'] = $ob->ctn;
- for($i=1;$i<=$ob->count;$i++)
- {
- $q = $this->query("SELECT count(*) AS `count` FROM `".$this->prefix."poll_result` WHERE `name` = '{$this->name}' AND `result` = '{$i}'");
- $this->i[$i] = $this->fetch_object($q)->count;
- }
- }
- private function percent($x)
- {
- $percent = ($x / $this->i['all']) * 100;
- }
- function form($name = '', $ip = '')
- {
- if($this->mysql === false) return 'connect failed';
- if(!$this->check($this->name, $ip))
- {
- // Формируем вопрос
- $res = $this->fetch_object($this->query("SELECT * FROM `".$this->prefix."poll` WHERE name = '{$this->name}'"));
- {
- }
- $this->form .= '<input type="hidden" name="name" value="'.$this->name.'">';
- $this->form .= $this->Atpl[2];
- }
- else
- {
- // Формируем результаты
- $res = $this->fetch_object($this->query("SELECT * FROM `".$this->prefix."poll` LEFT JOIN `".$this->prefix."poll_result` USING (`name`) WHERE `name` = '{$this->name}'"));
- {
- $this->form .= str_replace(array('{result}','{percent}','{count}','{color}'), array($v, $this->percent($this->i[$k+1]), $this->i[$k+1], $this->color[array_rand($this->color)]), $this->Rtpl[1]);
- }
- $this->form .= $this->Rtpl[2];
- }
- return $this->form;
- }
- function delete($name, $ip = '')
- {
- if($this->mysql === false) return 'connect failed';
- {
- $q = $this->query("DELETE FROM `".$this->prefix."poll_result` WHERE `ip` = '$ip'".((!empty($name)) ? " AND `name` = '$name'" : ''));
- return ($q) ? 'Deleted successfully!' : 'Fail!';
- }
- $q = $this->query("DELETE FROM `".$this->prefix."poll` WHERE `name` = '$name'");
- $qq = $this->query("DELETE FROM `".$this->prefix."poll_result` WHERE `name` = '$name'");
- return ($q && $qq) ? 'Deleted successfully!' : 'Fail!';
- }
- function install()
- {
- if($this->mysql === false) return 'connect failed';
- $x = $this->query('CREATE TABLE `'.$this->prefix.'poll_result` (`id` int(10) NOT NULL AUTO_INCREMENT, `result` varchar(10) NOT NULL, `ip` varchar(20) NOT NULL, `date` timestamp NOT NULL, `name` text NOT NULL, PRIMARY KEY (`id`)) ENGINE = MyISAM DEFAULT CHARSET = '.$this->charset.(($this->charset == 'utf8') ? 'COLLATE '.$this->charset.'_unicode_ci' : ''));
- $xx = $this->query('CREATE TABLE `'.$this->prefix.'poll` (`name` varchar(30) NOT NULL, `question` text NOT NULL, `count` int(2) NOT NULL, `answers` text NOT NULL, PRIMARY KEY (`name`)) ENGINE = MyISAM DEFAULT CHARSET = '.$this->charset.(($this->charset == 'utf8') ? 'COLLATE '.$this->charset.'_unicode_ci' : ''));
- echo ($x && $xx) ? 'Install successful!' : 'Fail! Perhaps the table already exist.';
- }
- private function query($sql)
- {
- if($this->mysql === false) return 'connect failed';
- if($this->mysqli)
- $q = $this->mysql->query($sql);
- else
- return $q;
- }
- private function fetch_object(&$q)
- {
- if($this->mysql === false) return 'connect failed';
- if($this->mysqli)
- $ob = $q->fetch_object();
- else
- return $ob;
- }
- }
- ?>