how to avoid repetition of values in dropdown list while updating in php












2















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










share|improve this question

























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
















2















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










share|improve this question

























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














2












2








2


1






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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















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

















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












6 Answers
6






active

oldest

votes


















1














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();
})





share|improve this answer


























  • 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



















0














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







share|improve this answer
























  • 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



















0














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>





share|improve this answer































    0














    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>





    share|improve this answer


























    • 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



















    0














    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
    }





    share|improve this answer































      0














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







      share|improve this answer


























      • 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













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


      }
      });














      draft saved

      draft discarded


















      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









      1














      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();
      })





      share|improve this answer


























      • 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
















      1














      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();
      })





      share|improve this answer


























      • 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














      1












      1








      1







      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();
      })





      share|improve this answer















      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();
      })






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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



















      • 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













      0














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







      share|improve this answer
























      • 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
















      0














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







      share|improve this answer
























      • 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














      0












      0








      0







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







      share|improve this answer













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








      share|improve this answer












      share|improve this answer



      share|improve this answer










      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



















      • 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











      0














      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>





      share|improve this answer




























        0














        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>





        share|improve this answer


























          0












          0








          0







          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>





          share|improve this answer













          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>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 11:02









          Amitoj Singh AhujaAmitoj Singh Ahuja

          32819




          32819























              0














              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>





              share|improve this answer


























              • 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
















              0














              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>





              share|improve this answer


























              • 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














              0












              0








              0







              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>





              share|improve this answer















              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>






              share|improve this answer














              share|improve this answer



              share|improve this answer








              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



















              • 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











              0














              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
              }





              share|improve this answer




























                0














                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
                }





                share|improve this answer


























                  0












                  0








                  0







                  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
                  }





                  share|improve this answer













                  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
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 11:08









                  Games9999Games9999

                  62




                  62























                      0














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







                      share|improve this answer


























                      • 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


















                      0














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







                      share|improve this answer


























                      • 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
















                      0












                      0








                      0







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







                      share|improve this answer















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








                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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





















                      • 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




















                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'