Nick N
Nick N

Reputation: 3

PHP foreach not iterating first result

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/>";?>

Upvotes: 0

Views: 88

Answers (2)

Prix
Prix

Reputation: 19528

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.

Upvotes: 1

Buttle Butkus
Buttle Butkus

Reputation: 9486

Put query outside of the loop as the commenter Dagon said.

Use a query like this:

SELECT id, video_name, link, phase FROM videos WHERE id IN (?)

Then you can send a single query and get all your data at once.

Use $v = implode(',',$pieces); for the contents of IN ().

Using implode should never result in lost array elements.

Upvotes: 0

Related Questions