Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point how to actualize a combo with the current value retrieved from the database ?

Hi Experts

Could you point how to actualize a combo with the current value retrieved from the database ?

Accordingly to

User generated image
The status value retrieved from DB at the textbox is 3  correspondent to "Em Desenvolvimento"

How to make the combobox to assume the same value  "Em Desenvolvimento"

The code I have:
<div class="form-group">
	<label for="nome">Status</label>
	<input type="text" class="form-control"  name="idstatus" placeholder="Status" value="<?php echo $viewVar['produto']->getIdStatus(); ?>" required>
</div>

<div class="form-group">
	<label for="nome">Status</label>
	<label for="idstatus" class="validar field select">
		<select name="idstatus" id="idstatus">
			<option value="">Selecione ...</option>
			<option value="1">Pendente</option>
			<option value="2">Em Desenvolvimento</option>
			<option value="3">Em Teste</option>
			<option value="4">Concluido</option>
		</select>
	</label>
</div>

Open in new window


Thanks in advance!
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

You can do this with either jQuery or PHP.

** Note: In your code you have the combo and textbox with the same name (idstatus). These should be different otherwise the jQuery selector may behave unexpectedly.

// jQuery:
$(document).ready(function() {
    $('#idstatus').val( $('[name="idstatus"]').val() );
});

Open in new window


// PHP:
<?php $val = $viewVar['produto']->getIdStatus(); ?>
<select name="idstatus" id="idstatus">
    <option value="">Selecione ...</option>
    <option value="1" <?php echo ($val == 1) ? 'selected' : null ; ?>>Pendente</option>
    <option value="2" <?php echo ($val == 2) ? 'selected' : null ; ?>>Em Desenvolvimento</option>
    <option value="3" <?php echo ($val == 3) ? 'selected' : null ; ?>>Em Teste</option>
    <option value="4" <?php echo ($val == 4) ? 'selected' : null ; ?>>Concluido</option>
</select>

Open in new window

Avatar of Eduardo Fuerte

ASKER

Hi

Very good for now.

But to obtain the data from the database isn't necessary to send an AJAX requisition?

I mean retrive data from status table...
Don't you already have the statusID from the Database in your code. You seem to be echoing it out in your textbox:

<?php echo $viewVar['produto']->getIdStatus(); ?>
Ok for obtain the data of the status current value.

What I mean is how to obtain the FK value from status table, to mount the combo options.

User generated image
So Eduardo. I'm struggling to understand exactly what you need.

If you have a combo box on your page like this:

<select name="idstatus" id="idstatus">
    <option value="">Selecione ...</option>
    <option value="1">Pendente</option>
    <option value="2">Em Desenvolvimento</option>
    <option value="3">Em Teste</option>
    <option value="4">Concluido</option>
</select>

Open in new window


To set the text in the combobox, you only need to know the value, such as 1, 2, 3, 4. If you have the idStatus from your DB (such as 3), then setting the value of the combobox to 3 will automatically select Em Teste as the text of the combobox.

Inversely, if you select Pendente from the combo box and submit your form, then a value of 1 will be sent back to the server, which is what you would insert into the idStatus field of your produto table.

Hope that makes sense, but if I've misunderstood, please let me know.
@Chris.

What I meant is

This way the values 1,2,3,4 are harded code in PHP the page.

So, if a new status are inserted at status table 5 - XXX   f.e. it's not reflected in the combo for choose.

So the combo options must to be dinamically  actualized with the id/ name that came from status table.
Ah right. Got you. Basically, you'd need to run a query against your DB and then loop through the values. I don't know how you currently have your database connection set up but here's an example using PDO:

<?php
$dsn    = "mysql:dbname=yourDb;host=localhost";
$user   = "username";
$pass   = "password";

try {

    $dbh = new PDO($dsn, $user, $pass);
    $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    $status = $dbh->query("SELECT id, nome FROM status ORDER BY id")->fetchAll();

} catch(PDOException $e) {

    die($e->getMessage());

}
?>

<select name="idstatus" id="idstatus">
    <option value="">Selecione ...</option>
    <?php foreach ($status as $option): ?>
    <option value="<?php echo $option->id ?>"><?php echo $option->nome ?></option>
    <?php endforeach; ?>
</select>

Open in new window

Almost perfect.

Just one thing more, it must to assume the existent status value retrieved from DB in editions...
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Chris

Perfect!

Thanks a lot!
No worries Eduardo :)
@Chris

Could you give a look at this question derivation?

Amazingly a similar code doesn't works at a differente page

New question