Grab bank ID to load into HTML select

Asked

Viewed 216 times

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); ?>

1 answer

2


Maybe it’s the issue that’s not well-crafted, because I don’t fully understand what the problem is, but what I understand about your parameters is

$sql - SQL que vai retornar os resultados para preencher o <select> com valores
$idcampo - O nome do campo a dar ao <select> (pode ser qualquer coisa!)
$selected - O valor que deve ficar seleccionado no render

Right now you have <?php echo $objct->ComboBox('select id, nome from cliente', $objcli->getIdcli(), 0); ?>, I mean, you’re passing the ID of the object $objcli as the field to be used, I assume that currently the HTML on the page is something like <select name="21"> (if it is the 21 the value returned by $objcli->getIdcli()), and it should be something like <select name="id">.

For that, your call actually would have to be something like <?php echo $objcli->ComboBox('select id, nome from cliente', 'id', $objcli->getIdcli()); ?>

However, the form where this <select> is, when it is submitted and you want to know what value was chosen, just use the $_POST (or $_GET), for example, if you passed $idcampo as 'id', would be $_POST['id'] and from there you do whatever you need with that value.

//

PDO::FETCH_ASSOC

PDO::FETCH_ASSOC returns an array with the names of the fields as Indice.

// Exemplo
Array
(
    [id] => 1
    [nome] => Jão
)

In your job ComboBox, you use $row[1], that really should be making a mistake, Undefined index. You should probably use PDO::FETCH_BOTH, that gives you something like

Array
(
    [id] => 1
    [0] => 1
    [nome] => Jão
    [1] => Jão
)

And with this result, in function you should change the $row[1] for $row[0] and the $row[2] for $row[1] (since it is a 0 based array).

The value to pass in $idcampo can be just a string, can you just 'id' and must function correctly.

  • 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>&#xA; <option value="1">Jão</option>&#xA; <option value="2">Fernado</option>&#xA; <option value="3">Raimunod</option>&#xA;</select>

  • 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.

  • 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.

  • 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>

  • 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.

  • 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 line echo "<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.

Show 1 more comment

Browser other questions tagged

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