-2

is there a way to choose the initial value of a select tag from what the database really holds for that attribute?

EX: A table containing these records

ID  NAME  JOB
--  ----  ---
22  John  ENG
23  Alex  DOC
24  Peter SEC

if I wanted to edit one of these records while having a select to hold these 3 different Jobs, is there a way to select the initial value of this tag to be ENG if I wanted to edit John's record and be DOC if I chose to edit Alex's record?

NOTE: Guys I know that way that this post provides but I was asking for a way that I doesn't need to change the php code if I slightly changed the lower schemas :)

2

1 Answer 1

0

To get you started, say you pass the query parameter name (script.php?name=John):

<select name="job">
    <?php
    $query = "SELECT * FROM jobs";
    foreach(execute($query) as $row) { // pseudo-code
    ?>
        <option<?php if(isset($_GET['name']) && $_GET['name'] == $row['NAME']) echo ' selected="selected"'; ?>>
          <?=$row['JOB'];?>
        </option>
    <?php
    }
    ?>
</select>

Obviously this contains a lot of psuedo-code for the execution of the SQL, but if you loop through every row to populate the options for your select you can check to see if the query param $_GET['name'] equals the current job's NAME.

Sign up to request clarification or add additional context in comments.

2 Comments

It might be a good idea to move the SQL out of the templating code (even if this is a simple example, better in the long run )
Definitely is @MartinSamson, since the OP didn't give any example code I had nothing to work off. I just wanted to throw out a quick example for the user to get started.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.