Location: PHPKode > scripts > mysurvey > mysurvey/encuesta.inc.php
<?php
/*
  ,´¯`.      Federico Larsen
 : (` ;      Linux Registered User #304032.
 `. `´       Mar del Plata. Argentina!
   `·        ICQ: 39334744
*/



class appt_encuestas {
	var $id_enc;               //identifier
	var $pregunta;             //question to vote
	var $fecha_ini;            //initial date to be avaliable
	var $fecha_fin;            //end date
	var $orden;                //order if you display more than one
	
	function appt_encuestas () {           //contructor

	}

	function get_data ($id_enc){
		$q = "select * from appt_encuestas where id_enc = $id_enc";
		$result = mysql_query ($q);
		if (mysql_num_rows ($result) == 1) {
			$row = mysql_fetch_array ($result);
			$this->id_enc = $row["id_enc"];
			$this->pregunta = $row["pregunta"];
			$this->fecha_ini = $row["fecha_ini"];
			$this->fecha_fin = $row["fecha_fin"];
			$this->orden = $row["orden"];

			return true;
		}
		else{
			return false;
		}
	}

	function update_data () {
		$q = "update appt_ecnuestas pregunta = \"$this->pregunta\" , fecha_ini = \"$this->fecha_ini\" , fecha_fin = \"$this->fecha_fin\" , orden = $this->orden where id_enc = $this->id_enc ";
		mysql_query ($q);
	}

	function insert_data () {
		$q = "insert into appt_encuestas (pregunta , fecha_ini , fecha_fin , orden ) values  (\"$this->pregunta\"  , \"$this->fecha_ini\"  , \"$this->fecha_fin\")  , $this->orden )";
		mysql_query ($q);
	}
	
    function delete_data (){
		$q="delete from appt_votos_x_encuesta where id_enc = $this->id_enc";
		mysql_query ($q);
		$q="delete from appt_opciones_x_encuesta where id_enc = $this->id_enc";
		mysql_query ($q);
		$q="delete from appt_encuestas where id_enc = $this->id_enc";
		mysql_query ($q);
	}

	function add_opcion ($opcion , $orden){
		$op = new appt_opciones_x_encuesta ();
		$op->id_enc = $this->id_enc;
		$op->opcion = $opcion;
		$op->orden = $orden;
		$op->insert_data();
	}

	function html_inicio (){        //this is the htlm display. it might be changed to your like
		echo '<form name="votacion" action ="'.$_SERVER["PHP_SELF"].'" method="post">';
		echo '<input type = "hidden" name = "id_enc" value = "'.$this->id_enc.'">';

		echo ('<table border="1">');
		echo '<tr><td bgcolor="#8BFFA9">';
		echo $this->pregunta;
		echo '</td></tr> '."\n";

	}

	function html_fin (){     //end htlm of the display
		echo '<tr><td> <input type = "submit" name = "b_votar" value = "Votar"> <input type = "submit" name = "b_reslutados" value = "Resultados">';
		echo "</table> \n";
		echo "</form>";
	}

	function mostrar (){          //show the survey
		$this->html_inicio ();

		$q = "select * from appt_opciones_x_encuesta where id_enc = $this->id_enc";
		$result = mysql_query ($q);
		while ($row = mysql_fetch_array ($result)){
			echo '<tr><td> <input type="radio" name="id_op" value="'.$row["id_op"].'"/> '.$row["opcion"].' </td></tr> '."\n";
		}


		$this->html_fin ();
	}
	
	function votar ($id_op){        //to vote
		$ip = $_SERVER["REMOTE_ADDR"];
		$q = "select count(*) as cantidad from appt_votos_x_encuesta where id_enc = $this->id_enc and fecha = \"".date("Y-m-d")."\"  and ip = \"$ip\"  ";
		$result = mysql_query ($q);
		$row = mysql_fetch_array ($result);
		if ($row["cantidad"]==0){
			$q = "insert appt_votos_x_encuesta (id_enc, id_op , fecha , ip) values ($this->id_enc ,$id_op ,\"".date("Y-m-d")."\" ,\"$ip\")";
			
			mysql_query ($q);
			return true;
		}
		else{
			return false;
		}

	}
	function html_inicio_res (){            //initial html to show the results
		echo ('<table border="1">');
		echo '<tr><td bgcolor="#8BFFA9">';
		echo $this->pregunta;
		echo '</td></tr> '."\n";

	}

	function html_fin_res (){             //end of the html to show the results
		echo "</table> \n";
	}

	function ver_resultados (){
		$this->html_inicio_res ();

		$q = "select count(*) as total from appt_votos_x_encuesta where id_enc = $this->id_enc";
		$result = mysql_query ($q);
		$row = mysql_fetch_array ($result);
		$total = $row["total"];
		
		$q = "select o.opcion , count(*) as cant from appt_opciones_x_encuesta o INNER JOIN  appt_votos_x_encuesta v on (o.id_op = v.id_op) where v.id_enc = $this->id_enc group by o.opcion";
		$result = mysql_query ($q);

		while ($row = mysql_fetch_array ($result)){
			$porc = ($row["cant"] / $total ) * 100;
			$porc = round ($porc , 2);
			echo "<tr><td>".$row["opcion"]." $porc % </td></tr>\n";
		}


		$this->html_fin_res ();
	}

	function mostrar_link () {
		echo '<tr><td>'.$this->pregunta.'</td><td> <a href = "'.$_SERVER["PHP_SELF"].'?ver_res='.$this->id_enc.'">Resultados </a> </td><td> <a href = "'.$PHP_SELF.'?votar='.$this->id_enc.'">Votar</a> </td>'."\n";
	}
}
class appt_opciones_x_encuesta { //class options per survey
	var $id_opcion; //id option
	var $id_enc;    //id suervey
	var $opcion;    //the text of the option
	var $orden;     // the order to be displayed
	
	function appt_opciones_x_encuesta (){       //constructor

	}
	function get_data ($id_op){  // get the information from de db
		$q = "select * from appt_opciones_x_encuesta where id_op = $id_op";
		$result = mysql_query ($q);
		if (mysql_num_rows ($result) == 1) {
			$row = mysql_fetch_array ($result);
			$this->id_enc = $row["id_enc"];
			$this->id_op = $row["id_op"];
			$this->opcion = $row["opcion "];
			$this->orden = $row["orden"];

			return true;
		}
		else{
			return false;
		}
	}

	function update_data () {  //update the atributes on the db
		$q = "update appt_opciones_x_ecnuesta pregunta = id_enc = $this->id_enc , opcion = \"$this->opcion\" ,orden = $this->orden where id_op = $this->id_op ";
		mysql_query ($q);
	}

	function insert_data () {  //save atributes on the db as a new record
		$q = "insert into appt_opciones_x_encuesta (id_enc, opcion , orden ) values  ($this->id_enc ,\"$this->opcion\"  ,  $this->orden )";
		mysql_query ($q);
		$this->id_op = mysql_insert_id();
	}

	function delete_data (){  //delete the option from the db
		$q="delete from appt_opciones_x_encuesta where id_op = $this->id_op";
		mysql_query ($q);
	}
}
?>
Return current item: mysurvey