Go Back   Computer Forums > Web Design | Website Development > Web Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 11-07-2005, 10:10 AM   #1
BSOD
 
Join Date: May 2005
Posts: 2,734
Default Poll?

Alright, I don't visit this particular forum much here on CF, but now that I'm building a site, now's probably a good time to start. Anyway, my abilities with languages like HTML and PHP are sketchy at best. Dreamweaver makes it pretty easy, but I've found that to do anything more advanced than some text and images, you still need to know a bit of code.

So for right now, I need to know how to make a basic poll. I'm happy with using form objects, but if you have something better, feel free to tell me. I'm guessing I either have to use a data file or a database to keep track of votes. If a database is needed, I'd need that explained to me as well since I've had little experience with them. (Referring me to another thread for that would be fine if a good one already exists.) Also, I would like to limit users to one vote by IP address. Like I said, I'm not good at web languages, so please explain any code. Also, I know I should have searched for this topic on the forum, but I wanted to get some personalized responses. Thanks guys!

P.S. If you guys know any super good web design tutorial sites, please let me know! (I don't want anything like "Stacy's Killer Website Page" that shows you how to make hearts float down the screen or any crap like that. I want serious stuff.)
__________________

catalyst is offline   Reply With Quote
Old 11-07-2005, 11:20 AM   #2
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default Re: Poll?

I suppose that making polls can be divided into two sections,

the first ismost likely the proper way to do it,

You'll need PHP, and a database, (html forms obviously), and it's nice to have a host that supports the gd toolsso that you can draw bar charts...

however, if you only want to store and return numbers, then it's not important to have a database, or graphical imaging tools on the server.

are you looking to create a poll like can be found on these forums? where a member of the public can create a poll with any amounts of options and have nice bar charts drawn, or are you thinking that you want to ba able to create the polls yourself in a messy backend way?

I can post the code for a pretty neat poll application that I made for LKs dotOmega forums software. (which would get the job done).
or a link to some existing polling software...

perhaps more usefull I could set about writting a tutorial in parts about how to create a polling application using PHP, that would give you the poll scripts you want and perhaps teach something about PHP...

so,
Do you want to use a database, or something that doesn't use a database?
Do you want a nice graphical way to create polls?
Do you want graphical polls (bar charts) or text only polls?
Do you want a tutorial on how to create it, or do you want the easy way out and just the final code?

Answer those four questions and I'll get something for you.
(If I make a tutorial perhaps Steven/webmonkey would like to put it on his tutorial site?)
__________________

__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-07-2005, 02:59 PM   #3
BSOD
 
Join Date: May 2005
Posts: 2,734
Default Re: Poll?

A database poll would be fine, but like I said, I don't really know how to work with them, so I'd have to learn that first. If you have a nice non-database poll, that would be fine, too. And I don't want others to create polls like in this forum. I just want to make one myself to put on my website. One with bars would be nice, and I would like to learn how to make it myself. I need to learn more HTML and PHP and whatnot anyway. Oh yeah, for the database, would it just be MySQL? Hmmm, I really need to learn more about this web stuff.
catalyst is offline   Reply With Quote
Old 11-07-2005, 03:13 PM   #4
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default Re: Poll?

Yes,
I'll start to write a tutoral at work tomorow...

if anyone else wants to do this as well, You'll need...

a server using PHP,
a database server using MYSQL,
the GD modules for PHP, (you can check to see if you have these by creating this page)

Code:
<?php
phpinfo();
?>
or if you have console acces to yuor server by typing php -m to list the loaded modules.

you'l also need teh patience to follow these tutorials,
which will cover creating the database connection, creating the needed tables, constructing the forms, database design, draing pictures using php and creating rows in databases and updating data in databases.


Starting tomorrow, because I've just come back from the gym and I'm too knackered to do it tonight.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-07-2005, 10:10 PM   #5
BSOD
 
Join Date: May 2005
Posts: 2,734
Default Re: Poll?

Works like a charm. My server has version 5.0.4 in case you were curious/needed to know. Anyway, I really appreciate the help! I look forward to the tutorials.
catalyst is offline   Reply With Quote
Old 11-08-2005, 05:58 AM   #6
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default Re: Poll?

OK... I'll actually try to get al the code up, and explenations/tutorials today, rather than having this drag on, but if it doesn't all get posted today, I'm sure you'll all appreciate that I'm paid to work whilst I'm at work, not paid to post on forums!!

Tutorials on how to make Polling applications in PHP, using MySQL,

1 - Creating a poll,
For this, I've decided that although you didn't want users to be able to create polls, I'll make a nice page so that the users can create polls, this might be helpful to someone else, and will certainly be helpful to you when creating polls.

Remember this is all from an already made poll application.

firstly, All polls wil be created from a nice graphical user interface.


The most important bit is (IMHO) that the polls can be extensible, it's all very well creating a polling aplication that enables you to ask yes no questions, but what about questions that might require a little more than a yes / no question?

well this form addresses that by allowing the user to add as many boxes as they like.

It all revolves around a neat little Jscript app, all the javascript applet does is write new datato the HTML page, the key thing here is that it all updates without having to refresh the page since it's done in Jscript, (notice that I've written Jscript, not javascript, as this is IE only code for posting polls, however, if you're a firefox user, you can be fairly assured that it's OK to use IE on your own site where you know that there is no spyware/activex downloaders.

Anyway

heres the code.
You can see that when the user clicks on a button it invokes the jscript function addrow, and passed the element id 'mytable', this causes the jscript function to add exrta information to the element, in this case an extra row. the function also increments a value called poll_option_count, this is used to keep a track of how many options have been added to the poll.

the poll question has a maximim length of 255 charectors for reasons that will be obvious later, (it's to do with the database).

lastly there is a submit button to submit the poll to a php page that will recieve and interperaqte all the data.
Code:
<html>
<head>
	<script language="JavaScript" type="text/javascript">
	<!--
	var inputCount = 1
	function addRow(tablename,field,field_name) {
		 inputCount++;
		 tObj = document.all[tablename];
		 rsObj = tObj.insertRow();
		 fieldObj = rsObj.insertCell();
		 fieldObj.innerHTML = "<input type=text name='items_"+inputCount+"' />"
document.poll.poll_option_count.value=inputCount;
	}
	//-->
	</SCRIPT>
</head>
 
<body>
 
<form method="POST" action="post.php" name="poll">
<input type="hidden" name="poll_option_count" value="1" />
<input type=button onClick="addRow('myTable')" value="add a poll option"> 
<table border="0" cellspacing="3" cellpadding="1" id="myTable">
	<tr>
		 <th>
			 Question: <input name="poll_question" maxlength="255" /><br>
		 </th>
 
	</tr>
	<tr>
		 <td>
			 <hr>
		 </td>
	</tr>
	<tr>
		 <td>
			 <input name='items_1' />
</td>
	</tr>
 
</table>
<input type="submit" value="Submit Poll" />
</form>
 
</body>
</html>
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-08-2005, 06:23 AM   #7
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default Re: Poll?

Part 2 - database design,

When this poll was made it was designed to go into a forum system as an addin, so I didn't want loads of tables cloging up the database, as I'm sure you probably don't want loads of tables clogging up your database...

therefore the entire poll application uses only one table in the database, we'll call this table 'polls',

the table polls has five columns,
poll_id, (int, not null)
this field is used to identify the poll in question, this field basically assigns a number to a poll, polls are identified with numbers, that way the same question can be asked twice with different results, perhaps a rate this article poll or something, you can imagine the same poll might be used plenty of times over and over and over, poll cannot be identified by question alone.

poll_title, (varchar255, not null)
the poll title field contains the question that is asked in the poll, it's a varchar with length 255, you'll see that this is why the question was limited to 255 chars in the poll submission form. -you could remove the limit by making this a text field, (or a memo field if you're converting to MSSQL).

option_id, (autoincrement, not null)
This is the primary key of the table and is used to identify the options,

option_title, (varchar255, null)
this is where the options are named, this field is allowed to be null, since the poll title is stored in the same table as the poll options, (obviously the poll title doesn't have an option so we allow nulls here)

count (int, null)
this field is where the results of the poll are stored.

and here is the SQL code used to create that table,
Code:
CREATE TABLE `polls` (
  `poll_id` int(4) NOT NULL default '0',
  `poll_title` varchar(255) NOT NULL default '',
  `option_id` int(4) NOT NULL auto_increment,
  `option_title` varchar(255) default NULL,
  `count` int(4) default NULL,
  PRIMARY KEY  (`option_id`)
);
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-08-2005, 06:23 AM   #8
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default Re: Poll?

Part 3 - Database connections.

In this part of the tutorial I'll start with how to set up a connection from inside of PHP to a MySQL server.

Server connections are made with the function mysql_connect() after you have connected to the server you need to select the database you wish to use. this is done with the mysql_select_db() function.

For this, I'm going to assume some things, you can replace the assumptions with your actual parameters,

We'll put all the database connectivity stuff in a file called connect.php, this is useful since it can be called from other files without you having to re-write lots of parameters, also if you change your server/database/username/password, you only have to update one file rather than all files.


connect.php
Code:
<?php

// Database connection parameters you can change the parts inside of the quotes.
$username = "USERNAME";
$password = "PASSWORD";
$server = "SERVER";
$database = "DATABASE";

// do not edit after this point.
$link = mysql_connect($server, $username, $password)
   or die('Could not connect to server : ' . mysql_error());
mysql_select_db($database) or die('Could not select the database : $database');

?>
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-08-2005, 06:42 AM   #9
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default Re: Poll?

Part 4 - Saving new polls,

This pretty much picks up from where the first part left, the user has filled in the create poll HTML form, added a few poll options and pressed submit, now the poll (and an unknown amount of poll options, needs to be saved.

Because the amount of poll options is unknown (could be 2 options could be 10 options) there was a field on the first HTML form called poll_option_count, this has recorded how many poll options are included,

Firstly, we start the PHP by connecting to the database:

Code:
<?php
 
include('connect.php');
then we find out how many polls have been posted

Code:
$count = $_POST['poll_option_count'];
We check this to make sure that there is more than one option, (by definition a poll is not a poll if there is only one option.
Code:
if ($count!= "1")
{
Then we get the poll title data,
Code:
	//get poll title
	$poll_name = $_POST['poll_question'];
then we go through the options getting the poll options data, these are stored in an array called pollopt (for poll options)
Code:
//get all poll options
	for ($x=1;$x<=$count;$x++)
	{
		$pollopt[$x] = $_POST['items_' . $x];
	}
you see how the loop scroll sthrough the amount of poll options getting the options from the posted HTML data.

after this we need to find out what the last poll id was, and then add one to it to make the next poll id,

Code:
	$query = "select poll_id from polls order by poll_id asc";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error() . '<hr>%%%There has been an error in the application, please contact the administrator');
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$poll_id = $line['poll_id'];
	}
	//generate new poll id
	$poll_id++;
now we need to store the new poll title in a row of it's own...
Code:
/add new poll ID (and other poll details) to the table
	$query = "INSERT INTO `polls` ( `poll_id` , `poll_title` , `option_id` , `option_title` , `count` , `expires` ) VALUES ('$poll_id', '$poll_name', '', NULL , NULL , NULL );";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error() . '<hr>T£££here has been an error in the application, please contact the administrator');
	print "$poll_name<br><hr>";
the scroll through the array of poll options comiting each on of those to the database as well...
Code:
for ($x=1;$x<=$count;$x++)
{
	 //add option details to database
		print "$pollopt[$x]<br>";
		$query = "INSERT INTO `polls` ( `poll_id` , `poll_title` , `option_id` , `option_title` , `count` , `expires` ) VALUES ('$poll_id', '$poll_name', '', '$pollopt[$x]' , '0' , NULL );";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error() . '<hr>!!There has been an error in the application, please contact the administrator');
		$query = NULL;
}
now, we'll add some links so that you can see the polls, (or at least see how the polls are linked to, (using the poll_id (!!))
Code:
print "<a href=\"text_poll_result.php?pid=$poll_id\">View your poll in text</a><br>";
print "<a href=\"bar_poll_result.php?pid=$poll_id\">View your poll in graph</a><br>";
print "<hr>";
print "<a href=\"poll_vote.php?pid=$poll_id\">Vote in your poll</a><br>";
Lastly, remember how we opened an if statement to check the amount of option the poll creator had posted.

well a simple rule, if you open it you have to close it,
we'll also add an else statement to make sure they are not just presented with a blank screen if they have only one poll option.
Code:
}
else
{
	print "Your Poll did not contain enough options to be vaild";
}
 
?>
And that is how the polls are stored...

the complete code is shown in the next post.
__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Old 11-08-2005, 06:42 AM   #10
Site Team
 
root's Avatar
 
Join Date: Mar 2004
Posts: 8,007
Default Re: Poll?

Code:
<?php
 
include('connect.php');
//get amount of options posted
$count = $_POST['poll_option_count'];
print "$count";
//check to make sure that more than one poll option is available
if ($count!= "1")
{
	//get poll title
	$poll_name = $_POST['poll_question'];
	//get all poll options
	for ($x=1;$x<=$count;$x++)
	{
		$pollopt[$x] = $_POST['items_' . $x];
	}
	$query = "select poll_id from polls order by poll_id asc";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error() . '<hr>%%%There has been an error in the application, please contact the administrator');
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$poll_id = $line['poll_id'];
	}
	//generate new poll id
	$poll_id++;
 
	//add new poll ID (and other poll details) to the table
	$query = "INSERT INTO `polls` ( `poll_id` , `poll_title` , `option_id` , `option_title` , `count` , `expires` ) VALUES ('$poll_id', '$poll_name', '', NULL , NULL , NULL );";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error() . '<hr>T£££here has been an error in the application, please contact the administrator');
	print "$poll_name<br><hr>";
 
for ($x=1;$x<=$count;$x++)
{
	 //add option details to database
		print "$pollopt[$x]<br>";
		$query = "INSERT INTO `polls` ( `poll_id` , `poll_title` , `option_id` , `option_title` , `count` , `expires` ) VALUES ('$poll_id', '$poll_name', '', '$pollopt[$x]' , '0' , NULL );";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error() . '<hr>!!There has been an error in the application, please contact the administrator');
		$query = NULL;
}
print "<a href=\"text_poll_result.php?pid=$poll_id\">View your poll in text</a><br>";
print "<a href=\"bar_poll_result.php?pid=$poll_id\">View your poll in graph</a><br>";
print "<hr>";
print "<a href=\"poll_vote.php?pid=$poll_id\">Vote in your poll</a><br>";
 
}
else
{
	print "Your Poll did not contain enough options to be vaild";
}
 
?>
__________________

__________________
I didn’t fight my way to the top of the food chain to be a vegetarian…
Im sick of people saying 'dont waste paper'. If trees wanted to live, they'd all carry guns.
"The inherent vice of capitalism is the unequal sharing of blessings; The inherent vice of socialism is the equal sharing of miseries."
root is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 06:49 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0