aebstract Posted April 18, 2008 Share Posted April 18, 2008 Okay, I know for a fact there are short-hand ways of doing what I am trying to accomplish, I'm just not 100% sure on the methods. Here is what I have for the code for a drop down menu: <select name=\"dropdown2\">"; $result = mysql_query("SELECT * FROM materials WHERE type='sheet' ORDER BY material ASC") or DIE(mysql_error()); while($r=mysql_fetch_array($result)){ $mid=$r["id"]; $material=$r["material"]; $price=$r["price"]; $width=$r["width"]; $length=$r["length"]; $content .= "<option value=\"{$mid}\">{$material}</option>\n"; } $content .= "</select> Somehow I would like it so that when I submit this form and have my $_POST['dropdown2'] variable, I can set the correct option as selected that matches which was selected on submit. Is there some short code I can throw in to my option field? Quote Link to comment Share on other sites More sharing options...
Fadion Posted April 18, 2008 Share Posted April 18, 2008 <?php if(isset($_POST['dropdown2'])){ if($mid = $_POST['dropdown2']){ $content .= "<option value=\"{$mid}\" selected=\"selected\">{$material}</option>\n"; } else{ $content .= "<option value=\"{$mid}\">{$material}</option>\n"; } } ?> Hope it is what u wanted. Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 18, 2008 Author Share Posted April 18, 2008 Sorta, I know there is a way to do all this in one line inside the <option> tag, I just don't know how to go about doing such a thing. Quote Link to comment Share on other sites More sharing options...
Fadion Posted April 18, 2008 Share Posted April 18, 2008 You're going for a normal working script, or a 100 byte (in filesize) script? U can do that code inside the <option> but u will have the same results with not-so-well-structured code. Quote Link to comment Share on other sites More sharing options...
haku Posted April 18, 2008 Share Posted April 18, 2008 The code he gave you was entirely fine (with the exception of the if statement, it was missing an equals sign). But if you want to be really picky, you can use this: <?php if(isset($_POST['dropdown2'])){ if($r['id'] == $_POST['dropdown2']){ $content .= "<option value=\"{$r['id']}\" selected=\"selected\">{$r['material']}</option>". PHP_EOL; } else{ $content .= "<option value=\"{$r['id']}\">{$r['material']}</option>" . PHP_EOL; } } ?> Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 18, 2008 Author Share Posted April 18, 2008 Unless one of the wizzes jumps in and makes a note on this, I'll prob. end up using an if statement such as this. Though I know for a fact it can be done in one short line of code. I had a navigation set up once that it would decide which menu item was chosen by a single line of code, but I forget how I had it and I don't think I have the file anymore. Quote Link to comment Share on other sites More sharing options...
haku Posted April 18, 2008 Share Posted April 18, 2008 Look big guy, you have two people who gave you well formed code that was better than yours, and you come up with 'unless one of the wizzes hops in'. Not a thanks at all. Then you say 'I know for a fact it can be done'. Why aren't you doing it yourself then? I'm guessing its because you are a shatty coder. $content .= ($r['id'] == $_POST['dropdown2']) ? "<option value=\"{$r['id']}\" selected=\"selected\">{$r['material']}</option>". PHP_EOL : "<option value=\"{$r['id']}\">{$r['material']}</option>" . PHP_EOL; This code is a hell of a lot sloppier as its hard to read. But its what you wanted. Now kindly go and screw off. Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 18, 2008 Author Share Posted April 18, 2008 Dude seriously, you're missing out on what I'm saying. I said that I would probably end up using your methods and that should be a thanks in itself, but I'm sorry that you have to have it handed to you on a silver platter to be happy with yourself and I am guessing your self-esteem. My code doesn't have this in it, cause I hadn't attempted yet. Then you say 'I know for a fact it can be done'. Why aren't you doing it yourself then? I'm guessing its because you are a shatty coder. I have mentioned several times that I did not know what had to be done and that I do know it can be. That is the reason I am not doing it myself. Please read. Though thanks for the name calling, kid. Jeez 0.o It's not done directly through an if statement the way you are trying, which is why I was hoping maybe someone else would come in that knew how to do it the way I was getting at. If you don't, stop trying then? Quote Link to comment Share on other sites More sharing options...
haku Posted April 18, 2008 Share Posted April 18, 2008 Whats with kids these days? Ask them to politely screw off, and they get all pissy. I guess I'll have word it differently for you. F uck Off. Quote Link to comment Share on other sites More sharing options...
aebstract Posted April 18, 2008 Author Share Posted April 18, 2008 wow... get out of my topic, please. I wasn't getting pissy in the least bit, please (as stated before) actually read what I say. Filter skipping? Break some more forum rules please. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.