doufan9377 2013-08-27 23:08
浏览 71
已采纳

PHP foreach不会迭代第一个结果

I am confused as to why the first result which is 0 is not being iterated. Could someone please explain why and show me what needs to be done to show [0]'s result?

Here is my Array:
array(3) { [0]=> string(3) "390" [1]=> string(3) "377" [2]=> string(3) "382" } 

Notice how [0] result is not shown via the foreach. The last two which are [1] and [2] show up fine.

You can see the results of this here: http://www.rotaryswing.com/swingviewer/videos.php

<?php 
//iterate through video IDS in our DB
foreach ($pieces as $key => $v) {

$sql ="SELECT id, video_name, link, phase FROM videos WHERE id=?";
    if ($stmt = $mysqli->prepare($sql)) {
        $stmt->bind_param("i", $v);

        if ($stmt->execute()) {
            $stmt->bind_result($id, $vid_name, $vid_link, $phase);
            while ($stmt->fetch()) {
                echo "<a style=\"font-size: 14px;\" href='http://www.rotaryswing.com/golf- instruction/video/rst-index.php?cat=$phase&subcat=Rotary%20Swing%20Tour&video=$id&id=$vid_link&name=$vid_name' target=\"blank\">$vid_name</a><br>";
            }
        }
        else {
            trigger_error("SQL query failed: " . $stmt->error, E_USER_ERROR);
        }
    }
}
?>

When I echo just the pieces it echos fine.

<?php echo $pieces[0] . "<br/>";?>

<?php echo $pieces[1] . "<br/>";?>

<?php echo $pieces[2] . "<br/>";?>
  • 写回答

2条回答 默认 最新

  • drlnwji79147769 2013-08-27 23:19
    关注

    You could use a WHERE IN with your array:

    # create the correct amount of ?
    $placeholder = implode(', ', array_fill(0, count($pieces), '?'));
    $sql ="SELECT id, video_name, link, phase FROM videos WHERE id IN ({$placeholder})";
    if ($stmt = $mysqli->prepare($sql))
    {
        # add at the begin the type of your array the field types
        array_unshift($pieces, implode('', array_fill(0, count($pieces), 'i')));
        # bind the field type and each value
        call_user_func_array(array($stmt, 'bind_param'), $pieces);
        if ($stmt->execute())
        {
            $stmt->bind_result($id, $vid_name, $vid_link, $phase);
            while ($stmt->fetch())
            {
    ?>
    <a style="font-size: 14px;" href="http://www.rotaryswing.com/golf-instruction/video/rst-index.php?cat=<?php echo $phase; ?>&subcat=Rotary%20Swing%20Tour&video=<?php echo $id; ?>&id=<?php echo $vid_link; ?>&name=<?php echo $vid_name; ?>" target="blank"><?php echo $vid_name; ?></a><br>
    <?php
            }
        }
        else
        {
            trigger_error("SQL query failed: " . $stmt->error, E_USER_ERROR);
        }
    }
    

    Using call_user_func_array(array($stmt, 'bind_param'), $pieces); we bind each field type and parameters to the bind_param.

    Using $placeholder = implode(', ', array_fill(0, count($pieces), '?')); we create string with the correct amount of placeholders that $pieces have, so if $pieces have 4 ids it will create a string like this ?, ?, ?, ? that we then append inside the query at IN ({$placeholder})

    Using array_unshift($pieces, implode('', array_fill(0, count($pieces), 'i'))); we create and append all the types as the first element of the array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)