Drop Box Help

  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok guys, I'm crying out for help again!!! I have a drop box that is generated from a query of my users. What I want to do whenever I open this form is do a query of my database to see if the selection has already been set. For example, lets say that I open my form and my list has John Doe, Jane Doe, and John Smith in the list. When the form loads, it will query the database to see if that drop box has already has a value set for the user and if so, that will be the user that is displayed in the drop box. If its not set then it would go back to the first thing in my list, which is just a blank item.

here is my currect code that generates the drop down list

Code: [ Select ]
 
<form action="test_action.php" method="post">
   
    <select name="user1" >
     <option value=""></option>
       <?php
       $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
            if (!$result) {
                die("Database query failed: " . mssql_error());
            }
            while ($row = mssql_fetch_array($result)) {
            echo "<option value={$row['user_id']}>{$row['first_name']}</option>";          
            }
    ?>
        </select>
<p />
<input type="submit" value="Save" />
  1.  
  2. <form action="test_action.php" method="post">
  3.    
  4.     <select name="user1" >
  5.      <option value=""></option>
  6.        <?php
  7.        $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
  8.             if (!$result) {
  9.                 die("Database query failed: " . mssql_error());
  10.             }
  11.             while ($row = mssql_fetch_array($result)) {
  12.             echo "<option value={$row['user_id']}>{$row['first_name']}</option>";          
  13.             }
  14.     ?>
  15.         </select>
  16. <p />
  17. <input type="submit" value="Save" />


Please help!!! :?

Jordon
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I suggest you revisit this page. It would be exactly the same principle.. except it won't be checked="checked" it would be selected="selected" (In the option attribute...) e.g:
HTML Code: [ Select ]
<select name="my_selection">
<option value="Jane Doe">Jane Doe</option>
<option value="Joe Doe">Joe Doe</option>
<option value="Billy Bob" selected="selected">Billy Bob</option>
</selected>
  1. <select name="my_selection">
  2. <option value="Jane Doe">Jane Doe</option>
  3. <option value="Joe Doe">Joe Doe</option>
  4. <option value="Billy Bob" selected="selected">Billy Bob</option>
  5. </selected>

