Click here to Skip to main content
15,069,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried to learn CodeIgniter framework using tutorial provided by website.
but when I tried to pass checkbox status values from View to controller, it was displayed following error msg.

Quote:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/Todos.php

Line Number: 29



When I trace that it is due to foreach loop initialization. Then I echo the

echo $completed_todos;
echo $completed_todos[0];
echo $completed_todos[1]; — No value;


and see whether it returns values. But it have only return single value and perhaps not as an array ( I have more than one completed todos.)

Can you please help me to solve that. That would be a great help to me.


Quote:
VIEW :index


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple CodeIgniter App - Todos</title>
<link rel="stylesheet"
href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>


<div class="container">

<?php echo form_open('todos/update_completed'); ?>

<h1>Todos</h1>

<div class="list-group">
<?php foreach ($todos as $todo) { ?>
<div class="list-group-item clearfix">
<?php
echo form_checkbox('completed', $todo->id, $todo->completed);
?>
<?php echo $todo->task; ?>

</div>

<?php } ?>
<button type="submit" class="btn btn-primary">Update</button>
<?php echo form_close(); ?>


</div><!-- /.container -->

</body>
</html>


Quote:
Todos controller code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Todos extends CI_Controller {

public function __construct() {
parent::__construct();

// Load the todo model to make it available
// to *all* of the controller's actions
$this->load->model('Todo_model');
$this->load->helper('form');
}

public function index()
{
// 1. Load the data:
$all_todos = $this->Todo_model->get_all_entries();
// 2. Make the data available to the view
$data = array();
$data['todos'] = $all_todos;
// 3. Render the view:
$this->load->view('todos/index', $data);
}

public function update_completed() {

$completed_todos = $this->input->post('completed');
echo $completed_todos;
foreach ($completed_todos as $todo_id) {
echo 'The todo with id = ' . $todo_id
. ' is marked as completed.<br>';
}
}



}


Thank you very much for the assistance.
Thank you.
Heshan

What I have tried:

When I trace that it is due to foreach loop initialization. Then I echo the

echo $completed_todos;
echo $completed_todos[0];
echo $completed_todos[1]; — No value;


and see whether it returns values. But it have only return single value and perhaps not as an array ( I have more than one completed todos.)

Can you please help me to solve that. That would be a great help to me.
Posted
Updated 22-Aug-18 15:23pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900