Troche nie wiem co masz na myśli. Scrypt sondy był juz gotowy nie ja go pisałem:
Moze przytocze zawartość obu plików:
poll.php
Kod
<?php
// _______________
//------------------------< SOME INCLUDES >-------------------------------------
// ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
include 'config.php';
include 'includes/functions.php';
require_once("includes/configmagik.php");
// __________________
//-----------------------< GLOBAL VARIABLES >-----------------------------------
// ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
// current script name
$currentFile= array_pop(explode("/", $_SERVER["SCRIPT_NAME"]));
// txt/db file path
$txtPath = $scriptPath."txt/txtdb.ini.php";
$imagesPath = $scriptPath."images/";
/******************************************************************************/
// MAIN SCRIPT //
poll($txtPath, $imagesPath);
?>
includes/functions.php
Kod
<?php
/*******************************************************************************
**** MAIN POLL FUNCTIONS ****
*******************************************************************************/
function poll($path="txt/txtdb.ini.php", $img="images/") {
global $width;
// create and configure new ConfigMagik-Object
$dbTXT = new ConfigMagik( $path, true, true);
$dbTXT->SYNCHRONIZE = false;
// if poll exist
if($dbTXT->get('pollid','MAIN')!=null) {
// se č stato inserito un voto...
if ( (isset($_POST['voto'])) && ($_POST['voto']!="") ) {
// Estraggo l'id del sondaggio (pollid)
$pollid = $dbTXT->get('pollid', 'MAIN');
// Controlla se l'utente ha giŕ votato questo sondaggio
// Se non ha mai votato questo sondaggio...
if ( !isset($_COOKIE['poll4all']) || $_COOKIE['poll4all']!=$pollid ) {
// registra il voto
$anKey = "an".$_POST['voto']."poll";
$dbTXT->increment($anKey, 'ANSWERS');
if ($dbTXT->save()) $msg = "<br />- Dzięki za głos -";
else print "<div style=\"width: ".$width."px; text-align: center;\">- Error during storing vote -</div>";
// Invio il cookie che notifica l'avvenuta votazione
setcookie( "poll4all", $pollid, time()+60*60*24*360 );
}
// visualizza il sondaggio (risultati)
pollPrint($dbTXT, $img, $msg);
}
else {
// visualizza il form per la votazione
pollForm($dbTXT);
}
}
// if poll doesn't exist
else print "<div style=\"width: ".$width."px; text-align: center;\">- No Poll Available -</div>";
}
/*******************************************************************************
**** VISUALIZZA RISULTATI SONDAGGIO ****
*******************************************************************************/
function pollPrint(&$dbTXT, $imgPath="images/", $msg="") {
global $border,$width,$bgColor,$percentageBg,
$cellpadding,$questionAlign,$optionsAlign,
$questionColor,$optionColor,$questionBgColor,
$font,$answerSize,$RandomColors,$questionSize,$barBg;
// inizializzo l'array contenente i numeri corrispondenti ai colori per gli istogrammi
$colors = array(1,2,3,4,5,6,7);
if ($RandomColors) shuffle($colors);
$numAnswers = $dbTXT->get('numAnswers', 'MAIN');
$question = $dbTXT->get('question', 'MAIN');
$totVoti = votiTotali($dbTXT);
echo "<!-- Poll Box -->
<table style=\"width: ".$width."px; background-color: ".$bgColor."; border: ".$border."px solid #000000; padding: ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px;\">
<!-- QUESTION -->
<tr><td style=\"text-align: ".$questionAlign."; background-color: ".$questionBgColor."; font-family: ".$font.",Arial; font-size: ".$questionSize."px; color: ".$questionColor."; padding: 5px 5px 5px 5px;\">$question</td></tr>
<!-- Separator -->
<tr><td style=\"width: ".$width."px; height: 5px;\"> </td></tr>";
// visualizza voti
for ($i=0; $i<$numAnswers; $i++) {
$answerKey = "an".($i+1);
$pollKey = $answerKey."poll";
$answer = $dbTXT->get($answerKey,"ANSWERS");
$poll = $dbTXT->get($pollKey,"ANSWERS");
// calcolo la percentuale evitando la divisione per zero
if ($poll!=0) {$percentuale=(($poll*100)/$totVoti);}
else $percentuale=0;
if ($percentuale) {
$w = (int)(((($width-($cellpadding*2)-8)*0.01)*$percentuale));
$barra = "<img src=\"".$imgPath."".$colors[$i].".gif\" width=\"$w\" height=\"8\" alt=\"bar\" />";
}
else $barra = "<img src=\"".$imgPath."0.gif\" width=\"8\" height=\"8\" alt=\"no result\" />";
echo "<!-- Text Answer -->
<tr><td style=\"text-align:".$optionsAlign."; background-color: ".$percentageBg."; font-family: ".$font.",Arial; font-size: ".$answerSize."px; color: ".$optionColor.";\">$answer: $poll votes (".round($percentuale,1)."%)</td></tr>
<!-- Bar -->
<tr><td style=\"text-align: left; background-color: ".$barBg.";\">$barra</td></tr>";
}
// toal votes
echo "<tr><td style=\"text-align: center; font-size: ".$answerSize."px;\">Głosów: $totVoti.$msg</td></tr>
</table>";
}
/*******************************************************************************
**** VISUALIZZA IL FORM PER VOTARE ****
*******************************************************************************/
function pollForm(&$dbTXT) {
global $border,$width,$cellpadding,$questionAlign,$questionBgColor,
$font,$answerSize,$optionColor,$bgColor,$oddBgColor,$evenBgColor,
$questionSize,$questionColor;
$currentFile= array_pop(explode("/", $_SERVER["SCRIPT_NAME"]));
if (isset($_SERVER['QUERY_STRING'])) $query="?".$_SERVER['QUERY_STRING'];
$numAnswers = $dbTXT->get('numAnswers', 'MAIN');
$question = $dbTXT->get('question', 'MAIN');
$totVoti = votiTotali($dbTXT);
echo "<!-- Poll Form Table -->
<form name=\"sondaggio\" method=\"post\" action=\"".$currentFile.$query."\">
<table style=\"width: ".$width."px; background-color: ".$bgColor."; border: ".$border."px solid #000000; padding: ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px ".$cellpadding."px;\">
<!-- QUESTION -->
<tr><td colspan=\"2\" style=\"text-align: ".$questionAlign."; background-color: ".$questionBgColor."; font-family: ".$font.",Arial; font-size: ".$questionSize."px; color: ".$questionColor.";\">$question</td></tr>";
for ($i=1; $i<=$numAnswers; $i++) {
$anKey = "an".$i;
$answer = $dbTXT->get($anKey, 'ANSWERS');
if ( $i&1 ) $bgColor = $oddBgColor;
else $bgColor = $evenBgColor;
echo "<!-- Answers -->
<tr><td style=\"width: ".($width-10)."px; font-family: ".$font.",Arial; font-size: ".$answerSize."px; background-color: ".$bgColor.";\">$answer</td>
<td style=\"width: 10px; background-color: ".$bgColor.";\"><input type=\"radio\" name=\"voto\" value=\"$i\" /></td>
</tr>";
}
echo "<!-- Submit Button -->
<tr><td colspan=\"2\" style=\"text-align: center; padding: 5px 0px 0px 0px;\">
<input type=\"submit\" name=\"submit\" value=\"Głosuj\" /></td>
</tr></table></form>";
}
/*******************************************************************************
******* RITORNA I VOTI TOTALI ******
*******************************************************************************/
function votiTotali(&$dbFile) {
$tot=0;
$numAnswers = $dbFile->get('numAnswers', 'MAIN');
for ($i=1; $i<=$numAnswers; $i++) {
$pollKey = "an".$i."poll";
$tot+=$dbFile->get($pollKey,"ANSWERS");
}
return $tot;
}
?>
Cały scrytp do podglądu jest tutaj:
Sonda.RAR