Voting class for posting votings to your homepage, simple to use. Requires MySQL
By : ShiVAs
<?php
class Voting {
var $db_host; // duomenų bazės adresas.
var $db_user; // duomenų bazės vartotojo vardas.
var $db_password; // duomenų bazės slaptažodis.
var $db_name; // duomenų bazės vardas.
var $db_table; // duomenų bazės lentelės vardas.
// jūsų klasės kodas.
function Voting($db_host,$db_user,$db_password,$db_name,$db_table)
{
// dabar gautus duomenis įraÅ¡ysim į vidinius klasės kintamuosius
$this->db_host=$db_host; // neapsigaukit, po -> pries db_host ženklo "$" nereikia
$this->db_user=$db_user;
$this->db_password=$db_password;
$this->db_name=$db_name;
$this->db_table=$db_table;
}
function CreateTable()
{
// Dėmesio, duomenų bazei, bei lentelei naudojami tie parametrai kuriuos perdavėte kurdami klasę, t.y. perdavėte konstruktoriui.
// jungiamės prie duomenų bazės serverio
$db=mysql_connect($this->db_host,$this->db_user,$this->db_password);
// pasirenkame duomenų bazę su kuria dirbsime
mysql_select_db($this->db_name,$db);
// o dabar sukursime pačia lentelę.
// į kintamajį $sql įraÅ¡ysime SQL sakinį kuris sukuria mūsų lentelę.
$sql="CREATE TABLE ".$this->db_table." ( id int(11) NOT NULL auto_increment, maminis_id int(11) NOT NULL default '0', atsakymo_nr int(11) NOT NULL default '0', tekstas varchar(255) NOT NULL default '', starto_data bigint(20) NOT NULL default '0', pabaigos_data bigint(20) NOT NULL default '0', balsai tinyint(4) NOT NULL default '0', PRIMARY KEY (id), KEY id_2(id) )";
// kaip matome, mes naudojame tokį lentelės vardą kokį pasiėmėme per konstruktorių.
// pats kūrimas - užklausa siunčiama į serverį
if(!mysql_query($sql,$db))
return -2; // -2 pas mus bus DB klaida
// uždarome ryÅ¡į su serveriu.
mysql_close($db);
}
function AddPoll($klausimas, $atsakymai)
{
if(empty($klausimas))
return -1; // klaida, klausimas tuÅ¡čias.
if(sizeof($atsakymai)<2) // vietoj sizeof() galima naudoti count() - laisvas pasirinkimas
return -1; // klaida, atsakymų negali būti mažiau nei 2
// jungiamės prie duomenų bazės serverio
$db=mysql_connect($this->db_host,$this->db_user,$this->db_password);
// pasirenkame duomenų bazę su kuria dirbsime
mysql_select_db($this->db_name,$db);
$atsakymu=sizeof($atsakymai); // kiek turėsime atsakymų
// paruoÅ¡iame klausimą dėjimui į duomenų bazę
$klausimas=addslashes(htmlspecialchars($klausimas));
// pasiimam dabartinį laiką nuo UNIX epochos sekundėmis
$laikas=time();
// Dabar įterpiame klausimą į lentelę
if(!mysql_query("INSERT INTO ".$this->db_table." (id, maminis_id, atsakymo_nr, tekstas, starto_data, pabaigos_data, balsai) VALUES ('','0','0','$klausimas','$laikas','0','0')",$db))
return -2; // pas mus -2 bus duomenų bazės klaida.
// taigi, klausimas įterptas, pasiimam jo id.
$klausimo_id=mysql_insert_id();
// dabar suksim ciklą kol įterpsime visus atskymus.
for($i=0;$i<sizeof($atsakymai);$i++)
{
$atsakymas=addslashes(htmlspecialchars($atsakymai[$i])); // paruoÅ¡iam atsakymo tekstą dėjimui į DB.
if(!mysql_query("INSERT INTO ".$this->db_table." (id, maminis_id, atsakymo_nr, tekstas, starto_data, pabaigos_data, balsai) VALUES ('','$klausimo_id','".($i+1)."','$atsakymas','0','0','0')",$db))
return -2; // klaida - gryžtam.
}
// uzdarome ryÅ¡į su serveriu.
mysql_close($db);
return 0; // viskas gerai, apklausa įterpta.
}
function Vote($anketos_id, $balso_nr) {
// vėl jungiamės prie serverio
$db=mysql_connect($this->db_host,$this->db_user,$this->db_password);
// pasirenkame duomenų bazę su kuria dirbsime
mysql_select_db($this->db_name,$db);
if(!$result=mysql_query("UPDATE ".$this->db_table." SET balsai=balsai+1 WHERE id='$anketos_id'",$db))
return -2; // klaida
else {
if(!$result=mysql_query("UPDATE ".$this->db_table." SET balsai=balsai+1 WHERE maminis_id='$anketos_id' AND atsakymo_nr='$balso_nr'",$db))
return -2; // klaida
else {
return 0; // viskas gerai, balsas užskaitytas
}
}
}
function GetResults() {
// vėl jungiamės prie serverio
$db=mysql_connect($this->db_host,$this->db_user,$this->db_password);
// pasirenkame duomenų bazę su kuria dirbsime
mysql_select_db($this->db_name,$db);
// dabar pasiimsime iÅ¡ duomenų bazės paskutinį užduotą klausimą
if(!$result=mysql_query("SELECT id, tekstas, starto_data, balsai FROM ".$this->db_table." WHERE maminis_id='0' ORDER BY id DESC LIMIT 1",$db))
return -2; // klaida
else {
$klausimas=mysql_fetch_row($result);
mysql_free_result($result); // atlaisvinam rezultatus.
$klausimo_id=$klausimas[0];
$grazinimui=$klausimas; // priskiriame klausimo informacija kintamajam kurį gražinsime.
$grazinimui[2]=date("Y-m-d",$grazinimui[2]); // laika sekundėmis nuo UNIX epochos paverčiame į mums suprantama datą.
$result=mysql_query("SELECT atsakymo_nr, tekstas, balsai FROM ".$this->db_table." WHERE maminis_id='$klausimo_id' ORDER BY atsakymo_nr ASC",$db);
$grazinimui[]=mysql_num_rows($result); // iÅ¡ kart po klausimo informacijos seks atsakymų skaičius, kad mes lengviau galėtume atspausdinti rezultatus.
while($arow=mysql_fetch_row($result))
{
$grazinimui[]=$arow[0];
$grazinimui[]=$arow[1];
$grazinimui[]=$arow[2];
// taip paraÅ¡yta tik tam kad jūs suprastumėte kas vyksta, o vyksta atsakymų informacijos kabinimas prie jau mūsų turimo "array" kintamojo galo
}
mysql_free_result($result); // atlaisvinam rezultatus
}
return $grazinimui; // graziname array'ju.
}
}
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects. FREE! Go There Now!
|
|
|
|
Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |