mysqli_fetch_assoc只返回一次迭代的结果

时间:2019-04-13 13:47:18

标签: php

while循环内的mysqli_fetch_assoc,它位于for循环内,但仅对for循环的第一次迭代获得响应。

试图在控制台上打印它,但也没有响应。

    public void composeEmail() {

        String[] addresses=new String[1];
        addresses[0]="koen.pelsmaekers@coffee.com";

        String subject="Coffee Order";

        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:"));

        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, orderDetails);

        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(Intent.createChooser(intent, "Send Email"));
        }
    }

我应该在每个表格行中获得相同的选择选项。

1 个答案:

答案 0 :(得分:3)

这是mysqli_fetch_*方法的行为。它们遍历结果集,逐行获取,并且当没有行时,每次对mysqli_fetch_的结果调用都将不返回任何内容。

在您的情况下,应减少对相同行的迭代。可以通过以下方式完成:

// iterate over your results once and collect them to a string
$stdSubjectOptions = '';
while($sub_result = mysqli_fetch_assoc($stdsubject)){
    $stdSubjectOptions .= '<option value="'.$sub_result['id'].'">'.$sub_result['name'].'</option>';
}

// iterate over your results once and collect them to a string
$teachers = '';
while($teacher_result = mysqli_fetch_assoc($all_branch_teacher_raw)){
    $teachers .= '<script>console.log('.$teacher_result['id'].')</script>';
    $teachers .= '<option value="'.$teacher_result['id'].'">'.$teacher_result['first_name'].' '.$teacher_result['last_name'].'</option>';                         
} 

for($i=0; $i<$subject_count_result['subject_count']; $i++){
    echo '<tr><td></td><td><select name="subject_id[]" id="subject_id[]" class="form-control"><option value="select" >Select</option>';
    // just output previously formed string
    echo $stdSubjectOptions;    
    echo  '</select></td><td>';
    echo '<select name="teacher_id[]" id="teacher_id[]" class="form-control"><option value="select" >Select</option>';
    // just output previously formed string
    echo $teachers;
    echo '</select>';
    echo '</td><td></td></tr>';
}