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>";
}
}
Related
function chkQuestions($info)
{
$query = $this->handle->prepare("SELECT * FROM tbltest");
$query->execute();
$rows = $query->fetchAll();
$getPost = $query->rowCount();
foreach($rows as $row) {
for($i = 1; $i <= $getPost; $i++) {
$answerID[$i] = array($info['answer'.$i]);
$lo = array($answerID);
}
foreach($answerID as $question) {
if(array_key_exists($row['answer'],$question)) {
$test = "found";
return $test;
} else {
return $question;
}
}
}
}//chkQuestions
Above is my function, all works, but when I compare the row in my database called answer with the array it doesn't work?
I tried hard coding in the value and it works correctly but not with the array.
I checked to see the values of array and the value is indeed there.
In logic $answerID[$i] = array($info['answer'.$i]); = option1 and if the array value option1 = the database row answer which is set to option1 than return found, but it tells me it cannot find it.
array(1)
{
[0]=> string(7) "option1"
}
Above is the dum_var of the array $question. Any help?
foreach($questions as $question)
{
?>
<h3><?php echo $question['question'] ?></h3>
<div class = "col-xs-12">
<input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option1']?>" />
<label for="question-1-answers-A">A)<?php echo $question['option1'] ?> </label>
</div>
<div class = "col-xs-12">
<input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option2']?>" />
<label for="question-1-answers-B">B)<?php echo $question['option2'] ?></label>
</div>
<div class = "col-xs-12">
<input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option3']?>" />
<label for="question-1-answers-C">C)<?php echo $question['option3'] ?></label>
</div>
<div class = "col-xs-12">
<input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option4']?>" />
<label for="question-1-answers-D">D)<?php echo $question['option4'] ?></label>
<br>
<br>
</div>
<?php
}
echo "<input type='hidden' name='subaction' id='subaction' value='chkQuestion'> <button class='btn btn-primary' name='submit' type='submit'>Submit</button> </form>";
}
else
echo "No Quiz Found </form>";
?>
</div>
I don't have a lot of confidence in my answer because your coding method does a good job of confusing me. However, I feel like I need to get something on the page so we have some sort of solid base to work from:
function chkQuestions($info){
$check=array(); // this will hold the results to return
$stmt=$this->handle->prepare("SELECT `testID`,`answer` FROM tbltest");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
if($info['answer'][$row['testID']]==$row['answer']){
$check[$row['testID']]="correct";
}else{
$check[$row['testID']]="incorrect";
}
}
return $check; // handle this however you like on your main page
}
I have this code:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php if($l_id == '1'){ echo 'class="active"'; }?>><?php echo $l_name;?></li>
<?php } // closing 1st while loop
?>
</ul>
<div class="tab-content">
<?php
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
while($langs = mysqli_fetch_assoc($r)){
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php if($l_id == '1'){ echo 'in active'; }?>" id="<?php echo $l_name;?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang;?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang;?>" id="title_<?php echo $l_lang;?>" value="<?php echo $opened['title_'.$l_lang.'']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang;?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang;?>" id="header_<?php echo $l_lang;?>" value="<?php echo $opened['header_'.$l_lang.'']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php if($page_type == 'tour'){ echo 8;}else {echo 12;} ?>">
<label for="body_<?php echo $l_lang;?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang;?>" id="body_<?php echo $l_lang;?>" rows="7" placeholder="Page Body"><?php echo $opened['body_'.$l_lang.'']; ?></textarea>
</div>
</div>
<?php } //closing 2nd while loop
?>
</div>
When running it, the result is a tabbed form (I skipped the form tags and some html from above, to reduce the code writen) and everything is OK.
My questions are:
How to have the same output, but with a single query and while loop?
Is it possible to make this a function? Any hints?
Thank you!
I think you will have to loop twice, but you don't need to make two queries at all!
Use mysqli_fetch_all to store the results in an array and then loop through it
For example:
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$langs = mysqli_fetch_all($r);
foreach($langs as $lang){
//render links
}
//...
foreach($langs as $lang){
//render tabs
}
Your script will run much faster
Try
<?php
function get_rows()
{
$q = 'SELECT * FROM languages ORDER BY id DESC';
$r = mysqli_query($dbc, $q);
$languages = array();
while($langs = mysqli_fetch_assoc($r))
{
$languages[] = $langs;
}
return $languages;
}
$languages = get_rows();
if($languages != false)
{
?>
<ul>
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_name = $langs['name_en'];
?>
<li <?php
if($l_id == '1')
{
echo 'class="active"';
}
?>><?php echo $l_name; ?></li>
<?php
}
?>
</ul>
<div class="tab-content">
<?php
foreach($languages as $langs)
{
$l_id = $langs['id'];
$l_lang = $langs['language'];
$l_name = $langs['name_en'];
?>
<div class="tab-pane fade <?php
if($l_id == '1')
{
echo 'in active';
}
?>" id="<?php echo $l_name; ?>">
<div class="form-group col-xs-4">
<label for="title_<?php echo $l_lang; ?>">Title:</label>
<input class="form-control" type="text" name="title_<?php echo $l_lang; ?>" id="title_<?php echo $l_lang; ?>" value="<?php echo $opened['title_' . $l_lang . '']; ?>" placeholder="Page Title">
</div>
<div class="form-group col-xs-4">
<label for="header_<?php echo $l_lang; ?>">Header:</label>
<input class="form-control" type="text" name="header_<?php echo $l_lang; ?>" id="header_<?php echo $l_lang; ?>" value="<?php echo $opened['header_' . $l_lang . '']; ?>" placeholder="Page Header">
</div>
<div class="form-group col-xs-<?php
if($page_type == 'tour')
{
echo 8;
}
else
{
echo 12;
}
?>">
<label for="body_<?php echo $l_lang; ?>">Body:</label>
<textarea class="form-control editor" name="body_<?php echo $l_lang; ?>" id="body_<?php echo $l_lang; ?>" rows="7" placeholder="Page Body"><?php echo $opened['body_' . $l_lang . '']; ?></textarea>
</div>
</div>
<?php } ?>
</div>
<?php
}
?>
Save results while first iterating in some array and the iterate over this array for the second time:
$tmp_array = array();
while ($langs = mysqli_fetch_assoc($r)){
$tmp_array[] = $langs;
}
//some code here
//and second array
foreach ($tmp_array as $langs) {
//more code here
}
I have a form which has a list of checkboxes to be filled in on insert to database as well as when editing. When editing I am trying to populate the fields with a list of fields from the database which is in the $groups variable and the checked value being checked against the group_id from groups with the group_id from the $user. It is working except it only fills out one checkbox and some users belong to multiple groups. Any ideas, or more efficient ways to do this.
Heres my code so far
<?php foreach($groups as $group) : ?>
<?php foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
} else {
$checked = '';
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
This problem is that you are searching all the $users['group'] array and not stopping when you find a match, so unless last $uG['group_id'] matches $group['id'] you continue and therefore clear $checked after potentially setting it.
So just add a break when you find a match.
Also, officially the correct way of setting the checked status is checked="checked" although most modern browsers are not that pedantic, its possibly better to stick to the HTML spec
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = 'checked="checked"';
break;
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
you could also use a ternary operator and in_array() to do this all in one simple statement
<?php
foreach($groups as $group) :
$checked = in_array( $group['id'], $user['groups'] ) ? 'checked="checked"' : '';
?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
Define $checked = ''; first then loop and change the value if match found
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
I'm using a foreach to save in database my form datas.
And, sometimes some values are empty because it's the same as the last value entered. I would like (if the current iteration value is empty) to get the last iteration value and to print it.
Do you have a solution?
Thank you!
<?php
$html = file_get_contents("Intranet link");
function parseTable($html){
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = true;
$tables = $dom->getElementById('tblResultats');
$rows = $tables->getElementsByTagName('tr');
return $rows;
}
$rows = parseTable($html);
$i = 1;
$count = 0;
foreach ($rows as $row){
if($i <> 1 ){
$cols = $row->getElementsByTagName('td');
if (preg_match('/visite/i',$cols->item(0)->nodeValue)) {
$count = $count-1;
}else{
$cols->item(6)->nodeValue = str_replace(' ', '', $cols->item(6)->nodeValue);
$train_nbr = preg_replace("/[^0-9,.]/", "", $cols->item(6)->nodeValue);
if($train_nbr == ""){
$train_nbr = "00000";
}
?>
<div id="criter_<?php echo $count; ?>" class="panel <?php if($cols->item(4)->nodeValue == " -Z"){ echo "panel-z-series"; }elseif($cols->item(4)->nodeValue == " -X"){ echo "panel-x-series"; }elseif($cols->item(4)->nodeValue == " BB"){ echo "panel-bb-series"; }elseif($cols->item(4)->nodeValue == " -B"){ echo "panel-b-series"; } ?>">
<input type="hidden" name="trainid_<?php echo $count; ?>" value="<?php echo $train_nbr; ?>">
<input type="hidden" name="traintype_<?php echo $count; ?>" value="<?php echo $cols->item(4)->nodeValue; ?>">
<input type="hidden" name="userid_<?php echo $count; ?>" value="<?php echo $_SESSION["Auth"]["username"]; ?>">
<input type="hidden" name="entrydate_<?php echo $count; ?>" value="<?php echo date("d/m/Y"); ?> <?php echo $cols->item(2)->nodeValue; ?>">
<input type="hidden" name="leftdate_<?php echo $count; ?>" value="<?php echo $cols->item(8)->nodeValue; ?>">
<input type="hidden" name="openeddate_<?php echo $count; ?>" value="<?php echo date("d/m/Y H:i:s"); ?>">
<input type="hidden" name="entrynumber_<?php echo $count; ?>" value="<?php echo $cols->item(0)->nodeValue; ?>|<?php echo $cols->item(1)->nodeValue; ?>">
<input type="hidden" name="leftnumber_<?php echo $count; ?>" value="<?php echo $cols->item(7)->nodeValue; ?>|<?php echo $cols->item(9)->nodeValue; ?>">
<div class="panel-heading">
<button onclick='addValue();$("#criter_<?php echo $count; ?>").remove();' class="btn btn-default"><i class="fa fa-trash"></i></button>
<strong><?php echo $cols->item(2)->nodeValue; ?> </strong>
<div style="font-size:18px;margin-top:-30px"><?php if($cols->item(4)->nodeValue == " -Z"){ echo "<strong>Z</strong>"; }elseif($cols->item(4)->nodeValue == " -X"){ echo "<strong>X</strong>"; }elseif($cols->item(4)->nodeValue == " BB"){ echo "<strong>BB</strong>"; }elseif($cols->item(4)->nodeValue == " -B"){ echo "<strong>B</strong>"; } echo $train_nbr; ?></div>
<span class="pull-right">
<div class="input-group date picker" >
<input type="text" class="form-control" value="<?php echo $cols->item(8)->nodeValue; ?>"/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</span>
<br>
<!-- If values from current iteration are empty, get the values from the -1 iteration -->
Train numéro <strong><?php echo $cols->item(0)->nodeValue; ?></strong> en provenance de <strong><?php echo $cols->item(1)->nodeValue; ?></strong> et à destination de <strong><?php echo $cols->item(9)->nodeValue; ?></strong> (<strong><?php echo $cols->item(7)->nodeValue; ?></strong>)
</div>
<div class="panel-body">
<div id="room_criter_<?php echo $count; ?>">
<p>
Commentaire sur l'engin :<br>
<input type="text" style="width:100%;display: inline-block;margin-bottom:8px;" name="comments_<?php echo $count; ?>" placeholder="Commentaire éventuel sur cet engin (imprévu par exemple)" class="form-control">
Planification des événements :<br>
<input type="text" style="width:70%;display: inline-block;margin-bottom:8px;" name="eventcriter_<?php echo $count; ?>[]" placeholder="Evènement ou intervention planifiée sur cet engin" class="form-control">
<input type="hidden" name="lastmodified_<?php echo $count; ?>[]" value="<?php echo $_SESSION['Auth']['username']; ?>" class="form-control">
<select style="width:19%;display: inline-block;" class="form-control" name="eventcriterstatus_<?php echo $count; ?>[]">
<option value="0" selected>Non effectuée</option>
<option value="1">En cours</option>
<option value="2">Terminée</option>
</select>
<a style="display: inline-block;border-radius: 4px;" class="btn btn-success" onclick="add_fields(<?php echo $count; ?>);"><span class="fa fa-plus"></span> Ajouter</a>
</p>
</div>
</div>
</div>
<?php
}
}
$i++;
$count++;
}
?>
I'm not sure if I understood the task right, but here's my try.
for ($i = 2, $n = count($rows); $i < $n; ++$i) {
$row = $rows[$i - 1];
$prevRow = $rows[$i - 2];
$cols = $row->getElementsByTagName('td');
$prevCols = $prevRow->getElementsByTagName('td');
$values = [];
for ($j = 0; $j < ITEMS_COUNT; ++$j) {
$value = $cols->item($j)->nodeValue;
if (empty($value)) {
$value = $prevCols->item($j)->nodeValue;
}
$values[$j] = $value;
}
//And then you can use $values[$j] instead of $cols->item($j)->nodeValue.
}
I have set up a group of checkboxes. They are dynamic, so the number of checkboxes will be different dependent on the person using the site. The structure of how the checkboxes are created is:
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format[<?php echo $second_num; ?>]" id="notset-<?php echo $num; ?>" <?php if($field['playback_format'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format[<?php echo $second_num; ?>]" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format[<?php echo $second_num; ?>]" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format[<?php echo $second_num; ?>]" id="3d-<?php echo $num; ?>" <?php if($field['playback_format'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
My save function is:
$new = array();
for ( $i = 0; $i < $count; $i++ ) {
$new[$i]['playback_format'] = $playbackFormats[$i];
}
I've been reading up on this issue and it seems its because my input fields do not contain unique names. I'm trying to store the data into an array, so it would be ['playback_format'] => dvd,3d,bluray or something similar.
Right now its only storing the last checked value. Is there a way I can use a forloop or something to iterate over the checked values and push them into my array??
You can just get rid of the "$second_num" in each <input name="playback_format[]"/> html tag. This will put everything into an array for you once you submit the form. You can check this by adding this line to the page as a test.
<?php print_r($_REQUEST['playback_format']); ?>
Generally, You want to avoid any loop if they aren't required.
Hope that helps with what you are doing.
What is $second_num? Does it need to be a part of the input name?
You can get PHP to recognise the submitted values as an array if you do it this way:
<input name="playback_format[<?php echo $second_num; ?>][]">
Or if you don't need $second_num as part of the name, just:
<input name="playback_format[]">
$_POST['playback_format'] will then be an array containing all the selected options.
There is a section in the PHP docs specifically about this behaviour.
Checkboxes have all the same name in your example. Name it differently like :
<label for="plabackformat-holder-label">Format</label>
<div class=" playbackformat-holder-<?php echo $num; ?> playbackformat-holder">
<div class="playback-format-radio-buttons">
<label for="notset-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="notset" name="playback_format_notset" id="notset-<?php echo $num; ?>" <?php if($field['playback_format_notset'] == 'notset') { echo 'checked'; } ?>>None
</label>
<label for="dvd-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="dvd" name="playback_format_dvd" id="dvd-<?php echo $num; ?>" <?php if($field['playback_format_dvd'] == 'dvd') { echo 'checked'; } ?>>DVD
</label>
<label for="bluray-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="bluray" name="playback_format_bluray" id="bluray-<?php echo $num; ?>" <?php if($field['playback_format_bluray'] == 'bluray') { echo 'checked'; } ?>>Bluray
</label>
<label for="3d-<?php echo $num; ?>">
<input type="checkbox" class="playbackformat-holder-radiobutton" value="3d" name="playback_format_3d" id="3d-<?php echo $num; ?>" <?php if($field['playback_format_3d'] == '3d') { echo 'checked'; } ?>>3d
</label><br />
</div>
</div>
And Try this in PHP :
//WHen you want to see what is send in Post :
var_dump($_POST);
//So, for get result :
$tab = array();
foreach($_POST as $key =>$value){
$tab[$key] = $value;
//Display it
echo $key . "=" . $value;
}