为什么我的第二个foreach循环在php文件中的第一个之后无效?

时间:2016-07-21 18:08:08

标签: php arrays foreach

以下代码应该从数据库中接收传入的待办事项任务,并将它们分成完成状态,例如待办事项,工作和完成状态,但由于某种原因,在我的第一个foreach循环之后,它发射得很好,其他foreach循环似乎什么都不做?

<?php  
        require_once 'app/init.php';
        $itemQuery = $db->prepare("SELECT id, name, state FROM items WHERE user = :user");
        $itemQuery->execute(['user'=>$_SESSION['user_id']]);
        $items = $itemQuery->rowCount() ? $itemQuery : [];
    ?>

<!DOCTYPE html>
<html>
<head>
    <title>Todo</title>

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
    <div class="container_fluid">
        <div class="row">
            <h1 class="col-md-12 text-center">Todo</h1>
        </div>  
        <div class="row list_container">
            <div class="todo_column col-md-4">                      
                <ul>
                    <?php foreach($items as $item): ?>  
                        <?php if($item['state'] == 1): ?>   
                            <li>
                                <span><?php echo $item['name']; ?></span><br /> 
                                <button type="button" class="btn btn-danger">Delete</button>
                                <button type="button" class="btn btn-warning">-></button>       
                            </li>
                        <?php endif; ?>
                    <?php endforeach; ?>        
                </ul>
            </div>
            <div class="working_column col-md-4">
                <ul>
                    <?php foreach($items as $item): ?>  
                        <?php if($item['state'] == 2): ?>   
                            <li>
                                <span><?php echo $item['name']; ?></span><br /> 
                                <button type="button" class="btn btn-danger">Delete</button>
                                <button type="button" class="btn btn-warning">-></button>       
                            </li>
                        <?php endif; ?>
                    <?php endforeach; ?>        
                </ul>
            </div>
            <div class="finished_column col-md-4">
                <ul>
                    <?php foreach($items as $item): ?>  
                        <?php if($item['state'] == 3): ?>   
                            <li>
                                <span><?php echo $item['name']; ?></span><br /> 
                                <button type="button" class="btn btn-danger">Delete</button>
                                <button type="button" class="btn btn-warning">-></button>       
                            </li>
                        <?php endif; ?>
                    <?php endforeach; ?>        
                </ul>
            </div>
        </div><!-- end of list_container-->


        <div class="row">
            <form action="add.php" method="post" class="col-md-12 item_add">
                <input type="text" name="name" placeholder="Write your task" class="input col-md-8 col-md-offset-2" required>
                <button type="submit" value="add" class="btn btn-success col-md-4 col-md-offset-4">Add Task</button>


            </form>
        </div>

    </div> <!-- end of container -->    
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我以为您正在使用PDO。试试这个:

require_once 'app/init.php';
$query = "SELECT id, name, state FROM items WHERE user = :user";
$itemQuery = $db->prepare($query);
$itemQuery->execute(['user'=>$_SESSION['user_id']]);
$items = $itemQuery->fetchAll(PDO::FETCH_ASSOC);