Custom Search
Bloggers Activos
Emacs como IDE para CakePHP aarkerio
La Negación del Viaje Lunar tonathiu
Porque los mononeurones si tenemos madre! blacksoul
BrunoFerías thot
The Art vendaval
Aclimatación extraterrestre ¿para qué? ahuramazdah
¿A que le tienes miedo? teosho
Sobre nazis, terror y medios tonathiu
Amenazas a la cuarta dimensión ¿de veras? ahuramazdah
Tarjeta Broadcom BCM94311MCG rev 02 teosho
Last Download
Segunda Fundación
Segunda Fundación
Pidiendo OpenSolaris 2008.5
vendaval
Sospechosismo
aarkerio
Slackware 12.1 Final
vendaval
Jaime Maussan da por auténtico video trucado del chupacabras hecho en Blender 3D
asarch
Linux hot girl
aarkerio
Calderón puede ser sujeto a juicio político, sostiene Carrancá
tonathiu
La desnutrición en México
aarkerio
Sistema Infalible
ordbal
Histórico
aarkerio
Nietzsche en la FCPyS
aarkerio
Google Groups Karamelo
Visit this group
GNU/Linux
GNU/Linux
Hacktivismo
Hacktivismo
Debian
Debian
NetBSD
NetBSD
WWW
WWW
Guia Linux
Guia Linux
Server Side
Server Side
Ofimatica
Ofimatica
Despabilando...
Despabilando...
Mundo Maya
Mundo Maya
Literatura
Literatura
Agora
Agora
Psicologia
Psicologia
Economia
Economia
Ambientalismo
Ambientalismo
Desarrollo
Desarrollo
Biologia
Biologia
Tú. Conocimiento
Tú. Conocimiento

Hacktivism

LinuxChix button

WWW \ Ajax Poll CakePHP
WWW
Ajax Poll CakePHP

Este artículo ha sido consultado en 724 ocasiones.

I am implementing a polling ajax system on CakePHP, I have two Postgresql tables:

CREATE TABLE polls (
    id serial PRIMARY KEY,
    question varchar(130) NOT NULL,
    created timestamp(0) with time zone DEFAULT now() NOT NULL,
    user_id integer NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    status integer DEFAULT 0 NOT NULL   -- defines if poll is published = 1   or  not = 0
);

CREATE TABLE pollrows (
    id serial PRIMARY KEY,
    poll_id integer NOT NULL REFERENCES polls(id) ON DELETE CASCADE,
    answer varchar(130) NOT NULL,
    color varchar(15) DEFAULT 'green' NOT NULL,
    vote integer DEFAULT 0 NOT NULL
);

Note "users" table must exist, if you don't have the table just delete the column user_id in "polls" table. Poll keeps the questions and Pollrows the answers and votes. (Is easy migrate the tables to->MySQL just use "date" instead "timestamp"). So the model Poll is:

<?php
//File: /app/models/poll.php
class Poll extends AppModel
{
  public $name = 'Poll';
 
  public $hasMany = array('Pollrow' =>
                         array('className'     => 'Pollrow',
                               'conditions'        => null,
                               'order'               => 'answer',
                               'limit'                => null,
                               'foreignKey'       => 'poll_id',
                               'dependent'       => true,
                               'exclusive'         => false,
                               'finderQuery'     => ''
                         )
                  );
}
?>

The Pollrow model:

<?php
class Pollrow extends AppModel
{
  public $name      = 'Pollrow';
 
  public $belongsTo = array('Poll' =>
                             array('className'  => 'Poll',
                                     'conditions'   => '',
                                     'order'          => '',
                                     'foreignKey'  => 'poll_id'
                           )
                     );
}
?>

I inserted manually the first poll:

INSERT INTO polls ("question", "user_id", "status") VALUES ('Who is your favourite Renaissance Artist?', 1, 1);

INSERT INTO pollrows ("poll_id", "answer", "color", "vote") VALUES (1, 'Michelangelo',        'green',    0);
INSERT INTO pollrows ("poll_id", "answer", "color", "vote") VALUES (1, 'Raphael',              'orange',   0);
INSERT INTO pollrows ("poll_id", "answer", "color", "vote") VALUES (1, 'Leonardo Davinci',  'red',        0);
INSERT INTO pollrows ("poll_id", "answer", "color", "vote") VALUES (1, 'Other',                  'blue',       0);
INSERT INTO pollrows ("poll_id", "answer", "color", "vote") VALUES (1, 'None',                   'yellow',    0);
 

