Anketler. Evet anketler. Sitenizdeki ziyaretçilerin veya kullanıcıların fikirlerini, düşüncelerini sanırım bilmek istiyorsunuz. Bunun için de size bir anket gerek. Hadi gelin beraber PHP ile anket uygulaması yapalım. Bunu istediğiniz gibi geliştirebilir, CSS kodlarıyla oynayabilirsiniz. Başlıyoruz.

1. Adım - Anketimize Arkaplan Resmi Hazırlayalım

Yukarıdaki gibi bir arkaplan resmi çiziyoruz. İsmini “arkaplan.png” olarak ayarlıyoruz ve “images” adında bir klasör oluşturarak içine taşıyoruz. (Dosyalarda PSD’si var kendinize göre düzenleyebilirsiniz.)

2. Adım - Sayfa İnşaası

HTML kodlarımızı yazmaya başlayalım.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Anket</title>

<link href="style.css" rel="stylesheet" type="text/css" />

</head>

<body>

<fieldset>

 <legend>Hangi javascript kütüphanesini tercih ediyorsunuz ?</legend>

 <form action="<?php echo $editFormAction; ?>" id="form1" name="form1" method="POST">

 	<label>

 		<input type="radio" name="Poll" value="mootools" id="Poll_0" />

 		Mootools

 	 </label>

 	<label>

 		<input type="radio" name="Poll" value="prototype" id="Poll_1" />

 		Prototype

 	</label>

 	<label>

 		<input type="radio" name="Poll" value="jquery" id="Poll_2" />

 		jQuery

 	</label>

 	<label>

 		<input type="radio" name="Poll" value="spry" id="Poll_3" />

 		Spry

 	</label>

 	<label>

 		<input type="radio" name="Poll" value="other" id="Poll_4" />

 		Diğer

 	</label>

 	<input type="submit" name="submit" id="submit" value="Oyla" />

 	<input type="hidden" name="id" value="form1" />

 	<input type="hidden" name="MM_insert" value="form1" />

 </form>

</fieldset>

</body>

</html>

Yukarıda form uygulamamızı HTML formatında kodladık. Sorumuzu yazdık. Seçeneklerimizi yazdık. Oyla butonumuzu koyduk. Bu yazdığımız kodları “poll.php” olarak kayıt edelim. Şimdi ise görünümü ayarlayalım.

3. Adım - CSS 

Aşağıdaki kodları “style.css” olarak kayıt edelim.

* {

 margin: 0;

 padding: 0;

}

body {

 font-size: 62.5%;

 font-family: Georgia, serif;

 background: url(resimler/arkaplan.png) top center no-repeat #233743;

}

h6 {

 font-size: 1.4em;

 margin-bottom: 15px;

}

a { color: white; }

label, li {

 display: block;

 padding: 5px;

 font-size: 1.4em;

 color: #e3f1fa;

}

fieldset {

 margin: 115px auto;

 width: 400px;

 padding: 8px 15px 15px 15px;

 border: 1px solid white;

 display: block; /* IE 7 Requires This */

 }

 legend {

 	padding: 4px 6px 4px 6px;

 	border: 1px solid white;

 	font-size: 2.0em;

 	color: #e3f1fa;

 	font-style: italic;

 }

ul { list-style: none; margin-bottom: 15px;}

.results-bar {

 padding: 10px;

 color: white;

 background: url(resimler/sonuc.png) left center;

 white-space: nowrap;

}

