how to avoid repetition of values in dropdown list while updating in php
I want to update "profile of a user" in php. There is a repetition of one value for two times in dropdown list. for example i take language value='Punjabi' from database but there is also a value placed in dropdown with name of 'Punjabi'.
The issue is simply that there is a repetition of value which i don't want.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?> '> <?php echo $queryArray["language"]; ?></option>
//for example, the value from database is "Punjabi"
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
</select>
<?php } ?>
when a value='Punjabi' from database is selected in dropdown list, the dropdown should not show the value='Punjabi' that is already placed in dropdown.
Remember: i have more than 1000 values in my dropdown(html) list.
screenshot
javascript php html dropdown
add a comment |
I want to update "profile of a user" in php. There is a repetition of one value for two times in dropdown list. for example i take language value='Punjabi' from database but there is also a value placed in dropdown with name of 'Punjabi'.
The issue is simply that there is a repetition of value which i don't want.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?> '> <?php echo $queryArray["language"]; ?></option>
//for example, the value from database is "Punjabi"
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
</select>
<?php } ?>
when a value='Punjabi' from database is selected in dropdown list, the dropdown should not show the value='Punjabi' that is already placed in dropdown.
Remember: i have more than 1000 values in my dropdown(html) list.
screenshot
javascript php html dropdown
are all languages present in database
– Amitoj Singh Ahuja
Jan 1 at 10:47
In the loop why is<option value="Punjabi">Punjabi</option>present? thats causing the repetition i think
– Hp_issei
Jan 1 at 10:53
and wouldselect distinct language from profile...not work? Also, why have theselectelement itself within the loop??
– RamRaider
Jan 1 at 11:13
thankx to all. @Ali Sheikhpour answered this question.
– Sajjad Ali
Jan 1 at 11:25
add a comment |
I want to update "profile of a user" in php. There is a repetition of one value for two times in dropdown list. for example i take language value='Punjabi' from database but there is also a value placed in dropdown with name of 'Punjabi'.
The issue is simply that there is a repetition of value which i don't want.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?> '> <?php echo $queryArray["language"]; ?></option>
//for example, the value from database is "Punjabi"
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
</select>
<?php } ?>
when a value='Punjabi' from database is selected in dropdown list, the dropdown should not show the value='Punjabi' that is already placed in dropdown.
Remember: i have more than 1000 values in my dropdown(html) list.
screenshot
javascript php html dropdown
I want to update "profile of a user" in php. There is a repetition of one value for two times in dropdown list. for example i take language value='Punjabi' from database but there is also a value placed in dropdown with name of 'Punjabi'.
The issue is simply that there is a repetition of value which i don't want.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?> '> <?php echo $queryArray["language"]; ?></option>
//for example, the value from database is "Punjabi"
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
</select>
<?php } ?>
when a value='Punjabi' from database is selected in dropdown list, the dropdown should not show the value='Punjabi' that is already placed in dropdown.
Remember: i have more than 1000 values in my dropdown(html) list.
screenshot
javascript php html dropdown
javascript php html dropdown
edited Jan 1 at 11:02
Sajjad Ali
asked Jan 1 at 10:39
Sajjad AliSajjad Ali
286
286
are all languages present in database
– Amitoj Singh Ahuja
Jan 1 at 10:47
In the loop why is<option value="Punjabi">Punjabi</option>present? thats causing the repetition i think
– Hp_issei
Jan 1 at 10:53
and wouldselect distinct language from profile...not work? Also, why have theselectelement itself within the loop??
– RamRaider
Jan 1 at 11:13
thankx to all. @Ali Sheikhpour answered this question.
– Sajjad Ali
Jan 1 at 11:25
add a comment |
are all languages present in database
– Amitoj Singh Ahuja
Jan 1 at 10:47
In the loop why is<option value="Punjabi">Punjabi</option>present? thats causing the repetition i think
– Hp_issei
Jan 1 at 10:53
and wouldselect distinct language from profile...not work? Also, why have theselectelement itself within the loop??
– RamRaider
Jan 1 at 11:13
thankx to all. @Ali Sheikhpour answered this question.
– Sajjad Ali
Jan 1 at 11:25
are all languages present in database
– Amitoj Singh Ahuja
Jan 1 at 10:47
are all languages present in database
– Amitoj Singh Ahuja
Jan 1 at 10:47
In the loop why is
<option value="Punjabi">Punjabi</option> present? thats causing the repetition i think– Hp_issei
Jan 1 at 10:53
In the loop why is
<option value="Punjabi">Punjabi</option> present? thats causing the repetition i think– Hp_issei
Jan 1 at 10:53
and would
select distinct language from profile... not work? Also, why have the select element itself within the loop??– RamRaider
Jan 1 at 11:13
and would
select distinct language from profile... not work? Also, why have the select element itself within the loop??– RamRaider
Jan 1 at 11:13
thankx to all. @Ali Sheikhpour answered this question.
– Sajjad Ali
Jan 1 at 11:25
thankx to all. @Ali Sheikhpour answered this question.
– Sajjad Ali
Jan 1 at 11:25
add a comment |
6 Answers
6
active
oldest
votes
Instead of creating a new option according to the user data, Check if existing options are equal to user data:
<select name="language" id="language" >
<option value="Punjabi" <?php if ($queryArray["language"]=="Punjabi"){echo 'selected="selected"'} ?>>Punjabi</option>
<option value="Hindi" <?php if ($queryArray["language"]=="Hindi"){echo 'selected="selected"'} ?>>Hindi</option>
<option value="Urdu" <?php if ($queryArray["language"]=="Urdu"){echo 'selected="selected"'} ?>>Urdu</option>
</select>
If there are large number of options and you don't want to hard code these conditions, you can remove the second option using javascript on DOM ready:
$(document).ready(function(){
$('option[value="<?php echo $queryArray["language"] ?>"]').eq(1).remove();
})
it is not possible because i have dropdown list which contain more than 1000 values.
– Sajjad Ali
Jan 1 at 10:49
Are those values read from a database or hard coded?@SajjadAli
– Ali Sheikhpour
Jan 1 at 10:50
thses are hard coded and placed in html
– Sajjad Ali
Jan 1 at 10:53
Ok. I have added a jquery solution to my answer. @SajjadAli
– Ali Sheikhpour
Jan 1 at 10:57
<؟php? Nice, but I doubt it works, or does it?
– digijay
Jan 1 at 11:01
|
show 1 more comment
skip the loop when value is equal to Punjabi, Urdu and Hindi.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<?php if($queryArray["language"]!="Punjabi" && $queryArray["language"]!="Urdu" &&
$queryArray["language"]!="Hindi") { ?>
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } ?>
i have not only these three value. There are above than 1000 values which i have in html drop down list.
– Sajjad Ali
Jan 1 at 11:01
are you getting duplicate values from database?
– rAnA bilAl
Jan 2 at 9:35
yes rana saab. it's answered above
– Sajjad Ali
Jan 2 at 9:37
add a comment |
I think you are doing it wrong way the correct way would be having a table which stored all the languages along with values
using selected attribute to achieve your objective
<?php
$result=mysqli_query($conn, "select * from profile where id=$firstPerson");
$queryArray1=mysqli_fetch_array($result);
$langOfUser=$queryArray1["language"];
?>
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from langtab");
while($queryArray=mysqli_fetch_array($result)){ ?>
<option value='<?php echo $queryArray["languageValue"];?> ' <?php if($langOfUser== $queryArray["languageValue"]){ echo 'selected';}?>> <?php echo $queryArray["languageName"]; ?></option>
<?php } ?>
</select>
add a comment |
You have to use if condition to display values in select option.
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){
if($queryArray["language"]!="Punjabi") {
$opval = "<option value=" . $queryArray["language"] . ">". $queryArray["language"]. " </option> "
echo $opval;
}
?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<option value="Urdu">Urdu</option>
</select>
i dont want to stop only "Punjabi" language, punjabi came from db and also placed in html dropdown and both are showing in html dropdown list, but i want only one "Punjabi" should show in dropdown
– Sajjad Ali
Jan 1 at 10:59
I have updated the logic. It might help you.
– Umar Abdullah
Jan 1 at 11:04
The logic will not add value in the select option due to if condition.
– Umar Abdullah
Jan 1 at 11:45
add a comment |
So your problem is that you have html hardcoded options and database options. You need to merge them into one on that website.
So you can use some javascript
elements = [1, 2, 9, 15].join(',')
$.post('post.php', {elements: elements})
But you can fill your elements like this is you don´t want to write it by hand
$("#id select").each(function()
{
allOptionsInSelect.push($(this).val());
});
Than on php side you can do
$elements = $_POST['elements'];
$elements = explode(',', $elements);
And now you have html hardcoded select on server side. Now you need to check if it doesn´t already exist when you are printing from database
You can do that like this
if(in_array(value_from_database, $elements) {
// It is so skip
} else {
// It is not, so print it
}
add a comment |
You can use if elseif this way.
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?>'><?php echo $queryArray["language"]; ?></option>
<?php if ($queryArray["language"] == "Hindi") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } elseif ($queryArray["language"] == "Urdu") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<?php } elseif ($queryArray["language"] == "Punjabi") { ?>
<option value="Urdu">Urdu</option>
<option value="Hindi">Hindi</option>
<?php } ?>
the above code will always have three languages(Hindi, Punjabi and Urdu) in the dropdown list without repitition.
– Vishnu V
Jan 1 at 11:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994790%2fhow-to-avoid-repetition-of-values-in-dropdown-list-while-updating-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of creating a new option according to the user data, Check if existing options are equal to user data:
<select name="language" id="language" >
<option value="Punjabi" <?php if ($queryArray["language"]=="Punjabi"){echo 'selected="selected"'} ?>>Punjabi</option>
<option value="Hindi" <?php if ($queryArray["language"]=="Hindi"){echo 'selected="selected"'} ?>>Hindi</option>
<option value="Urdu" <?php if ($queryArray["language"]=="Urdu"){echo 'selected="selected"'} ?>>Urdu</option>
</select>
If there are large number of options and you don't want to hard code these conditions, you can remove the second option using javascript on DOM ready:
$(document).ready(function(){
$('option[value="<?php echo $queryArray["language"] ?>"]').eq(1).remove();
})
it is not possible because i have dropdown list which contain more than 1000 values.
– Sajjad Ali
Jan 1 at 10:49
Are those values read from a database or hard coded?@SajjadAli
– Ali Sheikhpour
Jan 1 at 10:50
thses are hard coded and placed in html
– Sajjad Ali
Jan 1 at 10:53
Ok. I have added a jquery solution to my answer. @SajjadAli
– Ali Sheikhpour
Jan 1 at 10:57
<؟php? Nice, but I doubt it works, or does it?
– digijay
Jan 1 at 11:01
|
show 1 more comment
Instead of creating a new option according to the user data, Check if existing options are equal to user data:
<select name="language" id="language" >
<option value="Punjabi" <?php if ($queryArray["language"]=="Punjabi"){echo 'selected="selected"'} ?>>Punjabi</option>
<option value="Hindi" <?php if ($queryArray["language"]=="Hindi"){echo 'selected="selected"'} ?>>Hindi</option>
<option value="Urdu" <?php if ($queryArray["language"]=="Urdu"){echo 'selected="selected"'} ?>>Urdu</option>
</select>
If there are large number of options and you don't want to hard code these conditions, you can remove the second option using javascript on DOM ready:
$(document).ready(function(){
$('option[value="<?php echo $queryArray["language"] ?>"]').eq(1).remove();
})
it is not possible because i have dropdown list which contain more than 1000 values.
– Sajjad Ali
Jan 1 at 10:49
Are those values read from a database or hard coded?@SajjadAli
– Ali Sheikhpour
Jan 1 at 10:50
thses are hard coded and placed in html
– Sajjad Ali
Jan 1 at 10:53
Ok. I have added a jquery solution to my answer. @SajjadAli
– Ali Sheikhpour
Jan 1 at 10:57
<؟php? Nice, but I doubt it works, or does it?
– digijay
Jan 1 at 11:01
|
show 1 more comment
Instead of creating a new option according to the user data, Check if existing options are equal to user data:
<select name="language" id="language" >
<option value="Punjabi" <?php if ($queryArray["language"]=="Punjabi"){echo 'selected="selected"'} ?>>Punjabi</option>
<option value="Hindi" <?php if ($queryArray["language"]=="Hindi"){echo 'selected="selected"'} ?>>Hindi</option>
<option value="Urdu" <?php if ($queryArray["language"]=="Urdu"){echo 'selected="selected"'} ?>>Urdu</option>
</select>
If there are large number of options and you don't want to hard code these conditions, you can remove the second option using javascript on DOM ready:
$(document).ready(function(){
$('option[value="<?php echo $queryArray["language"] ?>"]').eq(1).remove();
})
Instead of creating a new option according to the user data, Check if existing options are equal to user data:
<select name="language" id="language" >
<option value="Punjabi" <?php if ($queryArray["language"]=="Punjabi"){echo 'selected="selected"'} ?>>Punjabi</option>
<option value="Hindi" <?php if ($queryArray["language"]=="Hindi"){echo 'selected="selected"'} ?>>Hindi</option>
<option value="Urdu" <?php if ($queryArray["language"]=="Urdu"){echo 'selected="selected"'} ?>>Urdu</option>
</select>
If there are large number of options and you don't want to hard code these conditions, you can remove the second option using javascript on DOM ready:
$(document).ready(function(){
$('option[value="<?php echo $queryArray["language"] ?>"]').eq(1).remove();
})
edited Jan 1 at 11:09
answered Jan 1 at 10:46
Ali SheikhpourAli Sheikhpour
5,57311742
5,57311742
it is not possible because i have dropdown list which contain more than 1000 values.
– Sajjad Ali
Jan 1 at 10:49
Are those values read from a database or hard coded?@SajjadAli
– Ali Sheikhpour
Jan 1 at 10:50
thses are hard coded and placed in html
– Sajjad Ali
Jan 1 at 10:53
Ok. I have added a jquery solution to my answer. @SajjadAli
– Ali Sheikhpour
Jan 1 at 10:57
<؟php? Nice, but I doubt it works, or does it?
– digijay
Jan 1 at 11:01
|
show 1 more comment
it is not possible because i have dropdown list which contain more than 1000 values.
– Sajjad Ali
Jan 1 at 10:49
Are those values read from a database or hard coded?@SajjadAli
– Ali Sheikhpour
Jan 1 at 10:50
thses are hard coded and placed in html
– Sajjad Ali
Jan 1 at 10:53
Ok. I have added a jquery solution to my answer. @SajjadAli
– Ali Sheikhpour
Jan 1 at 10:57
<؟php? Nice, but I doubt it works, or does it?
– digijay
Jan 1 at 11:01
it is not possible because i have dropdown list which contain more than 1000 values.
– Sajjad Ali
Jan 1 at 10:49
it is not possible because i have dropdown list which contain more than 1000 values.
– Sajjad Ali
Jan 1 at 10:49
Are those values read from a database or hard coded?@SajjadAli
– Ali Sheikhpour
Jan 1 at 10:50
Are those values read from a database or hard coded?@SajjadAli
– Ali Sheikhpour
Jan 1 at 10:50
thses are hard coded and placed in html
– Sajjad Ali
Jan 1 at 10:53
thses are hard coded and placed in html
– Sajjad Ali
Jan 1 at 10:53
Ok. I have added a jquery solution to my answer. @SajjadAli
– Ali Sheikhpour
Jan 1 at 10:57
Ok. I have added a jquery solution to my answer. @SajjadAli
– Ali Sheikhpour
Jan 1 at 10:57
<؟php? Nice, but I doubt it works, or does it?– digijay
Jan 1 at 11:01
<؟php? Nice, but I doubt it works, or does it?– digijay
Jan 1 at 11:01
|
show 1 more comment
skip the loop when value is equal to Punjabi, Urdu and Hindi.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<?php if($queryArray["language"]!="Punjabi" && $queryArray["language"]!="Urdu" &&
$queryArray["language"]!="Hindi") { ?>
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } ?>
i have not only these three value. There are above than 1000 values which i have in html drop down list.
– Sajjad Ali
Jan 1 at 11:01
are you getting duplicate values from database?
– rAnA bilAl
Jan 2 at 9:35
yes rana saab. it's answered above
– Sajjad Ali
Jan 2 at 9:37
add a comment |
skip the loop when value is equal to Punjabi, Urdu and Hindi.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<?php if($queryArray["language"]!="Punjabi" && $queryArray["language"]!="Urdu" &&
$queryArray["language"]!="Hindi") { ?>
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } ?>
i have not only these three value. There are above than 1000 values which i have in html drop down list.
– Sajjad Ali
Jan 1 at 11:01
are you getting duplicate values from database?
– rAnA bilAl
Jan 2 at 9:35
yes rana saab. it's answered above
– Sajjad Ali
Jan 2 at 9:37
add a comment |
skip the loop when value is equal to Punjabi, Urdu and Hindi.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<?php if($queryArray["language"]!="Punjabi" && $queryArray["language"]!="Urdu" &&
$queryArray["language"]!="Hindi") { ?>
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } ?>
skip the loop when value is equal to Punjabi, Urdu and Hindi.
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){ ?>
<select name="language" id="language" >
<?php if($queryArray["language"]!="Punjabi" && $queryArray["language"]!="Urdu" &&
$queryArray["language"]!="Hindi") { ?>
<option value="Hindi">Hindi</option>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } ?>
answered Jan 1 at 10:54
rAnA bilAlrAnA bilAl
264
264
i have not only these three value. There are above than 1000 values which i have in html drop down list.
– Sajjad Ali
Jan 1 at 11:01
are you getting duplicate values from database?
– rAnA bilAl
Jan 2 at 9:35
yes rana saab. it's answered above
– Sajjad Ali
Jan 2 at 9:37
add a comment |
i have not only these three value. There are above than 1000 values which i have in html drop down list.
– Sajjad Ali
Jan 1 at 11:01
are you getting duplicate values from database?
– rAnA bilAl
Jan 2 at 9:35
yes rana saab. it's answered above
– Sajjad Ali
Jan 2 at 9:37
i have not only these three value. There are above than 1000 values which i have in html drop down list.
– Sajjad Ali
Jan 1 at 11:01
i have not only these three value. There are above than 1000 values which i have in html drop down list.
– Sajjad Ali
Jan 1 at 11:01
are you getting duplicate values from database?
– rAnA bilAl
Jan 2 at 9:35
are you getting duplicate values from database?
– rAnA bilAl
Jan 2 at 9:35
yes rana saab. it's answered above
– Sajjad Ali
Jan 2 at 9:37
yes rana saab. it's answered above
– Sajjad Ali
Jan 2 at 9:37
add a comment |
I think you are doing it wrong way the correct way would be having a table which stored all the languages along with values
using selected attribute to achieve your objective
<?php
$result=mysqli_query($conn, "select * from profile where id=$firstPerson");
$queryArray1=mysqli_fetch_array($result);
$langOfUser=$queryArray1["language"];
?>
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from langtab");
while($queryArray=mysqli_fetch_array($result)){ ?>
<option value='<?php echo $queryArray["languageValue"];?> ' <?php if($langOfUser== $queryArray["languageValue"]){ echo 'selected';}?>> <?php echo $queryArray["languageName"]; ?></option>
<?php } ?>
</select>
add a comment |
I think you are doing it wrong way the correct way would be having a table which stored all the languages along with values
using selected attribute to achieve your objective
<?php
$result=mysqli_query($conn, "select * from profile where id=$firstPerson");
$queryArray1=mysqli_fetch_array($result);
$langOfUser=$queryArray1["language"];
?>
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from langtab");
while($queryArray=mysqli_fetch_array($result)){ ?>
<option value='<?php echo $queryArray["languageValue"];?> ' <?php if($langOfUser== $queryArray["languageValue"]){ echo 'selected';}?>> <?php echo $queryArray["languageName"]; ?></option>
<?php } ?>
</select>
add a comment |
I think you are doing it wrong way the correct way would be having a table which stored all the languages along with values
using selected attribute to achieve your objective
<?php
$result=mysqli_query($conn, "select * from profile where id=$firstPerson");
$queryArray1=mysqli_fetch_array($result);
$langOfUser=$queryArray1["language"];
?>
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from langtab");
while($queryArray=mysqli_fetch_array($result)){ ?>
<option value='<?php echo $queryArray["languageValue"];?> ' <?php if($langOfUser== $queryArray["languageValue"]){ echo 'selected';}?>> <?php echo $queryArray["languageName"]; ?></option>
<?php } ?>
</select>
I think you are doing it wrong way the correct way would be having a table which stored all the languages along with values
using selected attribute to achieve your objective
<?php
$result=mysqli_query($conn, "select * from profile where id=$firstPerson");
$queryArray1=mysqli_fetch_array($result);
$langOfUser=$queryArray1["language"];
?>
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from langtab");
while($queryArray=mysqli_fetch_array($result)){ ?>
<option value='<?php echo $queryArray["languageValue"];?> ' <?php if($langOfUser== $queryArray["languageValue"]){ echo 'selected';}?>> <?php echo $queryArray["languageName"]; ?></option>
<?php } ?>
</select>
answered Jan 1 at 11:02
Amitoj Singh AhujaAmitoj Singh Ahuja
32819
32819
add a comment |
add a comment |
You have to use if condition to display values in select option.
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){
if($queryArray["language"]!="Punjabi") {
$opval = "<option value=" . $queryArray["language"] . ">". $queryArray["language"]. " </option> "
echo $opval;
}
?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<option value="Urdu">Urdu</option>
</select>
i dont want to stop only "Punjabi" language, punjabi came from db and also placed in html dropdown and both are showing in html dropdown list, but i want only one "Punjabi" should show in dropdown
– Sajjad Ali
Jan 1 at 10:59
I have updated the logic. It might help you.
– Umar Abdullah
Jan 1 at 11:04
The logic will not add value in the select option due to if condition.
– Umar Abdullah
Jan 1 at 11:45
add a comment |
You have to use if condition to display values in select option.
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){
if($queryArray["language"]!="Punjabi") {
$opval = "<option value=" . $queryArray["language"] . ">". $queryArray["language"]. " </option> "
echo $opval;
}
?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<option value="Urdu">Urdu</option>
</select>
i dont want to stop only "Punjabi" language, punjabi came from db and also placed in html dropdown and both are showing in html dropdown list, but i want only one "Punjabi" should show in dropdown
– Sajjad Ali
Jan 1 at 10:59
I have updated the logic. It might help you.
– Umar Abdullah
Jan 1 at 11:04
The logic will not add value in the select option due to if condition.
– Umar Abdullah
Jan 1 at 11:45
add a comment |
You have to use if condition to display values in select option.
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){
if($queryArray["language"]!="Punjabi") {
$opval = "<option value=" . $queryArray["language"] . ">". $queryArray["language"]. " </option> "
echo $opval;
}
?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<option value="Urdu">Urdu</option>
</select>
You have to use if condition to display values in select option.
<select name="language" id="language" >
<?php $result=mysqli_query($conn, "select * from profile where id=$firstPerson");
while($queryArray=mysqli_fetch_array($result)){
if($queryArray["language"]!="Punjabi") {
$opval = "<option value=" . $queryArray["language"] . ">". $queryArray["language"]. " </option> "
echo $opval;
}
?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<option value="Urdu">Urdu</option>
</select>
edited Jan 1 at 11:02
answered Jan 1 at 10:50
Umar AbdullahUmar Abdullah
956418
956418
i dont want to stop only "Punjabi" language, punjabi came from db and also placed in html dropdown and both are showing in html dropdown list, but i want only one "Punjabi" should show in dropdown
– Sajjad Ali
Jan 1 at 10:59
I have updated the logic. It might help you.
– Umar Abdullah
Jan 1 at 11:04
The logic will not add value in the select option due to if condition.
– Umar Abdullah
Jan 1 at 11:45
add a comment |
i dont want to stop only "Punjabi" language, punjabi came from db and also placed in html dropdown and both are showing in html dropdown list, but i want only one "Punjabi" should show in dropdown
– Sajjad Ali
Jan 1 at 10:59
I have updated the logic. It might help you.
– Umar Abdullah
Jan 1 at 11:04
The logic will not add value in the select option due to if condition.
– Umar Abdullah
Jan 1 at 11:45
i dont want to stop only "Punjabi" language, punjabi came from db and also placed in html dropdown and both are showing in html dropdown list, but i want only one "Punjabi" should show in dropdown
– Sajjad Ali
Jan 1 at 10:59
i dont want to stop only "Punjabi" language, punjabi came from db and also placed in html dropdown and both are showing in html dropdown list, but i want only one "Punjabi" should show in dropdown
– Sajjad Ali
Jan 1 at 10:59
I have updated the logic. It might help you.
– Umar Abdullah
Jan 1 at 11:04
I have updated the logic. It might help you.
– Umar Abdullah
Jan 1 at 11:04
The logic will not add value in the select option due to if condition.
– Umar Abdullah
Jan 1 at 11:45
The logic will not add value in the select option due to if condition.
– Umar Abdullah
Jan 1 at 11:45
add a comment |
So your problem is that you have html hardcoded options and database options. You need to merge them into one on that website.
So you can use some javascript
elements = [1, 2, 9, 15].join(',')
$.post('post.php', {elements: elements})
But you can fill your elements like this is you don´t want to write it by hand
$("#id select").each(function()
{
allOptionsInSelect.push($(this).val());
});
Than on php side you can do
$elements = $_POST['elements'];
$elements = explode(',', $elements);
And now you have html hardcoded select on server side. Now you need to check if it doesn´t already exist when you are printing from database
You can do that like this
if(in_array(value_from_database, $elements) {
// It is so skip
} else {
// It is not, so print it
}
add a comment |
So your problem is that you have html hardcoded options and database options. You need to merge them into one on that website.
So you can use some javascript
elements = [1, 2, 9, 15].join(',')
$.post('post.php', {elements: elements})
But you can fill your elements like this is you don´t want to write it by hand
$("#id select").each(function()
{
allOptionsInSelect.push($(this).val());
});
Than on php side you can do
$elements = $_POST['elements'];
$elements = explode(',', $elements);
And now you have html hardcoded select on server side. Now you need to check if it doesn´t already exist when you are printing from database
You can do that like this
if(in_array(value_from_database, $elements) {
// It is so skip
} else {
// It is not, so print it
}
add a comment |
So your problem is that you have html hardcoded options and database options. You need to merge them into one on that website.
So you can use some javascript
elements = [1, 2, 9, 15].join(',')
$.post('post.php', {elements: elements})
But you can fill your elements like this is you don´t want to write it by hand
$("#id select").each(function()
{
allOptionsInSelect.push($(this).val());
});
Than on php side you can do
$elements = $_POST['elements'];
$elements = explode(',', $elements);
And now you have html hardcoded select on server side. Now you need to check if it doesn´t already exist when you are printing from database
You can do that like this
if(in_array(value_from_database, $elements) {
// It is so skip
} else {
// It is not, so print it
}
So your problem is that you have html hardcoded options and database options. You need to merge them into one on that website.
So you can use some javascript
elements = [1, 2, 9, 15].join(',')
$.post('post.php', {elements: elements})
But you can fill your elements like this is you don´t want to write it by hand
$("#id select").each(function()
{
allOptionsInSelect.push($(this).val());
});
Than on php side you can do
$elements = $_POST['elements'];
$elements = explode(',', $elements);
And now you have html hardcoded select on server side. Now you need to check if it doesn´t already exist when you are printing from database
You can do that like this
if(in_array(value_from_database, $elements) {
// It is so skip
} else {
// It is not, so print it
}
answered Jan 1 at 11:08
Games9999Games9999
62
62
add a comment |
add a comment |
You can use if elseif this way.
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?>'><?php echo $queryArray["language"]; ?></option>
<?php if ($queryArray["language"] == "Hindi") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } elseif ($queryArray["language"] == "Urdu") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<?php } elseif ($queryArray["language"] == "Punjabi") { ?>
<option value="Urdu">Urdu</option>
<option value="Hindi">Hindi</option>
<?php } ?>
the above code will always have three languages(Hindi, Punjabi and Urdu) in the dropdown list without repitition.
– Vishnu V
Jan 1 at 11:38
add a comment |
You can use if elseif this way.
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?>'><?php echo $queryArray["language"]; ?></option>
<?php if ($queryArray["language"] == "Hindi") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } elseif ($queryArray["language"] == "Urdu") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<?php } elseif ($queryArray["language"] == "Punjabi") { ?>
<option value="Urdu">Urdu</option>
<option value="Hindi">Hindi</option>
<?php } ?>
the above code will always have three languages(Hindi, Punjabi and Urdu) in the dropdown list without repitition.
– Vishnu V
Jan 1 at 11:38
add a comment |
You can use if elseif this way.
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?>'><?php echo $queryArray["language"]; ?></option>
<?php if ($queryArray["language"] == "Hindi") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } elseif ($queryArray["language"] == "Urdu") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<?php } elseif ($queryArray["language"] == "Punjabi") { ?>
<option value="Urdu">Urdu</option>
<option value="Hindi">Hindi</option>
<?php } ?>
You can use if elseif this way.
<select name="language" id="language" >
<option value='<?php echo $queryArray["language"];?>'><?php echo $queryArray["language"]; ?></option>
<?php if ($queryArray["language"] == "Hindi") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Urdu">Urdu</option>
<?php } elseif ($queryArray["language"] == "Urdu") { ?>
<option value="Punjabi">Punjabi</option>
<option value="Hindi">Hindi</option>
<?php } elseif ($queryArray["language"] == "Punjabi") { ?>
<option value="Urdu">Urdu</option>
<option value="Hindi">Hindi</option>
<?php } ?>
edited Jan 1 at 11:40
answered Jan 1 at 10:58
Vishnu VVishnu V
10217
10217
the above code will always have three languages(Hindi, Punjabi and Urdu) in the dropdown list without repitition.
– Vishnu V
Jan 1 at 11:38
add a comment |
the above code will always have three languages(Hindi, Punjabi and Urdu) in the dropdown list without repitition.
– Vishnu V
Jan 1 at 11:38
the above code will always have three languages(Hindi, Punjabi and Urdu) in the dropdown list without repitition.
– Vishnu V
Jan 1 at 11:38
the above code will always have three languages(Hindi, Punjabi and Urdu) in the dropdown list without repitition.
– Vishnu V
Jan 1 at 11:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994790%2fhow-to-avoid-repetition-of-values-in-dropdown-list-while-updating-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
are all languages present in database
– Amitoj Singh Ahuja
Jan 1 at 10:47
In the loop why is
<option value="Punjabi">Punjabi</option>present? thats causing the repetition i think– Hp_issei
Jan 1 at 10:53
and would
select distinct language from profile...not work? Also, why have theselectelement itself within the loop??– RamRaider
Jan 1 at 11:13
thankx to all. @Ali Sheikhpour answered this question.
– Sajjad Ali
Jan 1 at 11:25