1
How can I get bank ID to load into HTML select
My function like this:
public function ComboBox($sql, $idcampo, $selected)
{
$cn = conexao::getInstance()->prepare($sql);
echo "<select name='" . $idcampo . "'>";
$selec = "";
while ($row = $cn->fetch(PDO::FETCH_ASSOC)) {
if ($row[1] == $selected) {
$selec = " selected";
} else {
$selec = "";
}
echo "<option value='" . $row[1] . "'" . $selec . ">" . $row[2] . "</option>";
}
echo "</select>";
}
When calling the function and passing $sql, $idcampo and status (selected or not).
I call her to generate the select to select the value to do the insertion, I just don’t know how to get $idcampo on the Internet page, because it would need to load all $id’s from the bank to populate select in HTML.
On the call: <?php echo $objct->ComboBox('select id, nome from cliente', $objcli->getIdcli(), 0); ?>
This would be the following, This function when called generates a select where it is called, when the appropriate parameters are passed, the third one would be for editing not applied directly to the Insert. But I don’t know how to get the $idcampo from the bank to work.
$objcli->getIdcli()
it does not return the id with database data.<select>
 <option value="1">Jão</option>
 <option value="2">Fernado</option>
 <option value="3">Raimunod</option>
</select>
– JB_
That one
$objcli
already exists in the code. I assume that page is a form for editing a record? The$objcli
would, in my opinion, be such a record, and would be filled with a previous SQL SELECT on the page.– Leite
In this case this form is for insertion. For editing I can already get the id passed by $_GET['id']. Now on Insert I’m not getting by not having id past.
– JB_
So you do not need to have the currently selected value. You just need to say which field to use as
value
to the<option>
– Leite
Yeah, but I don’t know how to get the bank’s idcampo to play in because
$objcli->getIdcli()
it does not return the id with database data. This is my problem.– JB_
I updated the answer with some more info. I think you can just pass
"id"
as the value for$idcampo
, as it is only used on the lineecho "<select name='" . $idcampo . "'>";
, is not used for anything else, so it should not affect which ID field is used for the<option value=...>
, that’s the$row[1]
, which I explain now in the answer because I think I’m wrong.– Leite