3

I've searched the archives and this question seems closest to my own. But, unfortunately I've not been able to derive a solution from it. I have a form that users complete to create a list of questions from a large quiz database. The questions are categorised by subjects and graded by difficulty (1,2,3,4). Users can create a list of questions by selecting subjects and difficulty. It works fine. However we don't have many grade 1 questions on some subjects so want to combine the 1s and 2s into a single 'easy' grade. The form's option list works fine as below, but I want the second option to search for values 1&2 not just 2. Here's the relevant form code (it doesn't include all the subject details, just the difficulty selection):

<form action="" method="post" id="quizbuilder">
  <table width="400" border="0">
  <tr>
    <td>number of questions</td>
    <td><input name="quizSize" type="number" value="20" size="2" maxlength="2" min="1" max="50" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>difficulty</td>
    <td><select name="grade">
    <option value="%" selected="selected">any</option>
    <option value="2">easy</option>
      <option value="3">medium</option>
      <option value="4">difficult</option>
    </select>
    <td>&nbsp;</td>
  </tr>
  <tr>

The query looks like this:

mysql_select_db($database_qbquery, $qbquery);
$query_userquiz = sprintf("SELECT * FROM questions WHERE (grade LIKE %s) AND (topic1 LIKE %s OR topic2 LIKE %s OR topic3 LIKE %s) ORDER BY RAND() LIMIT %s", GetSQLValueString($userGrade_userquiz, "text"),GetSQLValueString($userTopic_userquiz, "text"),GetSQLValueString($userTopic_userquiz, "text"),GetSQLValueString($userTopic_userquiz, "text"),GetSQLValueString($userQuizSize_userquiz, "int"));
$userquiz = mysql_query($query_userquiz, $qbquery) or die(mysql_error());
$row_userquiz = mysql_fetch_assoc($userquiz);
$totalRows_userquiz = mysql_num_rows($userquiz);

Of course, what I'd love to do would be just put:

<option value="1,2">easy</option>

But I know that's not going to work because it's read as a string and not the values. Any help would be very much appreciated, my head is now spinning, it seems like it should be simple, but it's not to me.

Community
  • 1
  • 1
Graham C
  • 33
  • 3
  • 1
    You can always `explode(',', $value);` on that. – Peon Jan 08 '13 at 11:56
  • 1
    I imagine your GRADE Column keeps numeric values, does it? If it is the case change your Query for something like that: "SELECT * FROM questions WHERE grade IN(%s) AND (topic1 LIKE %s OR topic2 LIKE %s OR topic3 LIKE %s) ORDER BY RAND() LIMIT %s" I hope it helps. – medina Jan 08 '13 at 12:05
  • @DainisAbols, So keep the "1,2," as an option and explode that? If it is quick for you to do, can you elaborate? I'm guessing this is the cleanest solution. – Graham C Jan 08 '13 at 12:31
  • 1
    @medina By way of update and giving credit where it's due, my problem was eventually solved by using a version of your query above, many thanks for that and apologies for not exploring it before. – Graham C Jan 10 '13 at 14:32

1 Answers1

0

As I posted in the comment, you can easily explode the value before using it:

$value = $_POST['name'];
$name = explode( ',', $value );

Now you have an array here, with 1 or multiple values, just use them as you need. But it is a bit unclear, what's the final code, you are expecting.

PS: Remember to do some additional sql injection checks, before feeding them to you query.

Peon
  • 7,902
  • 7
  • 59
  • 100