0

I have this table called studien:

I have this table called studien

I want to create a dropdown menu that first creates the 5 different categories (column category) with the following statement:

SELECT DISTINCT category FROM studien ORDER BY category ASC

In another queue, I gather name and id of the study names and compare their category with the category in the first queue.

For some reason, that doesn't work.

I read something about multi_query, but how can I compare the same value from the same table?

Thanks in advance

$selectvar = "SELECT * FROM studien ORDER BY name ASC";
$selectvar2 = "SELECT DISTINCT category FROM studien ORDER BY category ASC";

$result = mysqli_query($mysqli, $selectvar);
$result2 = mysqli_query($mysqli, $selectvar2);

while ($row2 = mysqli_fetch_array($result2)) {
    echo "<option value='cat'>".utf8_encode($row2['category'])."</option>";
    while ($row = mysqli_fetch_array($result)) {
        if ($row2['category'] == $row['category']) {
            echo "<option value=".$row['id'].">".utf8_encode($row['name'])."</option>";
        }
    }
}
1
  • In all likelihood you do not want to use utf8_encode(). It has barely any real use and is an artifact of old days. Commented Jun 7, 2019 at 21:06

1 Answer 1

0

Your problem is that you are calling mysqli_fetch_array($result) multiple times in your inner loop without resetting the pointer for $result. PHP doesn't allow you to re-use the result of mysqli_query() without resetting the result pointer first. You can reset the pointer by adding the line

mysqli_data_seek($result, 0);

each time before you call mysqli_fetch_array($result).

[Apologies for my formatting, I'm on a mobile phone]

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.