Daniel Garcia
Daniel Garcia

Reputation: 41

Foreach php function inside HTML select options

Im a newbie to this forum and have just started coding in php. Need some help. I have the following code

<?php error_reporting(-1);

require_once('/mysql/sql_connect.php');

$q = "SELECT pty.pty_profile_name AS profile FROM pty, users WHERE users.username = 'testaccount' ";
    $r = @mysqli_query ($dbc, $q);

    $results_array = array();
    while($row = mysqli_fetch_assoc($r)) {
        array_push($results_array, $row);
    echo "<pre>"; print_r($results_array); echo "</pre>"; }

?>

<p>
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>  
   <select name="pty_select" > 
   <?php foreach($results_array as $key => $value){ ?>
                    <option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option> 
    <?php } ?>
        </select>
    <input type="submit" name="Submit" />
</form>

<?php 

if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?> 

The output I get is correct, but it displays the key, eg, 0 or 1 or 2, based on what I select in the form. I need the value output. Eg 0 = Emerton, it outputs "0" instead of Emerton.

If I echo $value['profile'] instead of pty_select, I get the last result of the query all the time. Which in this example would be 2, which is Ambarvale as I believe it just chooses the last row output of the query.

I hope I've made sense. Thanks in advance.

Upvotes: 4

Views: 52731

Answers (5)

Vyktor
Vyktor

Reputation: 21007

And now let's go to ideal world :)

Build data pairs database_id => name for options:

$q = "SELECT pty.id, pty.pty_profile_name AS profile FROM pty, users 
      WHERE users.username = 'testaccount'";
$r = mysqli_query($dbc, $q);

$values = array();
while($r = mysqli_fetch_row($r)) {
    $values[$r[0]] = $r[1];
}

Never use @ when working with database, why do you want to suppress errors instead of preventing/handling them?

Now you have real database IDs and respective values (in general, using unique IDs are better... if nothing else they have greater entropy - more efficient search). And sice displaying select box is really common in webs, lets:

function selectbox( $values = array(), $attributes = array(), $selected_value = null)
{
    // Header
    echo '<select';
    foreach( $attributes as $key => $val){
        echo ' ' . htmlspecialchars($key) . '="' . htmlspecialchars( $val) . '"';
    }
    echo '>';

    // Values
    foreach( $values as $key => $val){
        echo '<option value="' . htmlspecialchars( $key) .'"';
        if( $key === $selected_value){
            echo ' selected="selected"';
        }
        echo '>' . htmlspecialchars( $val) . '</option>';
    }
    echo '</select>';
}

And now usage :)

<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>  
    <?php selectbox( $values, array( 'name' => 'pty_select')); ?>
    <input type="submit" name="Submit" />
</form>

And what to do with it then?

$id = (int)(isset( $_POST['pty_select']) ? $_POST['pty_select'] : 0);
$name = null;
if( $id && isset( $values[$id])){
    $name = $values[$id];
}

Upvotes: 2

EJTH
EJTH

Reputation: 2219

if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?> 

Change it to something like

if(isset($_POST['Submit'])) {
    echo $results_array[$_POST['pty_select']]['profile'];
}

Or alternatively use profile option value.

Upvotes: 1

abhij89
abhij89

Reputation: 625

It will obviously echo the key, as you assigned the value of options as $key if you need the options in the $_POST['pty_select'] use this:

 <select name="pty_select" > 
<?php foreach($results_array as $key => $value){ ?>
                <option value="<?php echo $value['profile'];?>"><?php echo $value['profile'];     ?></option> 
<?php } ?>
    </select>

Upvotes: 5

hsuk
hsuk

Reputation: 6860

You mean you need the value what you have used to display it. Then, Change to :

<option value="<?php echo $value['profile']; ?>">
    <?php echo $value['profile']; ?>
</option> 

Upvotes: 3

chandresh_cool
chandresh_cool

Reputation: 11830

Give

<option value="<?php echo $value['profile']; ?>"><?php echo $value['profile']; ?></option> 

instead of

<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option> 

Upvotes: 2

Related Questions