Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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 10:39 PM.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    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

  • #3
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    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

  • #5
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    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.

  • #7
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    How would I update my page on each database update?
    Last edited by MHaris; 08-12-2007 at 02:47 AM.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    The page information is generated each time it's loaded so as soon as the database information changes the pages will show it.

  • #9
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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>

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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

  • #11
    Regular Coder
    Join Date
    Apr 2007
    Posts
    141
    Thanks
    3
    Thanked 1 Time in 1 Post
    there was one script in sourceforge.net

  • #12
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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?

  • #13
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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).

  • #14
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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)

  • #15
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •