0

I have a select box being filled in with a mysql query, it functions fine and is selectable. Data changes and works fine.

It currently sorts based on the branch_id column and what I want is the session variable of $branch_id to be the first option of the select box. This is based on the branch selected from a previous page.

This is where I am stuck.

Here is some sample code of the branch dropdown.

This selection then changes a list of users that appears in an options box below this code. It all functions fine, I just need to refine it so the currently selected branch(from a prev page) is the first in the branch dropdown.

If anyone can help I'd really appreciate it.

//get list of allowed Branchs
$AllowBranch = "SELECT branch_id FROM access WHERE userid IN (SELECT id FROM user WHERE username = '{$_SESSION['user']}') ORDER BY branch_id ASC";
$getAllowBranch = mysql_query($AllowBranch);
while($getAllowBranchRow = mysql_fetch_assoc($getAllowBranch))
{
    $NameSQL = "SELECT name FROM branchlist WHERE id = '{$getAllowBranchRow['branch_id']}'";
    $Nameresult = mysql_query($NameSQL); 
    $Namerow = mysql_fetch_assoc($Nameresult);

    echo "<OPTION VALUE = '{$getAllowBranchRow['branch_id']}' "; if($NeedBranch == $getAllowBranchRow['branch_id']) { echo "selected"; } echo "> ".ucwords(strtolower($Namerow['name']));
}
echo "</SELECT>";
Colin R
  • 25
  • 7

2 Answers2

0

Check the source code to see if the selected attribute is put in the right option tag. Also, you forgot to close every <option> tag with a </option>, not sure, but this could also be the problem.

Erik Terwan
  • 2,710
  • 19
  • 28
  • thanks for the tip on the option closing. changed. not sure what you mean by "see if the selected attribute is put in the right option tag" – Colin R Sep 09 '14 at 14:31
  • Well, you call `if($NeedBranch == $getAllowBranchRow['branch_id']) {` and if that succeeds you add the `selected` attribute to your ` – Erik Terwan Sep 09 '14 at 14:40
  • ok, gotcha. yes it does that. and if i select a new one and view the code the new branch has the selected tag. – Colin R Sep 09 '14 at 15:20
0

First of all, you have missing tag <select name="<your field name>"> before while loop.

Then you have missing closing tag </option> too. You cannot solve your problem, if you have HTML broken.

So, it should looks like this:

echo '<select name="<your field name>">';
while($getAllowBranchRow = mysql_fetch_assoc($getAllowBranch))
{
    $NameSQL = "SELECT name FROM branchlist WHERE id = '{$getAllowBranchRow['branch_id']}'";
    $Nameresult = mysql_query($NameSQL); 
    $Namerow = mysql_fetch_assoc($Nameresult);

    echo "<option value='" . $getAllowBranchRow['branch_id'] . "'" . $NeedBranch == $getAllowBranchRow['branch_id'] ? " selected" : "" . ">" . ucwords(strtolower($Namerow['name'])) . "</option>";
}
echo "</select>";

What I have done?

Firstly, I fixed your missing HTML tags. Then I just simply used ternary operator to check if branch_id equals to $NeedBrach and if yes, add following string selected and if not, add nothing.

Don't forget to replace <your field name> with your expect select name.

Important final thoughts!

I have to remind using mysql_* is deprecated, you should use mysqli_* or PDO instead with prepared statements.

Community
  • 1
  • 1
Lkopo
  • 4,798
  • 8
  • 35
  • 60
  • thanks for the replies guys. i couldn't get this to go no matter which way i tried so 'executive decision' it stays the way it is. thanks for the support though and the help in trying to get it to work for me, much appreciated! :) – Colin R Sep 16 '14 at 13:37