kc20121
kc20121

Reputation: 23

php get mysql data and display in <select>

I´m trying to get which option is in the databse from a <select>, here is my code:

<select name="category" value="<?php echo $category ?>" class="form-control">
    <option value="perros">Perro</option>
    <option value="gatos">Gato</option>
    <option value="peces">Pez</option>
    <option value="aves">Ave</option>
    <option value="reptiles">Reptil</option>
    <option value="roedores">Roedor</option>
    <option value="productos">Producto</option>
</select>

my PHP:

 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM posts WHERE ID=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);

 if($row)
 {

 $category = $row['category'];

the problem is that it shows the first <option> i have. Thank you

Whole code: http://pastebin.com/2jBjt3SD

Upvotes: 0

Views: 67

Answers (3)

Nizar Ahmed
Nizar Ahmed

Reputation: 180

Give the select and id and assign it a value with javascript like

<script type="text/javascript">
document.getElementById('your_id').vaue = <?php echo $category; ?>;
</script>

Upvotes: 0

choz
choz

Reputation: 17898

I am afraid there's no shortcut for this. You need to set it like,

<select name="category" class="form-control">
    <option value="perros" <?php $category == 'perros' ? ' selected="selected"' : ''; ?>>Perro</option>
    <option value="gatos" <?php $category == 'gatos' ? ' selected="selected"' : ''; ?>>Gato</option>
    <option value="peces" <?php $category == 'peces' ? ' selected="selected"' : ''; ?>>Pez</option>
    <option value="aves" <?php $category == 'aves' ? ' selected="selected"' : ''; ?>>Ave</option>
    <option value="reptiles" <?php $category == 'reptiles' ? ' selected="selected"' : ''; ?>>Reptil</option>
    <option value="roedores" <?php $category == 'roedores' ? ' selected="selected"' : ''; ?>>Roedor</option>
    <option value="productos" <?php $category == 'productos' ? ' selected="selected"' : ''; ?>>Producto</option>
</select>

Or, if you use jQuery, you have an alternative approach like,

<script type="text/javascript">
    $(function(){
        var catVal = <?php echo $category; ?>;
        $('select[name=category]').val(catVal);
    });
</script>

Upvotes: 0

Musa
Musa

Reputation: 97717

To set the selected option of a <select> you have to put the selected attribute on the correct option. Something like

<select name="category" class="form-control">
    <option <?php echo $category=='perros'?'selected':'' ?> value="perros">Perro</option>
    <option <?php echo $category=='gatos'?'selected':'' ?>value="gatos">Gato</option>
    <option <?php echo $category=='peces'?'selected':'' ?>value="peces">Pez</option>
    <option <?php echo $category=='aves'?'selected':'' ?>value="aves">Ave</option>
    <option <?php echo $category=='perros'?'selected':'' ?>value="reptiles">Reptil</option>
    <option <?php echo $category=='roedores'?'selected':'' ?>value="roedores">Roedor</option>
    <option <?php echo $category=='productos'?'selected':'' ?>value="productos">Producto</option>
</select>

you could use a loop to make it cleaner though

Upvotes: 2

Related Questions