The colors column correspond to images in app/webroot/images/static/poll directory, every images is 10x10 pixels and .png (and only .png) format.

I am putting the poll inside the div add_pollrow in app/views/users/general.thtml (my homepage site defined in routes.php), this view correspond to /users/general (controller/action). Surely you have another controller/action as your home page, just set DIV add_pollrow in the view and load the model Poll on the controller.

 The steps:

  1. Set a form with the question and the options to answer in add_pollrow div in the view app/views/users/general.thtml
  2. Send and add the vote to Pollrows model (table) in app/controllers/pollrows.php,  method "vote()" and after this, select the new values
  3. Render app/views/pollrows/results.thml and set the new values of poll in add_pollrow

So we need to load the Poll model in app/controllers/users_controller.php:

loadModel('Poll');

And inside "general" method:

       /**********  Poll  ****/
       
        $this->Poll = new Poll;  //Instantiate the model
       
        $conditions = array("status"=>1);     // only polls published
        $fields     = array("id", "question");
        $order      = "Poll.id DESC";              // the most recent poll
        $limit      = 1;
       
        $this->set('poll', $this->Poll->findAll($conditions, $fields, $order, $limit, null, 2));

After set "poll" array in app/views/users/general.thtml, I process the array "poll" in this view (you will need a loading.gif image):

<div class="tiny_title">Well, the poll</div>

<div id="loading" style="display: none;">
       <?php echo $html->image("static/loading.gif", array("alt"=>"Loading")); ?>
</div>

<div id="add_pollrow">
  <?php

    if ( !isset( $_SESSION["poll"] )  ) // the user has not voted, so, set the form
   {
    
echo $html->formTag('#','post', array("onsubmit"=>"return false;"));    // do nothing

     $array   = array();
   
     echo "<p><b>" . $poll[0]['Poll']['question'] ."</b></p>";
   
     echo $html->hiddenTag('Pollrow/poll_id', $poll[0]['Poll']['id']);       // Poll_id
   
     foreach ($poll[0]['Pollrow'] as $key=>$val)
     {
          $array[$val['id']] = $val['answer'];                                      // construct id->value array
     }
   
    echo $html->radio('Pollrow/id', $array, '<br /><br />');              //print the answers
   
    echo '<br /><br />';
   
    echo $ajax->submit('Vote', array("url" => "/pollrows/vote",
                                     "update"=>"add_pollrow",       // add_pollrow wil be updated with app/views/pollrows/results.thtml render
                                     "loading" => "Element.hide('add_pollrow');Element.show('loading')",
                                     "complete" => "Element.hide('loading');Effect.Appear('add_pollrow')"
        ));
  echo '</form>';
  }
  else // the user has voted, so just print the poll results
  {
   
    $total_votes = 0;
   
    foreach ($poll[0]["Pollrow"] as $key => $val)
    {
        $total_votes += $val["vote"];  // the total votes
    }
   
    foreach ($poll[0]["Pollrow"] as $key => $val)
    {
             $percent = ($val["vote"] * 100) / $total_votes;  // % = votes * 100 / total
             $width   = number_format($percent, 0);
             echo '<p><b>'.$val['answer'].'</b> '.number_format($percent, 2).'% <br />';
             echo $html->image('static/poll/'.$val["color"].'.png', array("height"=>"10", "width"=>$width,"alt"=>$val["answer"], "title"=>$val["answer"]) );
             echo "  ". $val['vote'];
    }
 
  echo "<p class='negrita'>Total votes:" . $total_votes . "</p>";
}
?>
</div>

Note $ajax->submit send the vote to app/controllers/pollrows/vote:

  public function vote() {
        // adds new vote to database
     if (!empty($this->params['data']))
     { 
       $vote = $this->Pollrow->field('vote', array("Pollrow.id" => $this->params['data']['Pollrow']['id']) );
      
       $vote += 1;
      
       $this->params['data']['Pollrow']['vote'] = $vote;  // add vote
      
       //die(print_r($this->params['data']));
      
       if ( $this->Pollrow->save($this->params['data']) )   // add the Poll vote
           {      
               $_SESSION["poll"] = true; //set session, only one vote per session
              
               $conditions = array("Pollrow.poll_id" => $this->params['data']['Pollrow']['poll_id']);
               $fields     = array("Pollrow.answer", "Pollrow.color", "Pollrow.vote", "Pollrow.poll_id");
               $order      = "Pollrow.answer DESC";
              
               $this->set('poll', $this->Pollrow->findAll($conditions, $fields, $order, null, null, 2));
              
               $this->render('results', 'ajax');
            }
            else
            {
                echo "Ajax error, check with the company's computer guy...";
            }
        }
    }

 

