How to get insertBatch() with checkbox in codeigniter 4 - php

How to get the checkbox data?
This is my view
<form method="post" action="<?= current_url();?>" >
<?php foreach($checklist as $item) : ?>
<ul>
<li>
<input type="hidden" name="name[]" value="<?= $item->name; ?>">
<div>
<label><?= $item->name; ?><label>
<input type="hidden" value="0" name="status=[]>
<input type="checkbox" value="1" name="status[]">
</div>
</li>
</ul>
<button type="submit">Submit</button>
</form>
this is my controller
public function checklist() {
if($this->request->getMethod() === 'post') {
$name = $this->request->getPost('nama');
$status = $this->request->getPost('status');
foreach($name as $key => $value) {
$checklist[] = [
'name' => $name[$key],
'status' => $status[$key]
];
}
$this->model->insertBatch($checklist);
return view('data',$data);
}
how to get the status if checked it get double array?

I assume this is a multi-item list with an item key. I assume $item['id'] is a unique key.
View
<?php
$checklist = [['id'=>1,'name'=>'Test name 1','status'=>1],
['id'=>2,'name'=>'Test name 2','status'=>0],
['id'=>3,'name'=>'Test name 3','status'=>1],
['id'=>4,'name'=>'Test name 4','status'=>0],
['id'=>5,'name'=>'Test name 5','status'=>0]];
?>
<pre>
<?=print_r($checklist)?>
</pre>
<form method="post" action="<?= current_url()?>" >
<ul>
<?php foreach($checklist as $item) : ?>
<li>
<input type="hidden" name="name[<?=$item['id']?>]" value="<?=$item['name']?>">
<div>
<label><?=$item['name']?><label>
<input type="checkbox" value="1" name="status[<?=$item['id']?>]"<?=$item['status']?' checked':''?>>
</div>
</li>
<?php endforeach;?>
</ul>
<button type="submit">Submit</button>
</form>
getPost
<pre>
<?php
if(isset($getPost))
print_r($getPost);
?>
</pre>
return
<pre>
<?php
if(isset($return))
print_r($return);
?>
</pre>
Controller
public function index()
{
$checklist = [];
if($this->request->getPost()) {
$getPost = $this->request->getPost();
$status = $this->request->getPost('status');
foreach($getPost['name'] as $key => $value) {
$checklist['return'][] = [
'name' => $value,
'status' => isset($status[$key])?1:0];
}
$checklist['getPost'] = $this->request->getPost();
// $this->Model->insertBatch($checklist);
}
return view('test',$checklist);
}
The final result image

Related

convert array object from database to string

I'm currently coding a blog to get experience with php.
In the edit.php I want to give the category of a post if a post has one.
This is the edit.php:
<div class="categories-heading">
<?php if (!empty($entry->c_Id)): ?>
<div class="category-heading">
<h3>Category: <?php echo e($categoryFromId); ?></h3>
</div>
<div class="category-heading">
<h3>change category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
</div>
</form>
<?php else: ?>
<div class="category-heading">
<h3>choose category</h3>
</div>
</div>
<form method="post">
<div class="categories-post-edit">
<?php foreach($categories as $cat): ?>
<div class="category-post-edit">
<input type="radio" name="category" value="<?php echo e($cat->id); ?>">
<label> <?php echo e($cat->category); ?></label>
</input>
</div>
<?php endforeach; ?>
This is the part of the function edit() in the PostsAdminController.php which is relevant for this:
if(!empty($_POST['title']) AND !empty($_POST['subheading']) AND
!empty($_POST['content'])
AND !empty($_POST['category'])) {
$entry->title = $_POST['title'];
$entry->subheading = $_POST['subheading'];
$entry->content = $_POST['content'];
$entry->c_Id = $_POST['category'];
$this->postsRepository->update($entry);
$savedSuccess = true;
}
$categoryFId = $this->categoryRepository->oneCategory($entry->c_Id);
$categoryFromId = $categoryFId['category'];
var_dump($categoryFromId);
$this->render("post/admin/edit", [
'entry' => $entry,
'savedSuccess' => $savedSuccess,
'categories' => $categories,
'categoryFromId' => $categoryFromId
]);
}
And this is the function oneCategory in the CategoryRepository.php that interacts with the database.
public function oneCategory($id)
{
$table = $this->getTableName();
$model = $this->getModelName();
$stmt = $this->pdo->prepare("SELECT `category` FROM `$table`
WHERE id = :id");
$stmt->setFetchMode(PDO::FETCH_CLASS, $model);
$oneCategory = $stmt->fetch(PDO::FETCH_CLASS);
return $oneCategory;
}
oneCategory() gives out an array no matter if I put SELECT * or SELECT category in the query, that's why I put $categoryFId['category'] into a variable after.
It works, but I think there must be a more easy or quicker way (like special functions or so), I just didn't manage to find what I search for

How to get value post array in codeigniter?

How to get value post array in codeigniter?
I have problem when I get value post array and echo the value. How to show post value when submit?
here the error message:
A PHP Error was encountered
Severity: Notice
Message: Uninitialized string offset: 0
Filename: controllers/blablabla
view html:
<?php $i=0; foreach ($doc as $row) { ?>
<label>
<input name="size[<?php echo $i; ?>]" type="checkbox" value="<?php echo $row['doc']; ?>"> <?php echo $row['doc']; ?>
</label>
<?php $i++; } ?>
controller :
$size = $this->input->post('size');
for ($i=0; $i<count($doc); $i++)
{
echo $size[$i];
}
Change the way name of checkbox written as follows,
<?php foreach ($doc as $row) { ?>
<label>
<input name="size[]" type="checkbox" value="<?php echo $row['doc']; ?
>"> <?php echo $row['doc']; ?>
</label>
<?php } ?>
And in post method,
$size_arr = $this->input->post('size');
foreach($size_arr as $v){
echo $v;
}
if for some reason it is not working then check with,
$size_arr = $_POST['size'];
foreach($size_arr as $v){
echo $v;
}
EDIT
One more alternative,
$arr = $this->input->post();
$size_arr = $arr['size'];
foreach($size_arr as $v){
echo $v;
}
Core version,
$arr = $_POST;
$size_arr = $arr['size'];
foreach($size_arr as $v){
echo $v;
}
Your html form code should be like below.
<input name="size[<?php echo $i; ?>]" type="checkbox" value="<?php echo $row['doc']; ?>">
Inside controller your code should be like below.
$size = $this->input->post('size');
foreach($size as $sa)
{
echo $sa;
}
No need to use $i in checkbox name in view file just take an array
View file
<?php foreach ($doc as $row) { ?>
<label>
<input name="size[]" type="checkbox" value="<?php echo $row['doc']; ?>"> <?php echo $row['doc']; ?>
</label>
<?php } ?>
Controller
$countsize = count($this->input->post('size'));
for ($i=0; $i<$countsize ; $i++)
{
echo $this->input->post('size')[$i];
}
This one works for me
In View file
<div id="area_input">
<div id="inputan" class="form-inline">
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
<div class="form-group col-sm-6">
<input type="text" class="form-control" name="size[]" placeholder="ukuran">
</div>
</div>
</div>
you can repeat input as needed.
in Controller file
$data = array(
'size' => $this->input->post('size'),
);
You can check stucture of array using print_r($data), or print 'em using:
foreach ($data as $key => $value) {
foreach ($value as $detail) {
echo $detail;
echo "<br>";
}
}

How to create a list of html radio buttons from php array?

I have following code (it's piece of bigger code):
<?php
include_once 'init/init.funcs.php';
$x = $_SESSION['answering']['index'];
echo $_SESSION['answering']['questions'][$x-1];
$result4 = mysql_query('SELECT kysimus_id FROM katse_kysimused
where kysimus= "' .$_SESSION['answering']['questions'][$x] . '"');
$question_id = mysql_result($result4, 0);
$result5 = mysql_query('SELECT * from katse_valik_vastused
where kysimus_id="'. $question_id . '"');
if($result5 === FALSE) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
//foreach($options as $option=>$option_value) {
//echo $option_value;
$count=count($options);
?>
<html>
<br>
<form method="post" action="answering.php">
<input type="radio" name="1"><?php echo $options[0]?><br>
<input type="radio" name="2"><?php echo $options[1]?><br>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Right now there are two fixed radio buttons. But I want it to have as many buttons, as many elements are in array "options" and each of them to have a value of one element written next to it. How could I do it?
Use a for loop for this: http://www.php.net/manual/en/control-structures.for.php
for ($i = 1; $i < count($options); $i++) {
?>
<input type="radio" name="<?php echo $i; ?>"><?php echo $options[$i]?><br>
<?php
}
Try like this:
<?php
while($row = mysql_fetch_assoc($result5)) {
$options[] = $row['vasuts'];
}
?>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach($options as $option=>$option_value) {
?>
<input type="radio" name="<?= $option; ?>"><?php echo $option_value?><br>
<?php }?>
<input name= "submit" type="submit" value="Vasta">
</form>
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $index=>$option) {
echo "<input type='radio' name='{$index}'>{$option}<br>";
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
Try using the below code.
<html>
<br>
<form method="post" action="answering.php">
<?php
foreach ($options as $key => $value) {
?>
<input type="radio" name="<?php echo $key; ?>"><?php echo $options[$key] ?><br>
<?php
}
?>
<input name= "submit" type="submit" value="Vasta">
</form>
</html>
you can do this using for each, like this:
<form method="post" action="answering.php">
<?php foreach ($options as $key => $value): ?>
<input type="radio" name="<?php echo $key; ?>" /><?php echo $value; ?><br />
<?php endforeach; ?>
<input name= "submit" type="submit" value="Vasta">
</form>

Move array element up one place PHP

I need help figuring out how to move a array item up one place using PHP. The PHP so far is as follows:
<?php
$task_list = array();
$task_list[] = 'Write chapter';
$task_list[] = 'Edit chapter';
$task_list[] = 'Proofread chapter';
switch( $_POST['action'] ) {
case 'Promote Task':
//This is where I'm stuck.
?>
and the HTML:
<?php if (count($task_list) > 0 && empty($task_to_modify)) : ?>
<h2>Select Task:</h2>
<form action="." method="post" >
<?php foreach( $task_list as $task ) : ?>
<input type="hidden" name="tasklist[]" value="<?php echo $task; ?>"/>
<?php endforeach; ?>
<label>Task:</label>
<select name="taskid">
<?php foreach( $task_list as $id => $task ) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<br />
<label> </label>
<input type="submit" name="action" value="Modify Task"/>
<input type="submit" name="action" value="Promote Task"/>
<input type="submit" name="action" value="Delete Task"/>
Any help would be much appreciated!
You only need to swap two rows (the selected task, and the task above). So if your indexes always are 0, 1, 2, 3, .., n:
$selectedRow = $_POST['taskid'];
if ($selectedRow === 0) {
echo 'Already top-priority';
} else {
$rowAbove = $taks_list[$selectedRow - 1];
$task_list[$selectedRow - 1] = $taks_list[$selectedRow];
$task_list[$selectedRow] = $rowAbove;
// Or without help-variable, but less readable
list($task_list[$selectedRow - 1], $task_list[$selectedRow])
= array($task_list[$selectedRow], $task_list[$selectedRow - 1]);
}

Checkbox array inside foreach form. Associate each checkbox with another variable

For the below example everything works as expected when ALL the checkboxes are checked. The problem occurs when one or more (but NOT all of them) are checked.
<form action="someaction" method="post">
<?php foreach ($fields as $field) { ?>
<input type="checkbox" name="checkpid[]" value="<?php echo $field['pid']; ?>">
<input type="hidden" name="checkprice[]" value="<?php echo $field['price']; ?>">
<input type="submit" name="submit" value="Submit">
<?php } ?>
</form>
<?php if (isset($_POST['checkpid'])) { ?>
<?php
$checkpid = $_POST['checkpid'];
$checkprice = $_POST['checkprice'];
?>
<?php foreach ($checkpid as $key => $checkpid) { ?>
<?php
$eachpid[] = $checkpid.",".$checkprice[$key];
?>
<?php } ?>
<?php print_r($eachpid), ?> // the $checkpid is always as expected, but the $checkprice does not match its row.
<?php } ?>
With my little knowledge I suspect it is something wrong in the declaration of the $key, but I am overwhelmed.
this is all i can suggest.
instead of adding two input how about adding the two data in value of a checkbox input separated with |. then when submitted just explode the value and receive array 1 for id and 1 for price.
<?php
if (isset($_POST['checkp'])) {
$checkp = $_POST['checkp'];
foreach ($checkp as $check) {
$c = explode("|", $check);
$eachpid[] = $c[0].",".$c[1];
}
print_r($eachpid);
}
?>
<form action="" method="post">
<?php foreach ($fields as $field) { ?>
<input type="checkbox" name="checkp[]" value="<?php echo $field['pid']; ?>|<?php echo $field['price']; ?>">
<?php } ?>
<input type="submit" name="submit" value="Submit">
</form>

Categories