Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-11-2007, 09:30 PM   PM User | #1
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
How would I achieve a poll script?

Hello,

I want to write a poll script based on certain candidates.

My table structure is as follow:

Answers - id, answer, candidate_id, question_id
Questions - id, question
Candidates - id, names

Each candidate will have the question and the answer. For example, I have 10 questions. Each candidate will have 10 questions and each question will have answer to it of the candidate.

Thanks

What I don't know is how will I link the table. For example, I have created a candidate, I want the newly created candidate to have all the questions linked to him from questions and answers page and vice versa.

Any idea guys?

P.S: I'm a newcomer to programming.

Last edited by MHaris; 08-11-2007 at 09:39 PM..
MHaris is offline   Reply With Quote
Old 08-11-2007, 10:31 PM   PM User | #2
PappaJohn
Senior Coder

 
Join Date: Apr 2007
Location: Quakertown PA USA
Posts: 1,028
Thanks: 1
Thanked 125 Times in 123 Posts
PappaJohn will become famous soon enough
I would set the tables up like this:

candidates:
Code:
id - integer, auto-increment
first_name - varchar of appropriate length
last_name - varchar of appropriate length
questions:
Code:
id - integer, auto-increment
candidate_id - integer to link the candidate to the question
question - text (unless short questions only, then varchar)
answers:
Code:
id - integer, auto-increment
question_id - integer to link the answer to the question
answer - text
PappaJohn is offline   Reply With Quote
Old 08-11-2007, 10:42 PM   PM User | #3
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
Quote:
Originally Posted by PappaJohn View Post
I would set the tables up like this:

candidates:
Code:
id - integer, auto-increment
first_name - varchar of appropriate length
last_name - varchar of appropriate length
questions:
Code:
id - integer, auto-increment
candidate_id - integer to link the candidate to the question
question - text (unless short questions only, then varchar)
answers:
Code:
id - integer, auto-increment
question_id - integer to link the answer to the question
answer - text
How would you query?
MHaris is offline   Reply With Quote
Old 08-11-2007, 10:59 PM   PM User | #4
PappaJohn
Senior Coder

 
Join Date: Apr 2007
Location: Quakertown PA USA
Posts: 1,028
Thanks: 1
Thanked 125 Times in 123 Posts
PappaJohn will become famous soon enough
To return all questions and answers associated with each candidate, for all the candidates, the SQL would be:
Code:
SELECT c.*, q.*, a.* FROM (( candidates c LEFT JOIN questions q ON q.candidate_id = c.id ) LEFT JOIN answers a ON a.question_id = q.id ) ORDER BY c.last_name
PappaJohn is offline   Reply With Quote
Old 08-11-2007, 11:06 PM   PM User | #5
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
Quote:
Originally Posted by PappaJohn View Post
To return all questions and answers associated with each candidate, for all the candidates, the SQL would be:
Code:
SELECT c.*, q.*, a.* FROM (( candidates c LEFT JOIN questions q ON q.candidate_id = c.id ) LEFT JOIN answers a ON a.question_id = q.id ) ORDER BY c.last_name
A friend of mine recommended me to use sessions instead of SQL. He told me that the long query is the wrong method.

Can you elaborate how can I use sessions and avoid this long query?
MHaris is offline   Reply With Quote
Old 08-11-2007, 11:13 PM   PM User | #6
PappaJohn
Senior Coder

 
Join Date: Apr 2007
Location: Quakertown PA USA
Posts: 1,028
Thanks: 1
Thanked 125 Times in 123 Posts
PappaJohn will become famous soon enough
I assumed you were storing this information in database tables (MySQL was my assumption). If that's the case, you will need SQL to retrieve the information from the database, sessions will not do that for you.

I believe what your friend is referring to is long query strings in the url, something like quiz.php?candidate=1&question=364&answer=1285. In which case, sessions would be useful.

That SQL I posted is not passed in the url, but is part of your php code.
PappaJohn is offline   Reply With Quote
Old 08-12-2007, 01:40 AM   PM User | #7
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
How would I update my page on each database update?

