PDA

View Full Version : Creating a Poll


Mark_W
06-22-2007, 04:04 PM
Hello again, exams are over so I'm back to doing what I enjoy doing.

Bit of background information

As I have mentioned before I am using a layout I created as part of my ICT A Level website coursework.
The website was on football grounds in the UK, it was basically a guide to all the grounds. What I originally wanted to do with the website was to have a section on each ground page where users could give it a rating out of 5 and then on the index page would be the top 5 rated grounds. However I never did this as I was unable to do it due to time and understanding. Now I want to get it sorted out.

In the English football league there are 92 football teams. So I need 92 polls.
Do I need to create 92 tables?

The following is the code for the poll area, the CSS has been added in case you decide to open it up in Dreamweaver.

<div id="contentcontainer" style="width:480px; margin: 10px 0px 0px 10px; float:left;">
<div id="content" style="width:480px; background:#CCCCCC; border: 1px solid #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size:14px;">
<div id="tops" style="background:#CC0000; border-bottom: 1px solid #000000; padding:2px; font-size:12px; font-family: Verdana, Arial, Helvetica, sans-serif;">Rate This Team</div>
<form>
1
<input type="radio" name="radio" id="radio" value="1" />

2
<input type="radio" name="radio2" id="radio2" value="2" />

3
<input type="radio" name="radio3" id="radio3" value="3" />
4
<input type="radio" name="radio4" id="radio4" value="4" />
5
<input name="radio5" type="radio" id="radio5" value="5" />
</form>
</div>

</div>

I understand that I am going to be writing information to the database and then returning it to the main page. Its all confusing me because of how large it would be.

Thanks for your help,

Mark

davidj
06-22-2007, 04:24 PM
In the English football league there are 92 football teams. So I need 92 polls.
Do I need to create 92 tables?


you only need 1 table. Call the table grounds and in grounds you would need the fields to represent each ground (capacity, level, amenities, cost per game and ground name) like top trumps. Set an auto Incrementing id field which grounds can be referenced.

in a seperate table you would run your poll using the id field to represent the ground your polling on. Thats the key

Mark_W
06-22-2007, 04:35 PM
I had already done the grounds table. And I had thought I might have had to link to tables like the Ajax dynamic drop downs.

I don't really understand how the two tables are then linked.

Mark_W
06-22-2007, 04:40 PM
Actually what I will do is going through the Ajax tutorials again.

davidj
06-22-2007, 04:58 PM
no dont do that. that wont help

create a table.... grounds

create fields .... ground_id, ground_name, team, cost, etc... (ground_id = auto increment)

populate the table with ground records then come back

Mark_W
06-22-2007, 05:10 PM
Ok yep I've sorted that table out.

davidj
06-22-2007, 06:08 PM
now you populate the table with your ground info. Each ground has 1 row of data then you can echo out the grounds as per the tutorials show you.

Mark_W
06-22-2007, 06:17 PM
Yep I had already done that part as well.

davidj
06-22-2007, 11:04 PM
ok so whats next? remind me again

Mark_W
06-23-2007, 12:38 AM
Erm, well to be honest I'm no further than I was in my original post as I had already done the grounds table and created the grounds page.

I'm guessing I'm going to have to create a new table and called this say groundpoll and have to fields called poll_result_id and poll_result. Poll_result_id will link to ground_id. So for example,

Ground_id
Arsenal = 1
Aston Villa = 2
Blackburn = 3

poll_result_id __ poll_result
1 _______________ 3
1 _______________ 4
1 _______________ 5
1 _______________ 4
1 _______________ 1
2 _______________ 4
2 _______________ 4
2 _______________ 3
3 _______________ 2
3 _______________ 1
3 _______________ 2

All the poll_result_id data with 1 will be related to Arsenal etc.

So then what I want to be able to do is write the ground_id for the relevant ground into the poll_result_id field and the rating (e.g. 5) into the poll_result field.

If all of that sounds right let me know, and I will have a go at it myself tomorrow evening.

Thanks,

Mark

Mark_W
06-23-2007, 10:13 AM
OK I've managed to sort out the poll_result_id by using a hidden value next to my poll which holds the 'team_id' value. And this works perfectly.

What I am having trouble doing is storing the poll values into the database.

This is the PHP values I have set

