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 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