简体   繁体   English

下拉列表多次显示值

[英]drop-down list showing values multiple times

I have a array in php - array and I am using the follwing code to create a drop-down list for this array - 我在php中有一个数组- 数组,并且正在使用以下代码为该数组创建一个下拉列表-

<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<select name="feature" id="Feature">
        <?php
        $i=0;
        foreach($newFeature as $feat)
        {
            ?>
        <option value="<?php echo $feat;?>"><?php echo $newFeature[$i];?></option>
            <?php
        $i++;
       }
         ?>
</select> <input type="submit" name="submit" value="Test">
</form>
</body>
</html>

The drop-down list contains values twice. 下拉列表包含两次值。 Here after traversing the array once and showing the values, code again traverse the value and show them again. 这里,在遍历数组一次并显示值之后,代码再次遍历该值并再次显示它们。

what I am doing wrong here ? 我在这里做错了什么? please guide. 请指导。

In my hopinion, you have a little bit confused the foreach with the for loop. 在我看来,您对foreach和for循环有些困惑。

In you case, saying $newFeature[$i] or $feat is actually refering to the same thing. 在您的情况下,说$ newFeature [$ i]或$ feat实际上是指同一件事。

Therefore, try this: 因此,请尝试以下操作:

<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<select name="feature" id="Feature">
        <?php
        foreach($newFeature as $feat)
        {
            ?>
        <option value="<?php echo $feat;?>"><?php echo $feat;?></option>
            <?php
       }
         ?>
</select> <input type="submit" name="submit" value="Test">
</form>
</body>
</html>

Show us the $newFeature array. 向我们展示$ newFeature数组。 Also, why do you need the $i variable? 另外,为什么需要$ i变量?

$newFeature[$i]

Here is a more cleaner way to generate the list, also you are using for each in a wrong way. 这是一种生成列表的更简洁的方法,对于每个列表,您都使用了错误的方法。

<form method="post" action="test.php">
    <select name="feature" id="Feature">
    <?php foreach($newFeature as $feat): ?>
        <option value="<?php echo $feat; ?>"><?php echo $feat; ?></option>
    <?php endforeach; ?>
    </select> 
    <input type="submit" name="submit" value="Test">            
</form>

if you are using foreach no need for counting and you should use $feat instead 如果您使用的是foreach无需计数,而应使用$feat代替

<?php
foreach($newFeature as $feat)
{
    ?>
<option value="<?php echo $feat;?>"><?php echo $feat;?></option>
    <?php
   }
?>

Using $newFeature[$i] is a wrong way of using foreach loop. 使用$ newFeature [$ i]是使用foreach循环的错误方法。 please post a snapshot of the array or try the following. 请发布阵列快照或尝试以下操作。

To debug and rectify the issue 调试并纠正问题

  1. Print the $newFeatures array before using the foreach and debug the code. 使用foreach之前,请打印$ newFeatures数组并调试代码。

    Or 要么

2.Add the following line in front of the foreach loop 2.在foreach循环的前面添加以下行

<?php $newFeature = array_unique($newFeature); ?>

This way might solve your problem. 这样可以解决您的问题。 I still recommend you to use the first suggestion and debug your code. 我仍然建议您使用第一个建议并调试您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM