I managed to achieve specific database array order, now I'd like to print it in proper combination, but I have no idea how the 'foreach' loop should look like.
What I have now is:
$textfields = get_settings('test_testbutton');
if (!empty($textfields)) {
foreach ($textfields as $textfield) {
?>
<p>
<input type="text" id="<?php echo $value['id']; ?>" name="test_testbutton[0][]" value="<?php echo $textfield; ?>" placeholder="Input Value"/>
<input type="text" id="<?php echo $value['id']; ?>" name="test_testbutton[1][]" value="<?php echo $textfield; ?>" placeholder="Input Value"/>
Remove
</p>
<?php
}
} else {
}
I really don't know how to change 'echo $textfield' and make it work. I tried adding [] to '$textfields' value and then echo $textfield[] or $textfield[0], but with no success :(
I attached a .jpg file to make it more understandable.
attachment
After your update. It seems that you forget a foreach. I hope it's helpful to achieve what you want.
// Following your dump
$textfields = array( 0 => array( 1234, "qwer", "abcd"), 1 => array("5678", "tyui", "efgh"));
if (!empty($textfields)) {
foreach ($textfields as $textfield) {
// First loop : 0 => array( 1234, "qwer", "abcd")
// Second loop: 1 => array("5678", "tyui", "efgh")
foreach ($textfield as $oneValue) {
// Loop on the second array $textfield
}
}
The Text in the attached image is a serialized array, so if you want to use foreach on this text you have to unserialize it, then an array will be created
$arr = unserialize($textfields);
I think this will help you and it will work :)
$textfields_data = get_settings('test_testbutton');
if (!empty($textfields_data))
{
$textfields = unserialize($textfields_data);
$i = 0;
foreach ($textfields as $textfield) {
?>
<p>
<input type="text" id="" name="test_testbutton1_<?php echo $i; ?>" value="<?php echo $textfield[$i]; ?>" placeholder="Input Value"/>
<input type="text" id="" name="test_testbutton2_<?php echo $i; ?>" value="<?php echo $textfield[$i]; ?>" placeholder="Input Value"/>
Remove
</p>
<?php
$i++;
}
}
else
{
}
Related
I need to use nested foreach for dependent checkboxes.
<input type="checkbox" name="all[]" value="<?php echo $row_bus_details['busid'];?>" >
<?php
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
?>
<input type="checkbox" name="bookingside[]" value="<?php echo $book_side_row['advt_side_id']; ?>" id="<?php echo $book_side_row['advt_side']; ?><?php echo $row_bus_details['busid'];?>" > <?php echo $book_side_row['advt_side']; ?><br/>
<?php } ?>
I need to loop the selected values of second checkbox if the first checkbox is selected.
I wrote the code like
$i = 0;
$busid = isset($_POST['all']) ? $_POST['all'] : array();
foreach ((array)$busid as $item) {
if(!empty($_POST['bookingside'])) {
foreach($_POST['bookingside'] as $side) {
$sql_book_side=mysqli_query($db,"INSERT INTO `advt_book_side`(bus_id,sides_id) VALUES ('$item','$side')");
$i++;
}
}
}
The result I need is just like the image below
You need to save data in serialize array from in data base like:
$sql_book_side=mysqli_query($db,"INSERT INTO advt_book_side(bus_id,sides_id) VALUES ('$item',serialize(array('left'=>1,'right'=>1,'back'=>0)))");
Print check box with check uncheck using below code
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
$array = unserialize($book_side_row['sides_id']);
foreach($array[0] as $side){
?>
<input type="checkbox" name="bookingside[]" value="<?php echo ($side)? $side:0; ?>">
<?php }
} ?>
I got stuck here with the codeigniter form validation. Here is my code below.
$match_data = $this->input->post('match_data');
$this->load->library('form_validation');
foreach($match_data as $roundno => $round_data) {
foreach ($round_data as $roundtimes => $round_times_data) {
foreach ($round_times_data as $match_no => $match) {
foreach ($match as $games_times => $games) {
$this->form_validation->set_rules("match_data[".$roundno."][".$roundtimes."][".$match_no."][".$games_times."][score1]", "score1", "trim|xss_clean|numeric");
$this->form_validation->set_rules("match_data[".$roundno."][".$roundtimes."][".$match_no."][".$games_times."][score2]", "score2", "trim|xss_clean|numeric");
}
}
}
}
When i am submitting a,b,c or anything like this the validation check is fine. but once i put proper numeric value it still showing me error. Please help me to find out the issue? Here is my input filed code
<div class="row">
<div class="col-sm-6">
<input class="form-control" type="text" name="match_data[<?php echo $edit_round_num; ?>][<?php echo $matchtime; ?>][<?php echo $match; ?>][<?php echo $gametime+1; ?>][score1]" value="<?php echo $games_per_match_array_one[$gametime]; ?>" />
</div>
<div class="col-sm-6">
<input class="form-control" type="text" name="match_data[<?php echo $edit_round_num; ?>][<?php echo $matchtime; ?>][<?php echo $match; ?>][<?php echo $gametime+1; ?>][score2]" value="<?php echo $games_per_match_array_two[$gametime]; ?>" />
</div>
</div>
In case of validating numeric values validation trim|xss_clean doesn't work or needed. So once removed trim|xss_clean the code starts working.
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>";
}
}
$va_fields = array();
$c = 0;
$field_num = 'field-'.$c;
settype($c,"integer");
while (isset($_POST[$field_num])){
$posted_field = $_POST[$field_num];
$va_fields = array( $c => $posted_field);
echo $c.': ';
echo '$posted_field: '.$posted_field;
$c+=1;
$field_num = 'field-'.$c;
echo ' <br /> va_fields - c:'.$va_fields[$c];
echo ' <br /> ';
}
For some reason, I cannot for the life of me get the variable $posted_fields into an array.
The values of $posted_field are what I need them to be, so I'm getting the data from the post. Then I try to store them in an array, and check the array and they aren't there. What am I doing wrong?
Edit: here's my form:
<form method="post" action="<?php echo get_site_url(); ?>/wp-admin/admin-post.php">
<input type="hidden" name="action" value="cpt_field_opts" />
<!-- some inputs here ... -->
<?php wp_nonce_field( 'cpt_field_opts', '_wp_nonce' ); ?>
<input type="hidden" name="post_type" value="<?php echo $post_type; ?>">
<input type="hidden" name="origin" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
<?php
$c = 0;
foreach ($fields as $field){
?>
<input type="textarea" id="field-<?php echo $c; ?>" name="field-<?php echo $c; ?>" value="<?php echo $field; ?>" size="25" /></br>
<?php
$c += 1;
}
?>
<input type="submit" value="Submit" >
</form>
<form method="post" action="<?php echo get_site_url(); ?>/wp-admin/admin-post.php">
<?php wp_nonce_field( 'cpt_field_opts_new', '_wp_nonce_2' ); ?>
<input type="hidden" name="post_type" value="<?php echo $post_type; ?>" />
<input type="hidden" name="action" value="cpt_field_opts_new" />
<input type="hidden" name="origin" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
<input type="submit" value="New" />
</form>
Many people are telling me to rewrite the array as this line:
$va_fields[$c] = $posted_field;
Which I have done, but I also started out with that and it still wasn't working
If you have a bunch of values in $_POST with names like "field-0", "field-1", etc, and you're trying to get them into an array:
$c = 0;
while (isset($_POST["field-$c"])){
// This creates a new element in $va_fields with key=$c and value=$_POST["field-$c"]
$va_fields[$c] = $_POST["field-$c"];
}
var_dump($va_fields); // should show you the array you want
But really, it would be easier to just modify your form to use inputs with name=field[] instead of numbering them. Then you would already have the array in $_POST['fields'] without having to do anything.
It is simple just iterate the $_POST array
<?php
$va_fields = array();
$c=0;
foreach($_POST as $val){
echo '$posted_field: '. $val;
$va_fields[$c] = $val;
}
var_dump($va_fields);
?>
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;
}