Last edited by MHaris; 08-12-2007 at 01:47 AM..
MHaris is offline   Reply With Quote
Old 08-12-2007, 01:51 AM   PM User | #8
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
The page information is generated each time it's loaded so as soon as the database information changes the pages will show it.
Inigoesdr is offline   Reply With Quote
Old 08-12-2007, 01:52 AM   PM User | #9
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
Quote:
Originally Posted by Inigoesdr View Post
The page information is generated each time it's loaded so as soon as the database information changes the pages will show it.
It doesn't updates the page for me.

candidates.php
PHP Code:
<?php

include('../config/db.php'); // Includes Database

$sql mysql_query('SELECT * FROM candidates');

while(
$row mysql_fetch_array($sql)){
    echo 
$row['name'];
    echo 
"<br />";
}

include(
'templates/candidates.html');
$submit $_POST['submit'];
$candidate $_POST['name'];

if(isset(
$submit)){

mysql_query("INSERT INTO candidates(name) VALUES('$candidate')") or die(mysql_error());

}

mysql_close(); // Closes DB connection

?>
candidates.html
Code:
<form action="candidates.php" method="post">
<input name="name">
<input type="submit" name="submit">
</form>
MHaris is offline   Reply With Quote
Old 08-12-2007, 02:09 AM   PM User | #10
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
You're inserting the user after you select the users in the database.
PHP Code:
<form action="<?=$_SERVER['SCRIPT_NAME'];?>" method="post">
<input name="name">
<input type="submit" name="submit">
</form>
<?php
include('../config/db.php'); // Includes Database

$submit $_POST['submit'];
$candidate $_POST['name'];

if(isset(
$submit)){

mysql_query("INSERT INTO candidates(name) VALUES('$candidate')") or die(mysql_error());

}

$sql mysql_query('SELECT * FROM candidates');

while(
$row mysql_fetch_array($sql)){
    echo 
$row['name'];
    echo 
"<br />";
}

mysql_close(); // Closes DB connection
Inigoesdr is offline   Reply With Quote
Old 08-12-2007, 05:58 AM   PM User | #11
likon
Regular Coder

 
Join Date: Apr 2007
Posts: 141
Thanks: 3
Thanked 1 Time in 1 Post
likon is an unknown quantity at this point
there was one script in sourceforge.net
likon is offline   Reply With Quote
Old 08-12-2007, 04:12 PM   PM User | #12
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
How will I iterate a query?

When I insert a new question(through a form), I want to add the question(iterate) for every candidate in candidates table therefore, it would need to update the candidate_id to every question. How will I achieve this?
MHaris is offline   Reply With Quote
Old 08-12-2007, 05:38 PM   PM User | #13
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Well, I'm not sure what you're asking. There's no reason to add the question for everyone. You just need to store the question(w/possible answers), and the user(w/their answer).
Inigoesdr is offline   Reply With Quote
Old 08-12-2007, 06:07 PM   PM User | #14
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
Quote:
Originally Posted by Inigoesdr View Post
Well, I'm not sure what you're asking. There's no reason to add the question for everyone. You just need to store the question(w/possible answers), and the user(w/their answer).
There is no possible answer. The answer is of the candidate and that's the correct answer in both(true or false condition).

You can look at the database structure above.

http://www.codingforums.com/showpost...82&postcount=2
(I don't have last_name for the candidates table, only name column)
MHaris is offline   Reply With Quote
Old 08-13-2007, 04:38 AM   PM User | #15
MHaris
New Coder

 
Join Date: Jun 2007
Posts: 60
Thanks: 7
Thanked 0 Times in 0 Posts
MHaris has a little shameless behaviour in the past
Quote:
Originally Posted by MHaris View Post
There is no possible answer. The answer is of the candidate and that's the correct answer in both(true or false condition).

You can look at the database structure above.

http://www.codingforums.com/showpost...82&postcount=2
(I don't have last_name for the candidates table, only name column)
Any ideas?
MHaris is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 03:38 PM.


Advertisement
Log in to turn off these ads.