shahinshyd
Member
کمک در ویرایش کد فرم نظر سنجی
با سلام من یک سورس سیستم نظر سنجی دارم که میخوام کمک کنید یک ويژگی به اون اضافه کنم که هر کاربر فقط یک بار نظر بده
کد فرم نظر سنجی
کد نمایش نظر سنجی
صفحه functions.php
تابعی برای اتصال با DB (دیتابیس)
کد:
تابعی برای درج مقادیری در تیبل best_Candid
این تابع یک رأی به هر کاندیدی که انتخاب شده اضافه میکنه.
کد:
صفحه polls_motor.php
نمایش آمار :
صفحه poll.html
کد:
با سلام من یک سورس سیستم نظر سنجی دارم که میخوام کمک کنید یک ويژگی به اون اضافه کنم که هر کاربر فقط یک بار نظر بده
کد فرم نظر سنجی
HTML:
<!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>نظرسنجی</title>
</head>
<body>
<form id="polls" name="polls" method="post" action="polls_motor.php">
<table width="159" border="1" dir="rtl">
<caption><strong>به کی رأی میدی ؟</strong></caption>
<tr>
<td width="20" align="center"><strong>1</strong></td>
<td width="65">سامان</td>
<td width="52"><label>
<input type="radio" name="poll" id="radio" value="سامان" />
</label></td>
</tr>
<tr>
<td align="center"><strong>2</strong></td>
<td>علی</td>
<td><label>
<input type="radio" name="poll" id="radio2" value="علی" />
</label></td>
</tr>
<tr>
<td align="center"><strong>3</strong></td>
<td>مهدی</td>
<td><label>
<input type="radio" name="poll" id="radio3" value="مهدی" />
</label></td>
</tr>
<tr>
<td colspan="2" align="center">ارسال رأی</td>
<td><label>
<input type="submit" name="button" id="button" value="Submit" />
</label></td>
</tr>
</table>
</form>
</body>
</html>
صفحه functions.php
تابعی برای اتصال با DB (دیتابیس)
کد:
PHP:
<?php
function DB_Connect()
{
$hostname = "localhost";
$username = "root";
$password = "";
$DB_name = "polls";
$link = mysql_connect("$hostname","$username","$password")or die("Cannot Connect To Database");
$db = mysql_select_db("$DB_name")or die("Cannot Select Database");
//برای اینکه اطلاعاتی که از دیتابیس میخونیم به صورت فارسی نمایش داده بشه از کد زیر استفاده میکنیم.
mysql_query("SET NAMES 'utf8'", $link);
mysql_query("SET character_set_connection = 'utf8'", $link);
return $link ;
}
?>
این تابع یک رأی به هر کاندیدی که انتخاب شده اضافه میکنه.
کد:
PHP:
<?php
function add_polls($Candid)
{
DB_Connect(); // برقراری ارتباط با دیتابیس
//اول آراء داده شده را بدست می آوریم سپس یک رأی به آراء قبلی می افزاییم
$query = "select poll from best_candid WHERE name = '$Candid'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result) ;
$poll = $row ['poll'] ; // بدست آوردن آراء کاندیدمان
$poll ++ ; // اضافه کردن یک رأی به آراء قبلی
// ثبت آراء در دیتابیس
$query = "UPDATE best_candid set poll = '$poll' WHERE name = '$Candid'" ;
$result = mysql_query($query);
// اگه تابع درست کار کنه مقدار ترو (درست) رو بر میگردونه و در غیر این صورت فالس (نادرست)
if (!$result)
return false;
return true ;
}
?>
PHP:
کد:<?php
include("functions.php");
// اگه کسی رأی بده اسکریپت اجرا میشه در و غیر اینصورت دوباره بر میگرده به همون صفحه قبل
if (isset ($_POST['poll']) or strlen($_POST['poll']) !== 0)
$candid_name = $_POST['poll'];
else
{
$next_page = "form.html";
header("Location: " . $next_page);
exit ;
}
// ثبت رأی داده شده
$polls_result = add_polls($candid_name) ;
if (!$polls_result)
{
echo "متاسفانه مشکلی پیش آمده ، لطفا بعدا سعی کنید .";
exit ;
}
//نمایش آمار و ساخت نمودار
DB_Connect(); // برقراری ارتباط با دیتابیس
$query = "select * from best_candid";
$result = mysql_query($query);
$num_candidates = mysql_num_rows($result);
// calculate total number of votes so far
$total_votes=0;
while ($row = mysql_fetch_array($result))
{
$total_votes += $row ['poll'];
}
@mysql_data_seek($result, 0); // reset result pointer
// دستورات ساخت تصویر نمودار
$width=400; // width of image in pixels
$left_margin = -30;
$right_margin= 80;
$bar_height = 15;
$bar_spacing = 10;
$font = 'arial.ttf'; // فونت مورد استفاده در نظرسنجی
$title_size= 14;
$main_size= 9;
$darsad_size= 8;
$small_size= 9;
$text_indent = 10;
$x = $left_margin + 60;
$y = 50;
$bar_unit = ($width-($x+$right_margin)) / 100;
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;
$im = ImageCreateTrueColor($width,$height);
// رنگ های مورد استفاده در تصویر
$white=ImageColorAllocate($im,255,255,255);
$blue=ImageColorAllocate($im,150,150,150);
$black=ImageColorAllocate($im,0,0,0);
$cry=ImageColorAllocate($im,200,200,200);
$pink = ImageColorAllocate($im,0,128,128);
$yellow = ImageColorAllocate($im,244,0,0);
$text_color = $black;
$percent_color = $white;
$percent_color2 = $black;
$bg_color = $white;
$line_color = $cry;
$bar_color = $blue;
$number_color = $pink;
// Create "canvas" to draw on
ImageFilledRectangle($im,0,0,$width,$height,$bg_color);
// Draw outline around canvas
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);
// Add title
$title = 'Poll Results';
$title_dimensions = ImageTTFBBox($title_size, 0, $font, $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x = ($width-$title_length)/2; // center it in x
$title_y = ($y - $title_height)/2 + $title_above_line; // center in y gap
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, $font, $title);
ImageLine($im, $x, $y-2, $x, $height-15, $line_color);
$si = 1 ;
while ($row = mysql_fetch_array ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row ['poll'] / $total_votes )*100));
else
$percent = 0;
$percent_dimensions = ImageTTFBBox($main_size, 0, $font, $percent. ' % ');
$percent_length = $percent_dimensions[2] - $percent_dimensions[0];
if ($total_votes > 0)
$right_value = intval(round(($row ['poll']/$total_votes)*100));
else
$right_value = 0;
$bar_length = $x + ($right_value * $bar_unit);
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/1.5),
$text_color, $font, $si);
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)+20, $y+($bar_height/1.5),
$number_color, $font, $row ['poll'] .' - '.$total_votes);
//*********************************************
if ($percent <= 93)
{
ImageTTFText($im, $darsad_size, 0, $width-$percent_length-80,
$y+($bar_height/1.5), $percent_color2, $font, $percent .' % ');
}
if ($percent >= 90 )
{
ImageTTFText($im, $darsad_size, 0, $width-$percent_length-80,
$y+($bar_height/1.5), $percent_color, $font, $percent.' % ');
}
$y=$y+($bar_height+$bar_spacing);
$si++ ;
}
/*******************************************
Display image
*******************************************/
Imagejpeg($im,'poll.jpg',100);
/*******************************************
Clean up
*******************************************/
ImageDestroy($im);
//بعد از اتمام ساخت تصویر به صفحه دیگه منتقل میشید که نتایج رو مشاهده کنید .
$next_page = "poll.html";
header("Location: " . $next_page);
?>
صفحه poll.html
کد:
کد:
<!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>نظرسنجی</title>
</head>
<body>
<div align="center">
<p><img src="poll.jpg" alt="نظرسنجی" border="0">
</p>
<p><a href="form.html">Back</a></p>
</div>
</body>
</html>
آخرین ویرایش: