לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #1  
ישן 07-03-2007, 16:23
  Rs3k Rs3k אינו מחובר  
 
חבר מתאריך: 17.05.04
הודעות: 1,454
שלח הודעה דרך ICQ אל Rs3k
מחלקה - סקר

כתוצאה מבקשות רבות לסקר, החלטתי לפרסם כאן קוד של סקר שבניתי לא ממזמן (יש לציין כי המחלקה משתמשת במחלקות אחרות שלי) :









קוד PHP:
<?php

 
class phpp_Exception extends Exception{
 
private 
$class;
 
## sets Exception's constructor and sets the custom Exception
# null
 
function __construct($message false$code$class){
parent::__construct($message$code);
$this->class $class;
}
 
## null
# Exception's class
 
public function getClass(){
return 
$this->class;
}
}
class 
phpp_filter{
 
## timestamp validation
# boolean
 
static public function timestamp($timestamp){
switch(
gettype($timestamp)){
case 
'string':
    if(!
$timestamp strtotime($timestamp));
     throw new 
phpp_Exception('Cannot translate the given var into unix timestamp'3__CLASS__);
    break;
 
case 
'integer':
    break;
 
default:
    throw new 
phpp_Exception('Cannot translate the given var into unix timestamp'3__CLASS__);
}
 
return 
$timestamp;
}
 
## email validation
# valid - email; other - false.
 
static public function email($str){
return (
preg_match("/^[\w\-\.]+\@[\w\-\.]+\.[\w\-]+$/i",$str)) ? ($str) : (false);
}
 
## tags stripping
# stripped string
 
static public function notags($str){
return (
is_string($str)) ? ($str strip_tags(strip_tags($str))) : (false);
}             
 
## addslashes
# slashed string
 
static public function xss($str){
return (
is_string($str)) ? ($str addslashes($str)) : (false);
}
 
## numerical letters
# numerical letter given in the argumant
 
static public function numeric($var){
return (
is_numeric($var)) ? ($var) : ((is_string($var)) ? ($var preg_replace('/[^\d]/'''$var)) : (false));
}
 
## alphabetical letters
# alphabetical letters given in the argumant
 
static public function alphabetic($var){
return (
is_string($var)) ? ($var preg_replace("/[^[:alpha:]]/"''$var)) : (false);
}
}
class 
phpp_db{
public 
$queryNum;
private 
$key$queryResource;
 
## host and db settings
# host resource
 
function __construct($db false$user 'root'$host 'localhost'$pass null){
if(!
$this->key = @mysql_pconnect($host$user$pass))
throw new 
phpp_Exception('Cannot connect into the given host ('.$host.')'0__CLASS__);
 
if(
trim($db) && !@mysql_select_db($db$this->key))
throw new 
phpp_Exception('Cannot connect into the given database('.$db.')'1__CLASS__);
}
 
## queries the given string and argumants
# queryResource - can be used for the fetch.
 
public function query($query){
if((
$type gettype($query)) != 'string')
throw new 
phpp_Exception('Unsupported query type was given ('.$type.')'2__CLASS__);
 
foreach(
array_slice(func_get_args(), 1) as $value){
switch(
$type gettype($value)){
    case 
'object':
     
$value get_object_vars($value);
 
    case 
'array':
     while(list(
$key$val) = each($value))
     
$query preg_replace("/\[:".$key.":]/"$val$query);
     break;
 
    case 
'string':
    case 
'integer':
     
$query sprintf($query$value);
     break;
 
    default:
     throw new 
phpp_Exception('Unsupported argumant type was given ('.$type.')'3__CLASS__);
}
}
 
if(!
$this->queryResource mysql_query($query$this->key))
throw new 
phpp_Exception('Cannot query the given string - $query ('.mysql_error().')'4__CLASS__);
 
return 
$this->queryResource;
}
 
## fetches the query resource
# the fetched data - according to the method
 
public function fetchResult($query false$method 'object'){
if(!
$this->queryResource = ($query) ? ((is_resource($query)) ? $query $this->query($query)) : (isset($this->queryResource) ? ($this->queryResource) : (false)))
throw new 
phpp_Exception('Given query isn\'t a resource, as a result can\'t fetch'5__CLASS__);
switch(
$method){
case 
'num':
    
$queryFetch = @mysql_num_rows($this->queryResource);
    break;
case 
'affected':
    
$queryFetch = @mysql_affected_rows($this->key);
    break;
case 
'insertID':
    
$queryFetch = @mysql_insert_id($this->key);
    break;
case 
'object':
case 
'array':
case 
'assoc':
case 
'row':
case 
'lengths':
    
$method 'mysql_fetch_'.$method;
    
$queryFetch = @$method($this->queryResource);
    break;
default:
    throw new 
phpp_Exception('Unsupported fetch method was given'6__CLASS__);
}
 
return 
$queryFetch;
}
 
function 
__destruct(){
mysql_close($this->key);
unset(
$this);
}
}
/*
CREATE TABLE `polls` (
`title` VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`total` INT( 11 ) NOT NULL ,
`id` INT( 8 ) NOT NULL AUTO_INCREMENT PRIMARY KEY 
) ENGINE = innodb CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE `poll_options` (
`title` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`votes` INT( 11 ) NOT NULL ,
`pid` INT( 8 ) NOT NULL ,
`id` INT( 8 ) NOT NULL AUTO_INCREMENT PRIMARY KEY 
) ENGINE = innodb CHARACTER SET utf8 COLLATE utf8_unicode_ci;
*/
class phpp_poll{
private 
$poll$db;
function 
__construct($poll$persistant false){
$this->db = new phpp_db('phpp_poll');
 
if(
$persistant && is_string($poll)){
$this->db->query("INSERT INTO `polls` (`title`) VALUES ('%s')"phpp_filter::xss($poll));
$poll $this->db->fetchResult(false'insertID');
}
 
switch(
gettype($poll)){
case 
'integer':
    
$poll $this->db->query("SELECT polls.* FROM `polls` WHERE polls.id = '%s'"$poll);
case 
'resource':
    
$this->poll = ($this->db->fetchResult($poll'num')) ? ($this->db->fetchResult($poll)) : (false);
     break;
default:
    throw new 
phpp_Exception('Unsupported type was given'0__CLASS__);
}
 
if(!
$this->poll)
throw new 
phpp_Excpetion('given poll doesn\'t exists in the database'1__CLASS__);
}
## null
# current poll's data - object
 
public function getData(){
return 
$this->poll;
}
 
## queries the given option(s)
# the option(s) - if opID is int - object, boolean - array with the poll's assigned options
 
public function getOpts($opID false){
switch(
gettype($opID)){
case 
'integer':
    
$this->db->query("SELECT poll_options.* FROM `poll_options` WHERE poll_options.id = '%s' AND poll_options.pid = '[:id:]'", (integer) $opID$this->poll);
    
$options $this->db->fetchResult();
    break;
case 
'boolean':
    
$this->db->query("SELECT poll_options.* FROM `poll_options` WHERE poll_options.pid = '[:id:]'"$this->poll);
    while(
$option $this->db->fetchResult())
     
$options[$option->id] = $option;
    break;
 
default:
    throw new 
phpp_Exception('Unsupported type was given'0__CLASS__);
}
 
return 
$options;
}
 
## queries the current poll's options
# the percentage of each option
 
public function getPercentage(){
foreach(
$this->getOpts() as $id => $option)
$options[$option->title] = round(($option->votes 100) / $this->poll->total);
return 
$options;
}
 
## votes to the given option - only if is assigned to the current poll
# true on sucsess, false on failure
 
public function vote($opID){
$this->db->query("UPDATE `poll_options`, `polls` SET polls.total = polls.total +1, poll_options.votes = poll_options.votes +1 WHERE poll_options.id = '%s' AND poll_options.pid = '[:id:]'", (integer) $opID$this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
## assigns the given option
# true on sucsess, false on failure
 
public function assignOpt($str){
$this->db->query("INSERT INTO `poll_options` (poll_options.title, poll_options.pid) VALUES('%s', '[:id:]')"phpp_filter::xss($str), $this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## updates the given option - only if assigned to the current poll
# true on success, false on failure
 
public function updateOpt($opID$str){
$this->db->query("UPDATE `poll_options` SET poll_options.title = '%s' WHERE poll_options.od = %s AND poll_options.pid = [:id:]"phpp_filter::xss($str), (integer) $opID$this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## updates the current poll's title
# true on success, false on failure
 
public function updateTitle($str){
$this->db->query("UPDATE `polls` SET polls.title = '%s' WHERE polls.id = '[:id:]'"phpp_filter::xss($str), $this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## delete the given option - only if assigned to the current poll
# true on sucsess, false on failure
 
public function delOpt($opID){
$this->db->query("DELETE poll_options.* FROM `poll_options` WHERE poll_options.pid = '[:id:]' AND poll_options.id = '%s'"$this->poll, (integer) $opID);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## deletes the given poll
# true on sucsess, false on failure
 
public function del(){
$this->db->query("DELETE polls.*, poll_options.* FROM `polls`, `poll_options` WHERE polls.id = poll_options.pid = '[:id:]'"$this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
public function 
__destruct(){
unset(
$this);
}
}
?>




התכלס סקר ללא שאר המחלקות:
קוד PHP:
 class phpp_poll{
private 
$poll$db;
function 
__construct($poll$persistant false){
$this->db = new phpp_db('phpp_poll');
 
if(
$persistant && is_string($poll)){
$this->db->query("INSERT INTO `polls` (`title`) VALUES ('%s')"phpp_filter::xss($poll));
$poll $this->db->fetchResult(false'insertID');
}
 
switch(
gettype($poll)){
case 
'integer':
    
$poll $this->db->query("SELECT polls.* FROM `polls` WHERE polls.id = '%s'"$poll);
case 
'resource':
    
$this->poll = ($this->db->fetchResult($poll'num')) ? ($this->db->fetchResult($poll)) : (false);
     break;
default:
    throw new 
phpp_Exception('Unsupported type was given'0__CLASS__);
}
 
if(!
$this->poll)
throw new 
phpp_Excpetion('given poll doesn\'t exists in the database'1__CLASS__);
}
## null
# current poll's data - object
 
public function getData(){
return 
$this->poll;
}
 
## queries the given option(s)
# the option(s) - if opID is int - object, boolean - array with the poll's assigned options
 
public function getOpts($opID false){
switch(
gettype($opID)){
case 
'integer':
    
$this->db->query("SELECT poll_options.* FROM `poll_options` WHERE poll_options.id = '%s' AND poll_options.pid = '[:id:]'", (integer) $opID$this->poll);
    
$options $this->db->fetchResult();
    break;
case 
'boolean':
    
$this->db->query("SELECT poll_options.* FROM `poll_options` WHERE poll_options.pid = '[:id:]'"$this->poll);
    while(
$option $this->db->fetchResult())
     
$options[$option->id] = $option;
    break;
 
default:
    throw new 
phpp_Exception('Unsupported type was given'0__CLASS__);
}
 
return 
$options;
}
 
## queries the current poll's options
# the percentage of each option
 
public function getPercentage(){
foreach(
$this->getOpts() as $id => $option)
$options[$option->title] = round(($option->votes 100) / $this->poll->total);
return 
$options;
}
 
## votes to the given option - only if is assigned to the current poll
# true on sucsess, false on failure
 
public function vote($opID){
$this->db->query("UPDATE `poll_options`, `polls` SET polls.total = polls.total +1, poll_options.votes = poll_options.votes +1 WHERE poll_options.id = '%s' AND poll_options.pid = '[:id:]'", (integer) $opID$this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
## assigns the given option
# true on sucsess, false on failure
 
public function assignOpt($str){
$this->db->query("INSERT INTO `poll_options` (poll_options.title, poll_options.pid) VALUES('%s', '[:id:]')"phpp_filter::xss($str), $this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## updates the given option - only if assigned to the current poll
# true on success, false on failure
 
public function updateOpt($opID$str){
$this->db->query("UPDATE `poll_options` SET poll_options.title = '%s' WHERE poll_options.od = %s AND poll_options.pid = [:id:]"phpp_filter::xss($str), (integer) $opID$this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## updates the current poll's title
# true on success, false on failure
 
public function updateTitle($str){
$this->db->query("UPDATE `polls` SET polls.title = '%s' WHERE polls.id = '[:id:]'"phpp_filter::xss($str), $this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## delete the given option - only if assigned to the current poll
# true on sucsess, false on failure
 
public function delOpt($opID){
$this->db->query("DELETE poll_options.* FROM `poll_options` WHERE poll_options.pid = '[:id:]' AND poll_options.id = '%s'"$this->poll, (integer) $opID);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
 
## deletes the given poll
# true on sucsess, false on failure
 
public function del(){
$this->db->query("DELETE polls.*, poll_options.* FROM `polls`, `poll_options` WHERE polls.id = poll_options.pid = '[:id:]'"$this->poll);
 
return (
$this->db->fetchResult(false'affected')) ? true false;
}
public function 
__destruct(){
unset(
$this);
}








לצפיה בדף נפרד

3 המחלקות המופיעות (חוץ מזו של הסקר) נמצאות בשימוש במחלקה, אך אפשרי כמובן לשנות את זה כך ששלוש המחלקות לא יהיו נחוצות.

אפשרויות

הוספת אפשרות:



קוד PHP:
 $poll->assignOpt('MyOption'// assigns an option with the title 'MyOption' - auto id 

עדכון אפשרות:



קוד PHP:
 $poll->updateOpt(1'MySpecialOption'// changes opt.1 title into MySpecialOption - Only if opt.1 is assigned to the current poll 

מחיקת אפשרות:



קוד PHP:
 $poll->deleteOpt(1// deletes opt.1 - only if assigned to the current poll 



הצבעה



קוד PHP:
 $poll->vote(1); // votes opt.1 - only if assigned to the current poll 

הסקר

פניה והוספת סקר:




קוד PHP:
 $poll = new phpp_poll(1// getting poll no.1 - if doesn't exists - Exception(1)
$poll = new phpp_poll('Mypoll'true// setting up a poll with the title Mypoll - auto id - no options yet.
$poll = new phpp_poll('Mypoll'// Exception(0) 


מחיקת הסקר:



קוד PHP:
 $poll->del() // deletes the current poll 

שינוי כותרת הסקר:



קוד PHP:
 $poll->updateTitle('myTitle'// changes the current poll's title in to myTitle 



קבלת אפשרויות הסקר:



קוד PHP:
 $poll->getOpts() // returns an array with the options and their data (data as an object)$poll->getOpts(1) // returns the data of opt.1 (data as an object) 



קבלת אחוזי אפשרויות הסקר:



קוד PHP:
 $poll->getPercentage() // returnes an array with the percentage for each option 



קבלת מידע הסקר:



קוד PHP:
 $poll->getData() // returns an object with the current poll's data 




אשמח אם תחפשו באגים, הציעו הצעות, העירו הערות וכדו'..


יש לציין כי מסיבה שאינה ידועה לי, כאשר שולפים את אחוזי אפשרויות הסקר, זה מתעוות אם מתישהו בזמן הריצה התרחשה הצבעה לאפשרות כלשהיא.



אשמח לעזרה בעניין התקלה.


מקווה שעזרתי (במיוחד לאלה שתמיד שואלים ל-סקר)
_____________________________________
EVERYTHING SHOULD BE MADE AS SIMPLE AS POSSIBLE, BUT NOT ONE BIT SIMPLER
ALBERT EINSTEIN


נערך לאחרונה ע"י Rs3k בתאריך 07-03-2007 בשעה 16:36.
תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 12:23

הדף נוצר ב 0.14 שניות עם 10 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2024 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר