下面的代碼應該從數據庫中接收傳入任務並將它們分成他們正在完成的狀態,如待辦事項,工作和完成,但由於某種原因,在我的第一個foreach循環啓動後,其他人foreach循環似乎沒有做任何事情?爲什麼我的第二個foreach循環在php文件中的第一個之後不起作用?
<?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>
你能澄清你的具體問題是什麼嗎? – mhatch
看來,我的第一個循環後,我的foreach循環不重複通過數組,或顯示我的數據庫數組爲空。我第一次回到同一個數組後,我怎樣才能使foreach循環? – Daniel