span.total-votes {

 font-size: 2.6ejm;

4. Adım - Sonuçlar İçin Veritabanı Oluşturalım

Anket sonuçlarını görebilmek ve o sonuçların bir yerde kayıtlı olup bir daha oylama yapıldığı zaman görülebilmesi için bir veritabanına ihtiyacımız var. Dolayısıyla bir veritabanı oluşturuyoruz. Scriptimizin veritabanına bağlanması için yeni bir dosya açarak aşağıdaki kodlarla MySQL bağlantımızı oluşturalım.

<?php

# FileName="Connection_php_mysql.htm"

# Type="MYSQL"

# HTTP="true"

$hostname_conn_vote = "localhost";

$database_conn_vote = "veritabanı-adı";

$username_conn_vote = "veritabanı-kullanıcı-adı";

$password_conn_vote = "veritabanı-şifresi";

//$conn_vote = mysql_pconnect($hostname_conn_vote, $username_conn_vote, $password_conn_vote) or trigger_error(mysql_error(),E_USER_ERROR);

$conn_vote = mysql_connect($hostname_conn_vote, $username_conn_vote, $password_conn_vote) or die('Can\'t create connection: '.mysql_error());

mysql_select_db($database_conn_vote, $conn_vote) or die('Can\'t access specified db: '.mysql_error());

?>

Oluşturduğumuz dosyanın ismini “conn_vote.php” olarak kayıt edelim ve ana dizinde “Connections” adlı bir klasör açarak içine taşıyalım.

Oluşturduğumuz veritabanına girerek (PHPMyAdmin) Aşağıdaki kodu sorgulayalım.

--

-- Table structure for table `poll`

--

CREATE TABLE `poll` (

  `id` int(3) NOT NULL auto_increment,

  `question` varchar(200) default NULL,

  PRIMARY KEY  (`id`)

) TYPE=MyISAM AUTO_INCREMENT=43 ;

--

-- Dumping fake data for table `poll`

--

INSERT INTO `poll` VALUES (42, 'jquery');

INSERT INTO `poll` VALUES (41, 'mootools');

INSERT INTO `poll` VALUES (40, 'other');

INSERT INTO `poll` VALUES (39, 'mootools');

INSERT INTO `poll` VALUES (38, 'jquery');

INSERT INTO `poll` VALUES (37, 'mootools');

INSERT INTO `poll` VALUES (36, 'spry');

INSERT INTO `poll` VALUES (35, 'jquery');

INSERT INTO `poll` VALUES (21, 'mootools');

INSERT INTO `poll` VALUES (22, 'other');

INSERT INTO `poll` VALUES (23, 'mootools');

INSERT INTO `poll` VALUES (24, 'mootools');

INSERT INTO `poll` VALUES (25, 'prototype');

INSERT INTO `poll` VALUES (26, 'other');

INSERT INTO `poll` VALUES (27, 'mootools');

INSERT INTO `poll` VALUES (28, 'spry');

INSERT INTO `poll` VALUES (29, 'jquery');

INSERT INTO `poll` VALUES (30, 'mootools');

INSERT INTO `poll` VALUES (31, 'prototype');

INSERT INTO `poll` VALUES (32, 'mootools');

INSERT INTO `poll` VALUES (33, 'mootools');

INSERT INTO `poll` VALUES (34, 'mootools');

 5. Adım - PHP Kodlaması

Aşağıda yazdığımız kodları poll.php’ye girerek HTML kodlarının üstüne ekliyoruz.

<?php require_once('Connections/conn_vote.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO poll (id, question) VALUES (%s, %s)",

                       GetSQLValueString($_POST['id'], "int"),

                       GetSQLValueString($_POST['Poll'], "text"));

mysql_select_db($database_conn_vote, $conn_vote);

  $Result1 = mysql_query($insertSQL, $conn_vote) or die(mysql_error());

$insertGoTo = "results.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

$colname_rs_vote = "-1";

if (isset($_GET['recordID'])) {

  $colname_rs_vote = $_GET['recordID'];

}

mysql_select_db($database_conn_vote, $conn_vote);

$query_rs_vote = sprintf("SELECT * FROM poll WHERE id = %s", GetSQLValueString($colname_rs_vote, "int"));

$rs_vote = mysql_query($query_rs_vote, $conn_vote) or die(mysql_error());

$row_rs_vote = mysql_fetch_assoc($rs_vote);

$totalRows_rs_vote = mysql_num_rows($rs_vote);

?>

Aşağıdaki kodu ise dosyamızın en altına ekliyoruz.

<?php

mysql_free_result($rs_vote);

?>

6. Adım - Sonuçlar Sayfası

Anketimizde oyların yüzdelerini görmek için bir sonuç sayfasına ihtiyacımız olacak. Aşağıdaki kodlarımızı yazarak “results.php” olarak kayıt ediyoruz.

<?php require_once('Connections/conn_vote.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

mysql_select_db($database_conn_vote, $conn_vote);

$query_rs_vote = "SELECT * FROM poll";

$rs_vote = mysql_query($query_rs_vote, $conn_vote) or die(mysql_error());

$row_rs_vote = mysql_fetch_assoc($rs_vote);

$totalRows_rs_vote = mysql_num_rows($rs_vote);

$resultQuestion1 = mysql_query("SELECT * FROM poll WHERE question='mootools'");

$num_rowsQuestion1 = mysql_num_rows($resultQuestion1);

$resultQuestion2 = mysql_query("SELECT * FROM poll WHERE question='prototype'");

$num_rowsQuestion2 = mysql_num_rows($resultQuestion2);

$resultQuestion3 = mysql_query("SELECT * FROM poll WHERE question='jquery'");

$num_rowsQuestion3 = mysql_num_rows($resultQuestion3);

$resultQuestion4 = mysql_query("SELECT * FROM poll WHERE question='spry'");

$num_rowsQuestion4 = mysql_num_rows($resultQuestion4);

$resultQuestion5 = mysql_query("SELECT * FROM poll WHERE question='other'");

$num_rowsQuestion5 = mysql_num_rows($resultQuestion5);

$percentQuestion1 = ($num_rowsQuestion1 / $totalRows_rs_vote)*100;

$percentQuestion2 = ($num_rowsQuestion2 / $totalRows_rs_vote)*100;

$percentQuestion3 = ($num_rowsQuestion3 / $totalRows_rs_vote)*100;

$percentQuestion4 = ($num_rowsQuestion4 / $totalRows_rs_vote)*100;

$percentQuestion5 = ($num_rowsQuestion5 / $totalRows_rs_vote)*100;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

 <title>Sonuçlar</title>

 <link href="style.css" rel="stylesheet" type="text/css" />

</head>

<body>

 <fieldset>

<legend>Sonuçlar</legend>

<ul>

 		<li>

 			<span class="total-votes"><?php echo $num_rowsQuestion1 ?></span> Mootools

 			<br />

 			<div class="results-bar" style="width: <?php echo round($percentQuestion1,2); ?>%;">

 				 <?php echo round($percentQuestion1,2); ?>%

 			</div>

 		</li>

<li>

 			<span class="total-votes"><?php echo $num_rowsQuestion2 ?></span> Prototype

 			<div class="results-bar" style="width: <?php echo round($percentQuestion2,2); ?>%;">

 				 <?php echo round($percentQuestion2,2); ?>%

 			</div>

 		</li>

<li>

 			<span class="total-votes"><?php echo $num_rowsQuestion3 ?></span> jQuery

 			<div class="results-bar" style="width: <?php echo round($percentQuestion3,2); ?>%;">

 				 <?php echo round($percentQuestion3,2); ?>%

 			</div>

 		</li>

<li>

 			<span class="total-votes"><?php echo $num_rowsQuestion4 ?></span> Spry

 			<div class="results-bar" style="width: <?php echo round($percentQuestion4,2); ?>%;">

 				 <?php echo round($percentQuestion4,2); ?>%

 			</div>

 		</li>

<li>

 			<span class="total-votes"><?php echo $num_rowsQuestion5 ?></span> Diğer

 			<div class="results-bar" style="width: <?php echo round($percentQuestion5,2); ?>%;">

 				 <?php echo round($percentQuestion5,2); ?>%

 			</div>

 		</li>

 	</ul>

<h6>Toplam Oy Sayısı: <?php echo $totalRows_rs_vote ?></h6>

<a href="poll.php">Oylamaya Geri Dön</a>

</fieldset>

</body>

</html>

<?php

mysql_free_result($rs_vote);

?>

Bitiş

Demoyu Görüntüle

Dosyaları İndir

Dosya Şifresi : www.baybars.net

Kaynak ve Orjinal Metin : CSS - Tricks


Bir Yorum - “PHP ile Dinamik Anket Uygulaması Yapımı”  

  1. 1 Adderall.


Yorum Gönder