0

enter image description here

Each row has a select dropdown, so if in the 1st row dropdown, a name has been selected and saved to the database, next time the 1st row's dropdown will show respective name based on db record.

For the 2nd, 3rd and 4th row's dropdown will show a select dropdown without any name being selected

Now i can only work out how to do it by hardcoding. But as i said the no of student name is unknown as it can be added or deleted. Is there anyway i can select the student name that has been saved in the db previously

<td><select class="input" name="name[]"  >
<option value="select" <?php if ($row['name'] == 'select') echo 
'selected="selected"'; ?> > select</option>
<option value="<?php echo $_SESSION['name'] ?>" <?php if ($row['name'] == 
 $_SESSION['name']) echo 'selected="selected"'; ?> >  <?php echo 
$_SESSION['name'] ?>   </option>
 </select></td>
epiphany
  • 756
  • 1
  • 11
  • 29
  • What is your table schema and query? `$row` contains an associative array of each record in your table. – BenM Oct 18 '17 at 07:24
  • Read the answer by dilip patel https://stackoverflow.com/a/43758800/7628448 – newbie Oct 18 '17 at 07:26
  • @ BenM. $row['name' ] contain an array with student names in it – epiphany Oct 18 '17 at 07:27
  • This drop down is used by some logged in using the created students name or is it just a general drop down? – S4NDM4N Oct 18 '17 at 07:33
  • @Sand , the dropdown display the latest student names in the db – epiphany Oct 18 '17 at 07:38
  • My question was this drop down is in a form used by a user who has the credentials of the student or is it used as a general form filed? if this is used in a general filed you can't set a selected element because there's no reference point to equal the variable data to. If this is used by a user who has their credentials in the database which you can use in session to compare and set the selected element. – S4NDM4N Oct 18 '17 at 07:42
  • i've tried to use session and compare but some problems occur. It's like a system that every students can login, their name will be stored in the cookie and session. The dropdown is to be used in a record form with multiple row and each row with a select dropdown . eg 1st row the name Mary has been saved, but now another student tom login in then in the select dropdown in the 1st row the name mary which has been saved to db wont be shown as it wont matched the session – epiphany Oct 18 '17 at 07:50
  • 1
    The first thing you need to mention this in your question because the question doesn't have this information. Then post all the codes which correspond with this question so the community can understand it better. If you do that you will get a better answer. – S4NDM4N Oct 18 '17 at 07:58
  • In your first code did you start the session using `session_start();`? – S4NDM4N Oct 18 '17 at 08:04
  • yes, and i make sure that i put it at the top of my code as well – epiphany Oct 18 '17 at 08:08
  • I can get the name from session, as it's like a shared work form, all student can login and select their name after they finish certain task. The form contain multiple row of record with each row contain a select dropdown, each row name might be different depends on the db value – epiphany Oct 18 '17 at 08:10
  • Well as I said you need to show the entire code you're just showing parts of your code with out seeing the entire code community can't give definitive answer. – S4NDM4N Oct 18 '17 at 08:15
  • it's nearly the entire code except for the session_start(), and the sql select statement. And both part seems fine as i can retrieve data from database and session – epiphany Oct 18 '17 at 08:19
  • @epiphany Do you want the selected option to be the most recently added entry to the table or the most recent user to have logged in? – Robbie Oct 18 '17 at 09:01
  • most recently added entry in the db – epiphany Oct 18 '17 at 09:06
  • @Robbie W.as there's multiple row in the db, for tthose row which its select dropdown has/have selected a name, those rows' selected value will be from the db, and for those row which its dropdown hasn't been selected, it will show the recent logged in user from the session.Hope im clear,thanks – epiphany Oct 18 '17 at 09:10
  • To clear things up further, it might be worth adding to your question a sample of your database tables and what you expect the html output to look like. – Robbie Oct 18 '17 at 09:41
  • updated,please have a look,thanks – epiphany Oct 18 '17 at 10:07

1 Answers1

2

Your while loop is going to create a new list for each row returned. Place it inside the select tag as such

<select name="course">
<option value="0">Pick student</option>
<?php
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
    }
?>
</select>

Having multiple options selected is just going to select the first option anyway. By default, the most recent addition should be the last row in the table.

Robbie
  • 200
  • 1
  • 5
  • 11
  • it's like a record, for the 1st time access, it will show all student name with none selected, then after selected a specific name to be saved, for the 2nd access, can the student name that has been saved ti the db from the last time be show in the select dropdown?\ – epiphany Oct 18 '17 at 07:42
  • How can you refer the last selection ? with out refer point. You have to send that selection to a storage and call it back when the form loads again. – S4NDM4N Oct 18 '17 at 07:44
  • @epiphany I can't test this right now but the select tag has an attribute called multiple that allows selection of multiple options. You might be able to just add that here for the functionality you're looking for. – Robbie Oct 18 '17 at 07:48
  • @Sand I wasn't quite sure if that's what OP was wanting but thought it was worth mentioning. The implementation would need to be tweaked. – Robbie Oct 18 '17 at 07:50
  • 1
    Yes and was not commenting on your answer your answer is correct. What the OP is asking to select the last user who got selected which we have to have a refer point a stored database data or something to equal it. – S4NDM4N Oct 18 '17 at 07:55