hakimserwa Posted July 14, 2012 Share Posted July 14, 2012 This is what i want. i have an array. array("new","junior","senior","expert"); these are option values of my edit form for my users so i want before a user makes changes to his status, the option showing should be his status first then other options bellow. i have these are status of my users table store allready so that means i can compare the current status from the database. if user status was senior i want to able to to have senior at the top of the options. $status = $row['clients']; note this is from my database now we want the use to update it. this is what i ask is there any function that can look in this array and compare to see if $status == to any of the array value and if its true let that value come to the top of the option values. or if its true then i can be able to catch it into a valuable Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 14, 2012 Share Posted July 14, 2012 <?php $status = 'senior'; // fake for demo purposes $options = array("new","junior","senior","expert"); echo "<select name='some_name'>\n"; // if the current status is one of the valid choices, output it first if(in_array($status,$options)){ echo "<option value='$status'>$status</option>\n"; } // output the remainder of the choices foreach($options as $option){ if($option != $status){ echo "<option value='$option'>$option</option>\n"; } } echo "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1361488 Share on other sites More sharing options...
hakimserwa Posted July 15, 2012 Author Share Posted July 15, 2012 hahhaha this is where it was hitting me if($option != $status){ i couldnt think of this. i will try this. Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1361618 Share on other sites More sharing options...
hakimserwa Posted July 20, 2012 Author Share Posted July 20, 2012 <?php $status = 'senior'; // fake for demo purposes $options = array("new","junior","senior","expert"); echo "<select name='some_name'>\n"; // if the current status is one of the valid choices, output it first if(in_array($status,$options)){ echo "<option value='$status'>$status</option>\n"; } // output the remainder of the choices foreach($options as $option){ if($option != $status){ echo "<option value='$option'>$option</option>\n"; } } echo "</select>\n"; hello thanks for this idea, yes it populated the status values at the top of option values but my main problem is i dont want the populated value to again show in the option or list values. it still shows there. i want if status is picked up from the array it must show at the top of the list values and it must not be repeated in the foreach. help Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363096 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 Use this instead <?php $status = 'senior'; // fake for demo purposes $options = array("new","junior","senior","expert"); echo "<select name='some_name'>\n"; // if the current status is one of the valid choices, output it first $key = array_search($status, $options); if($key !== FALSE){ echo "<option value='$status'>$status</option>\n"; unset($options[$key]); } // output the remainder of the choices foreach($options as $option){ if($option != $status){ echo "<option value='$option'>$option</option>\n"; } } echo "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363104 Share on other sites More sharing options...
hakimserwa Posted July 20, 2012 Author Share Posted July 20, 2012 Thanks will try it Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363129 Share on other sites More sharing options...
PFMaBiSmAd Posted July 20, 2012 Share Posted July 20, 2012 The code I posted doesn't repeat the option that is in $status. If your's did, then there's something wrong with the actual code you ran. Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363139 Share on other sites More sharing options...
Barand Posted July 20, 2012 Share Posted July 20, 2012 HTML produced by PFM <select name='some_name'> <option value='senior'>senior</option> <option value='new'>new</option> <option value='junior'>junior</option> <option value='expert'>expert</option> </select> HTML produced by xyph <select name='some_name'> <option value='senior'>senior</option> <option value='new'>new</option> <option value='junior'>junior</option> <option value='expert'>expert</option> </select> In both cases the status appears only once in the HTML. The first value, by default, is displayed and appears as the selected option in the dropdown list. It's the way select components work. Write yourself a browser plugin so you have one that works the way you specified. Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363144 Share on other sites More sharing options...
hakimserwa Posted July 20, 2012 Author Share Posted July 20, 2012 Write yourself a browser plugin so you have one that works the way you specified. Am sorry is i offended you but i am just desperate for help Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363152 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 If you're desperate, please try a solution before telling us it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363158 Share on other sites More sharing options...
Barand Posted July 20, 2012 Share Posted July 20, 2012 Write yourself a browser plugin so you have one that works the way you specified. Am sorry is i offended you but i am just desperate for help I am not offended but merely pointing out that you have unrealistic expectations of the normal behaviour of a select component. Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363175 Share on other sites More sharing options...
Barand Posted July 20, 2012 Share Posted July 20, 2012 PS you might want to consider an alternative <?php $status = 'senior'; // fake for demo purposes $options = array("new","junior","senior","expert"); // if the current status is one of the valid choices, output it first if(in_array($status,$options)){ echo "<input type='radio' name='some_name' value='$status' disabled /> $status<br />\n"; } // output the remainder of the choices foreach($options as $option){ if($option != $status){ echo "<input type='radio' name='some_name' value='$option' /> $option<br />\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363182 Share on other sites More sharing options...
hakimserwa Posted July 21, 2012 Author Share Posted July 21, 2012 If you're desperate, please try a solution before telling us it doesn't work. i might not know how to do something but that doesnt make me a fool to come here and ask for help and once granted one, then i start making comment on some others free help that they offer. i tried it and it failed maybe i missed out something but in my case it just failed. PS you might want to consider an alternative <?php $status = 'senior'; // fake for demo purposes $options = array("new","junior","senior","expert"); // if the current status is one of the valid choices, output it first if(in_array($status,$options)){ echo "<input type='radio' name='some_name' value='$status' disabled /> $status<br />\n"; } // output the remainder of the choices foreach($options as $option){ if($option != $status){ echo "<input type='radio' name='some_name' value='$option' /> $option<br />\n"; } } ?> thank you for understanding it show me that you were also once like me thank you. Quote Link to comment https://forums.phpfreaks.com/topic/265662-compare-value-of-an-array-with-a-value/#findComment-1363252 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.