Note, after add the vote and set 'poll' array, the view app/views/pollrows/results.thtml is rendered:

<?php
  //die(print_r( $poll ));
  $total_votes = 0;
 
  foreach ($poll as $key => $val)
  {
       $total_votes += $poll[$key]["Pollrow"]["vote"];  // the total votes
  }
 
  foreach ($poll as $key => $val)
  {
             $percent = ($poll[$key]["Pollrow"]["vote"] * 100) / $total_votes;  // % = votes * 100 / total
             $width   = number_format($percent, 0);
             echo '<p><b>' . $poll[$key]["Pollrow"]["answer"] . '</b> '.number_format($percent, 2).'% <br />';
             echo $html->image('static/poll/'.$poll[$key]["Pollrow"]["color"].'.png',
             array("height"=>"10", "width"=>$width,"alt"=>$poll[$key]["Pollrow"]["answer"], "title"=>$poll[$key]["Pollrow"]["answer"]) );
             echo "  ". $poll[$key]["Pollrow"]['vote'];
  }
 
  echo "<p>Total votes:" . $total_votes . "</p>";
?>

this view is settled inside add_pollrow div as we indicated in $ajax->submit.

And that is it!! You can see the Poll in action here.

Ficha del autor:

manuel_ARRROBA_mononeurona.org
Manuel Montoya estudió neuropsicología en la facultad y en el Instituto de Biomédicas de la UNAM. Trabajó en Compaq de México como diseñador de software, tiene diez años de experiencia en Java, PHP y SQL. Le interesan muchas cosas y neciamente le da por escribir sobre todas ellas. Actualmente trabaja en Chipotle Software, desarrollando Karamelo, una herramienta de e-Learning. Jedit.org y WindowMaker son su editor y escritorio favoritos.
aarkerio
The most effective way to restrict democracy is to transfer decision making from the public arena to unaccountable institutions. Chomsky.
Ver todos los articulos de aarkerio

Última actualización: 2007-04-29 10:57:00-05

Printable version

Add comment:



Captcha




Que estas haciendo?
teoshoteosho está:
Preparandome para el viaje a Puerto Vallarta... que triste...
2 hours, 53 minutes ago

scarecrowscarecrow está:
Du hast?
6 hours, 56 minutes ago

der_teufelder_teufel está:
Ich habe einen Kater, aber nicht so schlecht...
14 hours, 31 minutes ago

rnstuxrnstux está:
Y yo un Abrazo.
1 day, 14 hours ago

saidjosesaidjose está:
Dandole su habrazote a mi santa madre que me soporta
1 day, 16 hours ago

dsquiddsquid está:
esperando a que este el pozole
1 day, 17 hours ago

Que estuvimos haciendo >>
Chipotle Software

En tu equipo tienes:
Sólo Windows
Windows y Linux
Sólo Linux
Linux y un BSD
Solaris, linux y BSD
Sólo MacacOS
Rapiditas
Problemas de Lenguaje en niños
10410 lecturas
Sexualidad infantil y juvenil
9167 lecturas
Anticoncepción de Emergencia
7840 lecturas
Rompiendo cualquier clave WEP en unos pocos minutos
6921 lecturas
Sinapsis y exocitosis
6227 lecturas
Mi primer CakePHP, mmmmm cakeee
5264 lecturas
Evolución filética en las hepáticas
4699 lecturas
BASH y Primeros Comandos
4012 lecturas
CakePHP II Active Record
3742 lecturas
Cómo convertirse en hacker
3619 lecturas
Add to Technorati Favorites

ir arriba
Hasta donde puedo recordar, no hay una sola palabra en los Evangelios en alabanza de la inteligencia. B. Russell

The Queen is here Mozilla Firefox The Best DataBase CakePHP Framework XHTML GNU Hacker Chipotle Software

Too Cool for Internet Explorer