Creating array elements dynamically

-3

The form below contained in a page a.php submits to a page b.php .

<form method="post" action="b.php">
   <select name="zzz" id="zzz">
    <option value="id1" label="aaa">aaa</option>
    ..................
    ..................

In the page b.php I have the following code created manually that returns the label of the selected option of page a.php , I said the label and not the value.

<?php

  $options = array();

  $options["id1"] = "aaa";
  $options["id2"] = "bbb";
  $options["id3"] = "ccc";

  $label = $options[$_POST['zzz']];

  echo $label;

You can create these lines

 $options["id1"] = "aaa";
 $options["id2"] = "bbb";
 $options["id3"] = "ccc";

dynamically from a select being id1, id2 .... and aaa, bbb ... the values contained in the database table.

Is there any way to create these lines and that work like the one manually created? That is, instead of typing in the code id1 = "aaa" doing something like $row["id"] = "$row["xxx"]

    
asked by anonymous 19.07.2017 / 04:58

2 answers

3

Despite the downvotes, I made a point of reopening this post and show a solution.

On page a.php the optionsArray.php

$servidor = "localhost";
$usuario = "Usuario";
$senha = "SENHA";
$dbname = "NOME_DB";

$mysqli = mysqli_connect($servidor, $usuario, $senha, $dbname);


$query = "SELECT id_regional, regional FROM t_regional ORDER BY regional";
$resultado=$mysqli->query($query);


$filename="optionsArray.php";

if (!file_exists($filename)) {

    $linhas="";

    while ($row = mysqli_fetch_assoc($resultado)){

    $linhas .= "\n".'$options["'.$row['id_regional'].'"] = "'.$row['regional'].'";';

    }

    $linhas ="<?php \n".$linhas."\n ?>";

    file_put_contents($filename, $linhas);

}

On page b.php add the file optionsArray.php created on page a.php

$options = array();

include_once("optionsArray.php");

$label = $options[$_POST['cbx_regional']];

echo $label; 

The two pages above could merge into one without any problems.

  

Question: Why do you want to create this file dynamically?

Answer: Anderson Carlos Woss's answer is certainly the most obvious.

But I do not know if I understood the comment in the accepted answer to this post #

I have to add $ options ["music / three"]="somename"; $ Options ["music / four"]="somename"; to each one or is there another way to do this? " / p>

This is the reason for the question and this is the reason for my answer.

Opera Summary: The utility of this only the American could explain:)

  

NOTE: It is not clear where this information would come from, but I believe it is from a database.

    
19.07.2017 / 23:23
1

Assuming we have the browsers table in the database:

id | name
---+------------------
1  + Google Chrome
---+------------------
2  + Firefox
---+------------------
3  + Opera
---+------------------
4  + Safari
---+------------------
5  + Internet Explorer
---+------------------

We can generate the following HTML with PHP:

<?php

//...

$result = mysqli_query($mysqli, "SELECT 'id', 'name' FROM 'browsers'");

echo "<select name='browser'>";

while ($row = mysqli_fetch_assoc($result)) {
    echo "<option value='{$row['id']}' label='{$row['name']}'>{$row['name']}</option>";
}

echo "</select>";

The HTML output will be:

<select name='browser'>
    <option value='1' label='Google Chrome'>Google Chrome</option>
    <option value='2' label='Firefox'>Firefox</option>
    <option value='3' label='Opera'>Opera</option>
    <option value='4' label='Safari'>Safari</option>
    <option value='5' label='Internet Explorer'>Internet Explorer</option>
</select>

When the form is submitted, in PHP you can retrieve the value of the value attribute selected. This value is the id of the selected registry of the browsers table, so to get the name of the selected browser, just select the respective database record:

<?php

// ...

if ($stmt = mysqli_prepare($mysqli, "SELECT 'id', 'name' FROM 'browsers' WHERE 'id' = ?")) {

    mysqli_stmt_bind_param($stmt, "i", $_POST["browser"]);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $id, $name);
    mysqli_stmt_fetch($stmt);

    echo "O navegador selecionado foi: ", $name;

}

The complete code tested was:

<form action="" method="post">
    <select name="browser">
        <?php

            $mysqli = mysqli_connect("localhost", "root", "", "sopt");

            $result = mysqli_query($mysqli, "SELECT 'id', 'name' FROM 'browsers'");

            while ($row = mysqli_fetch_assoc($result)) {
                echo "<option value='{$row['id']}' label='{$row['name']}'>{$row['name']}</option>";
            }

        ?>
    </select>
    <button>Enviar</button>
</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    if ($stmt = mysqli_prepare($mysqli, "SELECT 'id', 'name' FROM 'browsers' WHERE 'id' = ?")) {

        mysqli_stmt_bind_param($stmt, "i", $_POST["browser"]);

        mysqli_stmt_execute($stmt);

        mysqli_stmt_bind_result($stmt, $id, $name);

        mysqli_stmt_fetch($stmt);

        echo "O navegador selecionado foi: ", $name;

    }

}

?>
    
20.07.2017 / 00:16