Jump to content

DillyDong

Members
  • Posts

    10
  • Joined

  • Last visited

    Never

Posts posted by DillyDong

  1. Hello, all, I've got a question I hope you can help me out with.

    I've got a database with two columns, one is called `the_question`, and the other is `the_answer`. Say the database looks like this:

    [code]
    +++++++++++++++++++++++++++++
    |`the_question`|`the_answer`|
    |++++++++++++++++++++++++++++
    |Apple        | Red        |
    |Banana        | Green      |
    |Grape        | Purple    |
    |Blueberry    | Blue      |
    |Cherry        | Red        |
    +++++++++++++++++++++++++++++
    [/code]

    Now, what I want to do is have a form on a page where on the left-hand column, each value from `the_question` is listed, and on the right of each `the_question` value, there should be an input box where the user should enter the appropriate answer.
    Now, I want to pass this data to a new page via form action, and I want the next page to check the user's responses against the appropriate `the_answer`fields. I'm having trouble understanding exactly how to do this. The script I have for the first page is:

    [b]test_1.php[/b]
    [code]
    <form method="post" action="test_2.php">
    <table>
    <tr><td>Apple</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
    <tr><td>Banana</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
    <tr><td>Grape</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
    <tr><td>Blueberry</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
    <tr><td>Cherry</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
    </table>
    <input type="submit" value="Submit" />
    </form>
    [/code]

    [b]test_2.php[/b]
    [code]
    <?php
    foreach($_POST as $value) {
    $data = $_POST; }

    foreach($data as $data_for_query) {
    $sql = "SELECT * FROM `table` WHERE `the_answer` = ".$data_for_query.";";
    $query = mysql_query($sql);
    if(empty(mysql_num_rows($query))) {
    echo "Sorry, the correct answer was: " ;
    //And at this point, how do I get it to return the correct answer? All I passed through the form was user input :(
    else {
    echo "Good job.";
    }
    }
    ?>
    [/code]

    Any help would be greatly appreciated! I have no idea.
  2. Hi all, sorry, I know the title is ambiguous, but I wasn't sure exactly how to explain it.

    Anyway,

    Say I have the following code:

    [code]'SELECT * FROM `table` WHERE `list_number` IN (1,3,5)[/code]

    Now, let's say that there are 20 rows for each matching `list_number` criteria (ie, There are 20 rows where `list_number` = 1, 20 rows where `list_number` = 3, and 20 rows where `list_number` = 5).

    What I would like to know is, how would I tell the MySQL query to get only 10 (in order or random, doesn't matter) rows for each `list_number`? I thought that LIMIT only worked for the entire query, not per where condition.

    Any help is appreciated! Thank you!

  3. Hi all! Let's say I have the following ten checkboxes:

    [code]<input name="checkbox_1" type="checkbox" value="1" />
      <input name="checkbox_2" type="checkbox" value="1" />
      <input name="checkbox_3" type="checkbox" value="1" />
      <input name="checkbox_4" type="checkbox" value="1" />
      <input name="checkbox_5" type="checkbox" value="1" />
      <input name="checkbox_6" type="checkbox" value="1" />
      <input name="checkbox_7" type="checkbox" value="1" />
      <input name="checkbox_8" type="checkbox" value="1" />
      <input name="checkbox_9" type="checkbox" value="1" />
      <input name="checkbox_10" type="checkbox" value="1" />[/code]

    Now, for each checkbox the user checks, I want to update a MySQL query that will get values from each list corresponding to that number. For example, if they check boxes 1 and 2, I want the MySQL query to look like:

    [code]"SELECT * FROM `table` WHERE (`list_number` = 1 OR `list_number` = 2)[/code]..etc

    How would I generate this MySQL query most efficiently with ten or more checkboxes? I was thinking of using [code]foreach()[/code] but I'm not sure if that's going to work. If someone could point me in the right direction that would be great! Thanks!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.