Zendframe Implement a voting module

Source: Internet
Author: User
Tags filter array count implement ini mysql
Analysis: Get the User IP, determine whether the IP is disabled, and then determine if there is no vote today to make the corresponding operation ...   The main steps are as follows:   Configure Application.ini to allow items to connect to a specified database   [MySQL] Db.adapter = Pdo_mysql Db.params.host = localhost Db.params.username = root Db.params.password = db.params.dbname=votedb & nbsp Initialize the database adapter   <?php  //to do a parent class that is specifically for other controllers to inherit  class Basecontroller extends zend_controller_action{  Public Function init () { //initialization database adapter   $url = constant ("Application_path"). Directory_separator. ' Configs '. Directory_separator. ' Application.ini ';   $dbconfig = new Zend_config_ini ($url, "MySQL");   $DB = zend_db::factory ($dbconfig->db);   Zend_db_table::setdefaultadapter ($DB);  } &nbsp  //CREATE TABLE Model   <?php//This must inherit zend_db_table, otherwise it is not a table model  class Item extends zend_db_table{ Bsp Protected $_name = ' item '; &NBSP}//Voting controller   <?php require_once ' basecontroller.php '; Require_once Application_path. ' /models/item.php '; Require_once Application_path. ' /models/filter.php '; RequIre_once Application_path. ' /models/votelog.php ';     Class Votecontroller extends basecontroller{public Function voteaction () {  $item _id = $this->getre Quest ()->getparam (' itemid ', ' no ');  //get ID//For obtaining IP address $_server[' remote_addr '   $IP = $this->getrequest ()->getserver (' remote_addr ');  //See if this IP is disabled   $filterModel = new Filter ();   $filters =   $filterModel->fetchall ("ip= ' $ip")->toarray ();   if (count ($filters) >=1) {  $this->view->info= "You're being used by the police!" ";  //success, jump to a global view   $this->_forward (' Err ', ' global ');   return;  }   //First see votelog This table today is not through a $today =date (' Ymd '); today's time $voteLogModel = new Votelog (); $where = "ip= ' $ip ' and vote_date= $today"; $res = $voteLogModel->fetchall ($where)->toarray (); if (count ($res) >0) { //If greater than 0 indicates that $this->render (' ERROR ') has been cast; else{//update item vote_count, add update log $data = array (' IP ' => $ip, ' vote_date ' => $today, ' item_id ' => $iTEM_ID); if ($voteLogModel->insert ($data) >0) { //If the update succeeds, change the Item table $itemModel = new Item ()//Get the corresponding item directly through the PRIMARY key $item = $ Itemmodel->find ($item _id)->toarray ();   $newvote = $item [0][' Vote_count ']+1; $set = Array (' Vote_count ' => $newvote); $where = "id= $item _id"; $itemModel->update ($set, $where); } $this->render (' OK '); }}    ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.