(That would be the output of the PHP script...
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, so here is the thing. I actually tried the same concept of the checked prior to posting on here. I can do the selection like you have; however, I don't know how or can't figure out how to do it due to my selection options being generated from a while loop. Also, with the checkbox, I know that checked will always be 1 and not checked will be 0; however, with my drop down for my users, I don't know what the user_id will be. The table that my drop down post to is SNP_Todo. So, whenever a name is selected, it post the user_id to the table. I wrote a query that said SELECT * from SNP_Todo. I then put the results into a array and then I want to pull the user_id to see if the drop down is already selected.

I just don't know how to incorpate it into my form, to show the user that is selected.

Is what I'm saying making any sense?

Jordon
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Yes, it makes sense.
PHP Code: [ Select ]
<form action="test_action.php" method="post">
   
    <select name="user1" >
     <option value=""></option>
       <?php
       $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
            if (!$result) {
                die("Database query failed: " . mssql_error());
            }
            while($row = mssql_fetch_array($result))
            {
                $selected = (($row['selected'] == '1') ? ' selected="selected"' : null);
                echo "<option value=\"{$row['user_id']}\"{$selected}>{$row['first_name']}</option>";          
            }
    ?>
        </select>
<p />
<input type="submit" value="Save" />
  1. <form action="test_action.php" method="post">
  2.    
  3.     <select name="user1" >
  4.      <option value=""></option>
  5.        <?php
  6.        $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
  7.             if (!$result) {
  8.                 die("Database query failed: " . mssql_error());
  9.             }
  10.             while($row = mssql_fetch_array($result))
  11.             {
  12.                 $selected = (($row['selected'] == '1') ? ' selected="selected"' : null);
  13.                 echo "<option value=\"{$row['user_id']}\"{$selected}>{$row['first_name']}</option>";          
  14.             }
  15.     ?>
  16.         </select>
  17. <p />
  18. <input type="submit" value="Save" />

I'm assuming that your users table looks something like (Rows contained in it)...
user_id
first_name
selected
--*whatever else you have there*--

Where only one user would have selected set to 1 and the rest either blank or 0... so, when you change the selected thing, make sure to set the current selected row from the user to '0' and the new user selected row to '1'. Hope that made sense :lol:

Hope that helps.
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

I hope that I understand your question correctly. Whenever I save to my database from the drop down menu. I save three things. I save a task_id, unit_id, user_id. So, my query that I'm pulling to see if that drop down is selected, looks like this:

Code: [ Select ]
$nm_results= mssql_query("SELECT * FROM SNP_Todo WHERE (task_id = 2) AND (Unit_id = 1)", $connection);



Because this is the only dropbox that post task_id 2 and unit_id 1, then I know this is the drop box. So, when task_id=2 and unit_id=1, I want to know what is in user_id and based on that, show that user as being selected.

Jordon
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

lol, I edited my previous post... I guess you might have missed it... under the code block...

[EDIT:] lol it sounds more complicated now as I read your posts :lol: I'll think on it tomorrow, but I must retire for today...

[EDIT 2:] Before I go, I'll say this. If you want to select sometime from table1 where certain things match in table2 that has a value associated with table1, you can use 'INNER JOIN' I'll speak more about it tomorrow (Or someone else who gets here before I do).
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Bogey wrote:
Yes, it makes sense.
PHP Code: [ Select ]
<form action="test_action.php" method="post">
   
    <select name="user1" >
     <option value=""></option>
       <?php
       $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
            if (!$result) {
                die("Database query failed: " . mssql_error());
            }
            while($row = mssql_fetch_array($result))
            {
                $selected = (($row['selected'] == '1') ? ' selected="selected"' : null);
                echo "<option value=\"{$row['user_id']}\"{$selected}>{$row['first_name']}</option>";          
            }
    ?>
        </select>
<p />
<input type="submit" value="Save" />
  1. <form action="test_action.php" method="post">
  2.    
  3.     <select name="user1" >
  4.      <option value=""></option>
  5.        <?php
  6.        $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
  7.             if (!$result) {
  8.                 die("Database query failed: " . mssql_error());
  9.             }
  10.             while($row = mssql_fetch_array($result))
  11.             {
  12.                 $selected = (($row['selected'] == '1') ? ' selected="selected"' : null);
  13.                 echo "<option value=\"{$row['user_id']}\"{$selected}>{$row['first_name']}</option>";          
  14.             }
  15.     ?>
  16.         </select>
  17. <p />
  18. <input type="submit" value="Save" />

I'm assuming that your users table looks something like (Rows contained in it)...
user_id
first_name
selected
--*whatever else you have there*--

Where only one user would have selected set to 1 and the rest either blank or 0... so, when you change the selected thing, make sure to set the current selected row from the user to '0' and the new user selected row to '1'. Hope that made sense :lol:

Hope that helps.



Ok, I was trying my response while you were typing this. I think that I understand. You have selected = 1; however, my users have a user_id, such as 1, 2, 3, 4, 5, etc........If I use my query and generate an array and then set a verible to 'user_id' then, could I put this:

Code: [ Select ]
 
$selected = (($row['selected'] == '$user') ? ' selected="selected"' : null);
 
  1.  
  2. $selected = (($row['selected'] == '$user') ? ' selected="selected"' : null);
  3.  


Jordon
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I'm pretty sure I got this right... this is how your inner join sql would look like... unless I totally misunderstood you :lol: (Which is quiet possible).
SQL Code: [ Select ]
SELECT * FROM SNP_Users INNER JOIN SNP_Todo ON SNP_Users.user_id = SNP_Todo.user_id (SNP_Todo.task_id = 2) AND (SNP_Todo.Unit_id = 1) ORDER BY SNP_Users.first_name ASC

That would give you information from both tables in the same database all of the information where the task_id is 2 and unit id is 1 (And the user associated with it.

So, if there is only one user associated with task_id 2 and unit_id 1, than you will get only one result... with that user... if there are more than one user.. all of those users would join in on the result set :)
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Thank you very much for your help!!! I'm going to play with it and will let you know how it comes out!!!

Thanks,
Jordon
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Ok, so it took me a little while to figure this out; however, with your help and trile and error, I was finally able to figure it out. Here is the code that worked!!!

Code: [ Select ]
<form action="test_action.php" method="post">
    <select name="user3" >
    <option value=""></option>
     <?php
     $nm1_result = mssql_query("SELECT * FROM SNP_Todo WHERE (task_id = 2) AND (Unit_id = 1)", $connection);
            if (!$nm1_result) {
                die("Database query failed: " . mssql_error());
            }
    $nm1_row = mssql_fetch_array($nm1_result);
    $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
            if (!$result) {
                die("Database query failed: " . mssql_error());
            }
            while($row = mssql_fetch_array($result))
            {
                $selected = (($row['user_id'] == "{$nm1_row['user_id']}") ? ' selected="selected"' : null);
                echo "<option value=\"{$row['user_id']}\"{$selected}>{$row['first_name']}</option>";         
            }
    ?>
        </select>
<p />
<input type="submit" value="Save" />
  1. <form action="test_action.php" method="post">
  2.     <select name="user3" >
  3.     <option value=""></option>
  4.      <?php
  5.      $nm1_result = mssql_query("SELECT * FROM SNP_Todo WHERE (task_id = 2) AND (Unit_id = 1)", $connection);
  6.             if (!$nm1_result) {
  7.                 die("Database query failed: " . mssql_error());
  8.             }
  9.     $nm1_row = mssql_fetch_array($nm1_result);
  10.     $result = mssql_query("SELECT * FROM SNP_Users ORDER BY first_name ASC", $connection);
  11.             if (!$result) {
  12.                 die("Database query failed: " . mssql_error());
  13.             }
  14.             while($row = mssql_fetch_array($result))
  15.             {
  16.                 $selected = (($row['user_id'] == "{$nm1_row['user_id']}") ? ' selected="selected"' : null);
  17.                 echo "<option value=\"{$row['user_id']}\"{$selected}>{$row['first_name']}</option>";         
  18.             }
  19.     ?>
  20.         </select>
  21. <p />
  22. <input type="submit" value="Save" />


Just wanted to share and say thank you very much!!!

Jordon
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

You know, that $nm1_row returns only one user... right?

Anyway, your welcome.
  • jordonshaw
  • Student
  • Student
  • User avatar
  • Posts: 96
  • Loc: Tennessee

Post 3+ Months Ago

Yep, due to the query pulling task_id and unit_id, it can only have 1 user.

Thanks,
Jordon
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Alright... glad you got it figured out :)

Post Information

  • Total Posts in this topic: 13 posts
  • Users browsing this forum: No registered users and 81 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.