$submit = $_POST['submit'];
$pollid = $_POST['teamid'];
$pollresult = $_POST['result'];
$pollone = $_POST['one'];
$polltwo = $_POST['two'];
$pollthree = $_POST['three'];
$pollfour = $_POST['four'];
$pollfive = $_POST['five'];This is the other PHP thing (cant remember what its called)

if ($submit && $pollid && $pollone) {
//////////////
$query = sprintf("INSERT into groundpoll (poll_result_id, poll_result) values ('$pollid', '$pollone')");
mysql_query($query) or die (mysql_error());
//////////////
$echo = "Thank you for voting";

}Using that works..obviously.

However if I used this if statement;

if ($submit && $pollid && $pollone && $polltwo && $pollthree && $pollfour && $pollfive) {

Then it wont submit as it is wanting all the poll variables to be filled in.

How do I do it so that it only sends the selected result?

davidj
06-23-2007, 11:37 AM
ah right

you need to use radio buttons and then name them the same. This way means that only one option is selected and you only need one $_POST to catch the result. The values would be different to identify which option was chosen. I would use a SWITCH to do this

Mark_W
06-23-2007, 09:48 PM
Ha, that easy once again, also solved another problem I was having with the radio buttons.

I say that though, however in my database the poll_result returns the value 'Resource id #4' Any idea what this means?

davidj
06-23-2007, 10:49 PM
show me hoyour ech-ing the result

Mark_W
06-23-2007, 11:03 PM
I'm not echoing the results yet, I'm just writing to the database at the moment.

This is my form

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
1
<input type="radio" name="poll" value="1" />

2
<input type="radio" name="poll" value="2" />

3
<input type="radio" name="poll" value="3" />
4
<input type="radio" name="poll" value="4" />
5
<input type="radio" name="poll" value="5" />
<input name="submit" type="submit" value="Submit" />
<input name="teamid" type="hidden" value="<?php echo $row['team_id']; ?>" />
<?php echo $echo;?></form>This is my PHP

<?php
require_once("connections/connection.php"); //database connection //

/////////////////////////////////////////
$submit = $_POST['submit'];
$pollid = $_POST['teamid'];
$result = $_POST['poll'];

////////////////////////////////////////


///////////////////////////////
$query =sprintf("SELECT * from grounds where team_id = $id "); // * means all, so 'select all data from table 1 //
$result =@mysql_query($query);
$row =@mysql_fetch_array($result);
////////////////////////////

if ($submit && $pollid && $result) {
//////////////
$query = sprintf("INSERT into groundpoll (poll_result_id, poll_result) values ('$pollid', '$result')");
mysql_query($query) or die (mysql_error());
//////////////
$echo = "Thank you for voting";

}


?>

davidj
06-24-2007, 12:27 PM
you are using 2 vars called $result. One catching the $_POST and the other in the recordset. You need to change one to be unique

Mark_W
06-24-2007, 02:05 PM
Ha, didn't notice that at first. Thanks davidj.

Now how would I display the results?

Say the following is the results from the poll for Arsenal:

poll_result_id __________ poll_result
1 _______________________3
1 _______________________3
1 _______________________4
1 _______________________1
1 _______________________5
1 _______________________3
1 _______________________5


That would give a rating of 3.4/5 as 3+3+4+1+5+3+5 = 24. 24/7 = 3.4

How would I get it displayed on the page as that?

Mark_W
06-24-2007, 05:19 PM
Actually I think I have it sussed out, ill just check first though.

Mark_W
06-24-2007, 06:37 PM
Ok I haven't sussed it out. I understand how to do all the maths with php such as '+' '-' '*' and '/' and I am able to do the simple and easy (to me) maths with php such as <?php echo 1+1 ?>

However I am having trouble with adding up all the data within the arrays.

davidj
06-24-2007, 06:42 PM
so you returning all the results in an array

how are you indexing the array?

Mark_W
06-24-2007, 06:47 PM
Indexing?

This is the query I did;

/////////////////////////////
$cquery =sprintf('SELECT * from groundpoll where poll_result_id = $id');
$cresult =@mysql_query($cquery);
$crow =@mysql_fetch_array($cresult);
/////////////////////////////Then I used this to check if it displayed all the votes for the ground.

<?php
do {
echo $crow['poll_result']. '<br>';
}while ($crow = mysql_fetch_array($cresult));?>