Print two queries in the same "td" or "select"

0

Good morning, I'm doing a CRUD, and right now I'm making a table where I can select modify or insert users, well I come to the conclusion, that when I pass the table to users they choose for example which user they want to select, but they can only select the acronym, but of course I would like them to select the acronym of their names but that next or superimposed you can see which name and surnames correspond to that acronym.

This is the code that I have to select the acronyms right now, I just need to print next or superimposed the name plus last names that are in the same table as the acronym.

<td>
    <select id="acro_usuario1" name="acro_usuario1">
            <option value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_usuario') : ''; ?>"><?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_usuario') : ''; ?> </option>    
            <?php
            $pdo = new PDO('mysql:host=localhost;dbname=deimos1', 'root', '');
            $pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
            $stmt = $pdo->prepare('Select acro_usuario from usuarios'); 
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                   echo '<option>' . $row['acro_usuario'] . '</option>';
            }
            ?>
     </select>
</td>
    
asked by Alberto Cepero de Andrés 27.04.2017 в 08:55
source

1 answer

1

What you should do is put inside the <option> tags the text you want the users to see and in the value attribute the value you want to be sent with the form.

It is important that all data go through the htmlspecialchars() function so that characters that can be confused with special HTML characters are replaced by its HTML entities ( < , > , " , ' and & ).

Example:

<td>
    <select id="acro_usuario1" name="acro_usuario1">
            <option value="<?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_usuario') : ''; ?>"><?php echo isset($obj_categoria) ? $obj_categoria->__GET('acro_usuario') : ''; ?> </option>    
            <?php
            $pdo = new PDO('mysql:host=localhost;dbname=deimos1', 'root', '');
            $pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
            $stmt = $pdo->prepare('Select acro_usuario from usuarios'); 
            $stmt->execute();
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                   echo '<option name="' .
                     htmlspecialchars($row['acro_usuario']) . '">' .
                     htmlspecialchars(
                       $row['acro_usuario'] . '(' .
                       $row['nombre'] . ' ' .
                       $row['apellidos'] . ')'
                     ) . '</option>';
            }
            ?>
     </select>
</td>

Depending on the method used in the form, the value of acro_usuario selected will arrive to you as $_GET['acro_usuario1'] or $_POST['acro_usuario1'] (in general, but discouraged, $_REQUEST['acro_usuario1'] ).

    
answered by 27.04.2017 / 11:03
source