Miscellaneous Code
  Home arrow Miscellaneous Code arrow MySQL and PHP simple voting class
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

MySQL and PHP simple voting class
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2002-06-28

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Voting class for posting votings to your homepage, simple to use. Requires MySQL

    By : ShiVAs

    <?php
    class Voting {

    var $db_host; // duomen&#371; baz&#279;s adresas.
    var $db_user; // duomen&#371; baz&#279;s vartotojo vardas.
    var $db_password; // duomen&#371; baz&#279;s slaptažodis.
    var $db_name; // duomen&#371; baz&#279;s vardas.
    var $db_table; // duomen&#371; baz&#279;s lentel&#279;s vardas.

    // j&#363;s&#371; klas&#279;s kodas.

    function Voting($db_host,$db_user,$db_password,$db_name,$db_table)
    {
    // dabar gautus duomenis &#303;rašysim &#303; vidinius klas&#279;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&#279;mesio, duomen&#371; bazei, bei lentelei naudojami tie parametrai kuriuos perdav&#279;te kurdami klas&#281;, t.y. perdav&#279;te konstruktoriui.

    // jungiam&#279;s prie duomen&#371; baz&#279;s serverio
    $db=mysql_connect($this->db_host,$this->db_user,$this->db_password);

    // pasirenkame duomen&#371; baz&#281; su kuria dirbsime
    mysql_select_db($this->db_name,$db);

    // o dabar sukursime pa&#269;ia lentel&#281;.
    // &#303; kintamaj&#303; $sql &#303;rašysime SQL sakin&#303; kuris sukuria m&#363;s&#371; lentel&#281;.

    $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&#303; lentel&#279;s vard&#261; kok&#303; pasi&#279;m&#279;me per konstruktori&#371;.

    // pats k&#363;rimas - užklausa siun&#269;iama &#303; server&#303;

    if(!mysql_query($sql,$db))
    return -2; // -2 pas mus bus DB klaida

    // uždarome ryš&#303; su serveriu.
    mysql_close($db);
    }

    function AddPoll($klausimas, $atsakymai)
    {

    if(empty($klausimas))
    return -1; // klaida, klausimas tuš&#269;ias.

    if(sizeof($atsakymai)<2) // vietoj sizeof() galima naudoti count() - laisvas pasirinkimas
    return -1; // klaida, atsakym&#371; negali b&#363;ti mažiau nei 2

    // jungiam&#279;s prie duomen&#371; baz&#279;s serverio
    $db=mysql_connect($this->db_host,$this->db_user,$this->db_password);

    // pasirenkame duomen&#371; baz&#281; su kuria dirbsime
    mysql_select_db($this->db_name,$db);

    $atsakymu=sizeof($atsakymai); // kiek tur&#279;sime atsakym&#371;

    // paruošiame klausim&#261; d&#279;jimui &#303; duomen&#371; baz&#281;
    $klausimas=addslashes(htmlspecialchars($klausimas));

    // pasiimam dabartin&#303; laik&#261; nuo UNIX epochos sekund&#279;mis
    $laikas=time();

    // Dabar &#303;terpiame klausim&#261; &#303; lentel&#281;
    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&#371; baz&#279;s klaida.

    // taigi, klausimas &#303;terptas, pasiimam jo id.
    $klausimo_id=mysql_insert_id();

    // dabar suksim cikl&#261; kol &#303;terpsime visus atskymus.

    for($i=0;$i<sizeof($atsakymai);$i++)
    {
    $atsakymas=addslashes(htmlspecialchars($atsakymai[$i])); // paruošiam atsakymo tekst&#261; d&#279;jimui &#303; 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š&#303; su serveriu.
    mysql_close($db);

    return 0; // viskas gerai, apklausa &#303;terpta.
    }

    function Vote($anketos_id, $balso_nr) {

    // v&#279;l jungiam&#279;s prie serverio
    $db=mysql_connect($this->db_host,$this->db_user,$this->db_password);

    // pasirenkame duomen&#371; baz&#281; 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&#279;l jungiam&#279;s prie serverio
    $db=mysql_connect($this->db_host,$this->db_user,$this->db_password);

    // pasirenkame duomen&#371; baz&#281; su kuria dirbsime
    mysql_select_db($this->db_name,$db);

    // dabar pasiimsime iš duomen&#371; baz&#279;s paskutin&#303; užduot&#261; klausim&#261;

    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&#303; gražinsime.

    $grazinimui[2]=date("Y-m-d",$grazinimui[2]); // laika sekund&#279;mis nuo UNIX epochos paver&#269;iame &#303; mums suprantama dat&#261;.

    $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&#371; skai&#269;ius, kad mes lengviau gal&#279;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&#363;s suprastum&#279;te kas vyksta, o vyksta atsakym&#371; informacijos kabinimas prie jau m&#363;s&#371; 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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Accelerating Software Innovation on i on Power Systems

    Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time.
    FREE! Go There Now!


    NEW! Application Development Tools for the Mainframe Developer

    You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z: Architecture

    Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server.
    FREE! Go There Now!


    NEW! Innovate don't duplicate! Asset reuse strategies for success

    Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository.
    FREE! Go There Now!


    NEW! Rational Modeling Extension for Microsoft.Net

    Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms.
    FREE! Go There Now!


    NEW! The role of integrated requirements management in software delivery

    This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
    FREE! Go There Now!


    NEW! Webcast: Introducing the new Information Server and Solutions community: LeverageInformation

    User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges.
    FREE! Go There Now!


    NEW! Whitepaper: Delivering SOA solutions: service lifecycle management

    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!

    MISCELLANEOUS CODE ARTICLES

    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT