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!
|
|
|
|
Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
|
|
|
|
Portfolio Management is about effectively managing portfolio value by aligning portfolio investments with business goals. This complimentary e-kit provides a collection of materials that can help you understand how IBM Rational enables and automates best practices for improved governance and clear visibility into portfolio and project performance across the entire IT project lifecycle. FREE! Go There Now!
|
|
|
|
Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
|
|
|
|
Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
|
|
|
|
Viper 2 brings a great value to developer communities including SQL, XML, PHP, Ruby, .NET and Java. You probably already know that DB2 Express-C is free for developers to develop, deploy and distribute. Viper 2 provides a variety of means that help move your application from the development stage to deployment more rapidly. This webcast shows how to best utilize the latest tools available for developing DB2 applications. FREE! Go There Now!
|
|
|
|
The unprecedented scope of a service-oriented architecture (SOA) initiative brings to the forefront a number of management and governance issues that were sidestepped in the past. The key to a successful SOA implementation is managing and governing activities throughout the entire SOA delivery lifecycle by ensuring that services conform to the needs of all of the business’s stakeholders. Learn how service lifecycle management allows the business to ensure that the process by which services are defined, created, tested, deployed, optimized and retired is manageable, repeatable and auditable. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |