Jump to content

Compare value of an array with a value


hakimserwa

Recommended Posts

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

Link to comment
Share on other sites

<?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";

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
    }
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.