Multiple select comparing two database lists

Asked

Viewed 20 times

0

I need to make a multiple selector in HTML, and the list of items will be loaded from a Mysql table using PHP, however, I want the values that are already in another table to be filled automatically. In the case when I create a promotion, I add several products, when I want to edit the promotion, I would like to select where I select the products, select those that are already saved. I tried some codes but it does not work, the closest is the current one that when the first item is selected, it is marked, but the others not.

$editDLL = "<br /><label>Itens</label><select name='itens[]' id='itens' class='form-control' multiple size='5'>";
while ($row2 = mysqli_fetch_row($result)) {
    $selected = 0;
    $row3 = null;
    $id     = (int)$row2[0];
    $nome   = $row2[1];
    while ($row3 = mysqli_fetch_row($cat2)) {
        $id2 = $row3[0];
        if ($id == $id2) {
            $selected = 1;
        }
    }
    if ($selected == 1) {
        $editDLL .= "<option value='$id' selected>$nome</option>";
        $selected = 0;
    } else {
        $editDLL .= "<option value='$id'>$nome</option>";
    }
}
$editDLL .= "</select>";
echo $editDLL;
  • Have you ever thought of an OUTER JOIN between the tables?

  • I hadn’t thought, but I don’t know if it’s the right way, since I need to load all the values of a table, but mark in html those that are the same as the other table, so that all the items in table 1 are present in HTML

  • It is what is expected that one SELECT * FROM tabela1 LEFT OUTER JOIN tabela2 ON (tabela1.campopk = tabela2.campofk) do.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.