简体   繁体   English

While循环仅执行一次

[英]While loop only executing once

I have a form that displays contact email addresses in text boxes, and drop down selections for titles for those corresponding emails. 我有一个在文本框中显示联系人电子邮件地址的表格,并为这些相应电子邮件的标题下拉选择。

TitleSelect1 Email1 TitleSelect2 Email2 .... TitleSelect1电子邮件1 TitleSelect2电子邮件2 ....

the emails have a title_id set, and the select list selects the existing Title. 电子邮件中设置了title_id,然后选择列表选择现有的Title。 This works fine for the first contact, but the rest will only display the email address, the title dropdown is empty. 这对于第一个联系人来说效果很好,但其余联系人仅显示电子邮件地址,标题下拉列表为空。

<?php

while ($row2 = mysql_fetch_array($Contact_list)) {
    ?>
<tr>
    <td align="right"><select id="Contacts_title" name="Contacts_title[]">
        <?

        while ($row3 = mysql_fetch_array($title_list)) { //put the contact titles into an array

            if ($row3['title_id'] == $row2['title_id']) {
                ?>
                <option value="<? echo $row3['title_id'] ?>" selected="true"><? echo $row3['title'] ?>!</option>';
                <?
            }

            else {
                ?>
                <option value="<? echo $row3['title_id'] ?>"><? echo $row3['title'] ?> </option>';
                <?
            }
        }

        ?>
    </select>
    </td>
    <td align="left"><input type="text" id="Contacts" name="Contacts[]" value="<? echo $row2['email'] ?>"/></td>
</tr>
<?
    $count++;
} 

mysql_fetch_array() only goes through the results of a given query once. mysql_fetch_array()只浏览一次给定查询的结果。 Since you're not executing the title_list query every time though your contact list loop, it's still at the end of the query results when you start every iteration after the first. 由于您不是通过联系人列表循环每次都执行title_list查询,因此在第一次迭代之后开始每次迭代时,查询结果仍在查询结果的末尾。

What you need to do is take all the results from your title_list query, put them into an array, and iterate over that for each of your contacts. 你需要做的就是采取一切从title_list查询的结果,把它们放到一个数组,并逐一为每个联系人。


Here's a general example of how you'd do this (with extraneous bits trimmed out): 这是一个有关如何执行此操作的一般示例(已修剪掉多余的位):

$contact_list = //some query
$title_list = //some query

$titles = array();
while($title_ar = mysql_fetch_array($title_list)) {
  $titles[] = $title_ar;
}

while($contact_ar = mysql_fetch_array($contact_list)) {
    // some stuff
    foreach($titles as $title) {
        // do something with each title
    }
}

您应将行放入变量中,并在while循环后回显。

once the inner while loop executes once fully, you wont get any more values in that mysql_fetch_array call again. 一旦内部while循环完全执行一次,您就不会再在该mysql_fetch_array调用中获得任何更多的值。 Since the cursor reached the end of rows. 由于光标到达了行尾。

You should store the results of the inner while loop into a php array and then iterate through this array every time within the inner while loop. 您应该将内部while循环的结果存储到一个php数组中,然后每次在内部while循环内遍历该数组。

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

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