runnerjp Posted August 3, 2008 Share Posted August 3, 2008 here is my code <?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach($lines as $line) { echo '<option>'.$line.'</option>'; } echo '</select>';?> im struggling to figure out how to make it so that a value allready selected in a previus selection is shown i know that <?php echo $pclub ?> will show my club value... but from here im stumped... Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/ Share on other sites More sharing options...
JasonLewis Posted August 3, 2008 Share Posted August 3, 2008 Okay, so you've got $pclub set to get the POST value from the previous form. Basically, just put an if statement in the foreach to check if $line == $pclub. foreach($lines as $line) { if($pclub == $line){ echo '<option selected="selected">'.$line.'</option>'; }else{ echo '<option>'.$line.'</option>'; } } Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606611 Share on other sites More sharing options...
runnerjp Posted August 3, 2008 Author Share Posted August 3, 2008 ok i undertsand the consept you showed me but it doesnt work <?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach($lines as $line) { if($pclub == $line){ echo '<option selected="selected">'.$line.'</option>'; }else{ echo '<option>'.$line.'</option>'; } }?> this is what i have but it still fails to select the $pclub Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606612 Share on other sites More sharing options...
Sulman Posted August 3, 2008 Share Posted August 3, 2008 You're missing the value of the option: <?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach($lines as $line) { if($pclub == $line){ echo '<option value="'.$line.'" selected="selected">'.$line.'</option>'; }else{ echo '<option value="'.$line.'">'.$line.'</option>'; } }?> Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606622 Share on other sites More sharing options...
runnerjp Posted August 3, 2008 Author Share Posted August 3, 2008 humm its still not working... is it because im getting the data from a text file? Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606629 Share on other sites More sharing options...
JasonLewis Posted August 3, 2008 Share Posted August 3, 2008 Okay, well your closing the </select> right? Try echoing out $pclub and $line on each cycle. So: foreach($lines as $line) { echo "Line Val: {$line} - pclub: {$pclub}<br />"; And actually see that there is matching. It may be because your getting it from a file. But if you're originally setting the values with data from the same file then it wouldn't be that. Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606632 Share on other sites More sharing options...
ShaunO Posted August 3, 2008 Share Posted August 3, 2008 It looks like $line may have a new line at the end of it, therefore it is not matching $pclub Be sure to remove any extranous line breaks, either \n, \r or both Use the trim() function on $line Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606633 Share on other sites More sharing options...
runnerjp Posted August 3, 2008 Author Share Posted August 3, 2008 humm well my coee currently looks like <?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach($lines as $line) { echo "Line Val: {$line} - pclub: {$pclub}<br />"; if($pclub == $line){ echo '<option value="'.$line.'" selected="selected">'.$line.'</option>'; }else{ echo '<option value="'.$line.'">'.$line.'</option>'; } }?> this line echo "Line Val: {$line} - pclub: {$pclub}<br />"; ecos out nothing... and shaun0 im not sure what the trim() function is and how to use it Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606646 Share on other sites More sharing options...
JasonLewis Posted August 3, 2008 Share Posted August 3, 2008 Haha of course it wouldn't. It's inside a select tag. Anyway, this is what ShaunO meant. <?php $lines = file('runningclubs.txt'); echo '<select class="inputedit" id="club" name="club">'; foreach($lines as $line) { if($pclub == trim($line)){ echo '<option value="'.$line.'" selected="selected">'.$line.'</option>'; }else{ echo '<option value="'.$line.'">'.$line.'</option>'; } } echo "</select>"; ?> Remember, the PHP Manual is a good resource. trim() Link to comment https://forums.phpfreaks.com/topic/117933-selecting-allready-picked-value-in-a-drop-down-menu/#findComment-606648 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.