I'm working on a website wherein rows are generated depending on how many users there are. In this example, I have three users. Basically, I pass data through $_POST using drop down select data. Here's what I'm passing to PHP. These are wrapped in <form> but I cleaned it to show just the important data.
...
<select name="taction[3]" >
<option value="accept">Accept</option>
<select name="taction[4]" >
<option value="accept">Accept</option>
<select name="taction[6]" >
<option value="accept">Accept</option>
...
My php looks like this:
$total = 1;
foreach ($_POST['taction'] as $userid => $action)
{
if ($action == "accept")
{
if ($total<1)
{
break;
}
else
{
echo $userid."foo";
$total = ($total - 1);
}
}
}
For some reason, it is still displaying three "foo's" when it should've stopped after the first "foo". What am I doing wrong?
Change it to
if($total <= 1)
Or start the $total variable at 0.
Thanks for your suggestions. I was at fault by relying on variables outside the foreach statement. I had to copy the operations inside the loop again for it to register the added data.
Related
I am trying to add a option value for select dropdown in CF7. I am using following shortcode to generate HTML like
<select name="Construction" class="form-control">
<option value="opt1">Masonry</option>
<option value="opt2">Frame</option>
<option value="opt3">Mixed Masonry-Frame</option>
</select>
My Shortcode is:
[select* Construction class:form-control "Masonry|opt1" "Frame|opt2" "Mixed Masonry-Frame|opt3"]
But All I got is:
<select name="Construction" class="form-control">
<option value="Masonry">Masonry</option>
<option value="Frame">Frame</option>
<option value="Mixed Masonry-Frame">Mixed Masonry-Frame</option>
</select>
I just followed the patterns used in https://contactform7.com/selectable-recipient-with-pipes/
Note : WPCF7_USE_PIPE was set true.
You might not need it anymore, but I came across the same problem today.
I solved it by filtering wpcf7_form_tag.
In my opinion a better solution than using JS because the values will be changed server-side before any form HTML is rendered.
Example implementation which should make the pipes work the way you want:
function so48515097_cf7_select_values($tag)
{
if ($tag['basetype'] != 'select') {
return $tag;
}
$values = [];
$labels = [];
foreach ($tag['raw_values'] as $raw_value) {
$raw_value_parts = explode('|', $raw_value);
if (count($raw_value_parts) >= 2) {
$values[] = $raw_value_parts[1];
$labels[] = $raw_value_parts[0];
} else {
$values[] = $raw_value;
$labels[] = $raw_value;
}
}
$tag['values'] = $values;
$tag['labels'] = $labels;
// Optional but recommended:
// Display labels in mails instead of values
// You can still use values using [_raw_tag] instead of [tag]
$reversed_raw_values = array_map(function ($raw_value) {
$raw_value_parts = explode('|', $raw_value);
return implode('|', array_reverse($raw_value_parts));
}, $tag['raw_values']);
$tag['pipes'] = new \WPCF7_Pipes($reversed_raw_values);
return $tag;
}
add_filter('wpcf7_form_tag', 'so48515097_cf7_select_values', 10);
Edit:
In the backend, the [tag] will be replaced by the value, not the label. But if you still want to have the label displayed in the e-mails instead, then that is also possible by recreating (reversing) the CF7 pipes. That way, you can actually choose which one to use. [tag] will display the label and [_raw_tag] will display the value.
I have edited the code above to reflect this. It is optional of course.
This will work server side only. According to the documentation this is to prevent values to be sent to the browser client.
I found this comment by Herbert Van-Vliet on this question
So, On submit you will get exact value which you have defined through Pipe Operator ("|")
I've got an array of users from a database (I'm working in PHP, using CodeIgniter in Sublime)
I've got a view, that has a select that show display all the users, so in my view, at the top, I have this code (suppose that the arrays has 3 items):
<?php
$optionsUsers = array();
$qtyUsers = count($users); -->
if($qtyUsers > 0){
$optionsUsers[0]['name'] = 'Choose an option';
for($i=0; $i < $qtyUsers; $i++){
$optionsUsers[$i]['id'] = $users[$i]["userId"];
$optionsUsers[$i]['name'] = $users[$i]["username"];
}
}
?>
Then, in the select part, I have this:
<select id="cursadaUsuario">
<?php
$qtyOptionsUsers = count($optionsUsers);
if($qtyOptionsUsers>0){
for($i=0; $i<$qtyOptionsUsers; $i++){
if($i == 0){
echo '<option value="0" disabled selected>Please select an option</option>';
}else{
echo '<option value="'.$optionsUsers[$i]['id'].'">'.$optionsUsers[$i]['name'].'</option>';
}
}
}else{
echo '<option value="">There are no options available</option>';
}
?>
</select>
I've assigned to the $optionsUsers array in [0] the string "Choose an option" because when iterating THAT ARRAY in the select, I wanted to display it as disabled and just iterate the rest of the elements as usual
The problem is that array $users starts in 0 --> I've checked it with a foreach and all the users display are actually there:
foreach ($users as $key => $value){
print_r($users);
});
But I wanted [0] to be the text to display, "Choose your option", so if I assign the text to [0], the first item in array $users is never shown :( (in this example, it would iterate the text and 2 of the items, not the 3)
If I remove the text to [O], and just iterate the array, all the users are shown correctly, no one is missing, but that means... no text to tell "Choose an option" :/
I would like it to look like this --> http://www.hkvstore.com/phpreportmaker/doc/images/dropdownselect.png, so I want the text "Choose you option" to be shown AND disabled, and then the elements of the array, in my case, a list of users.
Note: I would like to keep using a 'for' loop.
Note2: This (Add option selected disabled in PHP) would be kind of similar to what I want to achieve, but still, I don't think I could just change the keys and assign my own to a list of users, the could be changed, added, deleted, etc.. :/
Ideas? Thanks in advance! :)
Unless I'm not understanding you, it's the same as the "No options available" option – it belongs outside the loop:
if ($qtyOptionsUsers > 0) {
echo '<option value="0" disabled selected>Please select an option</option>';
for ($i = 0; $i < $qtyOptionsUsers; $i++) {
echo '<option value="'.$optionsUsers[$i]['id'].'">'.$optionsUsers[$i]['name'].'</option>';
}
} else {
echo '<option value="">There are no options available</option>';
}
you need to push the users data from index 1 rather than from 0 index.Try below code to fix the choose option
for($i=0; $i <= $qtyUsers; $i++){
$optionsUsers[$i+1]['id'] = $users[$i]["userId"];
$optionsUsers[$i+1]['name'] = $users[$i]["username"];
}
Hi i get the value from post i used serialize function its showing wrong value. Its just displaying N.. pls help me
Php Code
$item_select_alpha = $_POST['item_select_alpha'];
for ($alpha = 1; $alpha <= count($item_select_alpha); $alpha++) {
$serialise = serialize(array($item_select_alpha[$alpha]));
}
$item_quanity = $_POST['item_quanity'];
for ($qty = 1; $qty <= sizeof($item_quanity); $qty++) {
$item_quan = serialize($item_quanity[$qty]);
}
print_r($item_quan);
exit;
HTML Code
<select class="item_select_alpha" name="item_select_alpha[]">
<option value="">select the Alphabetic</option>
{foreach $size_alpha as $sa}
<option value="{$sa['size_id']}">{$sa['size_name']}</option>
{/foreach}
</select>
<input type="text" class="item_quanity" name="item_quanity[]" class="form-control">
You not add items to an array, but only changes the variable so it contains the last element. Try this:
$serialise[] = serialize(array($item_select_alpha[$alpha]));
$item_quan[] = serialize($item_quanity[$qty]);
your variables in for loops will contains just the last element, you update their value at each iteration. $item_quan defined in the second loop won't be "printable" out of the loop...
I am a beginner trying to ouptut the values of dropdown menu from a database. I already automatically generate the values from database but the problem i am having is I want the the value that is being selected to be displayed.
Ideally im trying to update a Subject which I want to data to be displayed in an HTML page where each item could be updated. What i need is to be able to select the 'position' of the subject that is beeing selected.
Here is my code:
<p>Position:
<select name="position">
<?php
$sel_subject = get_all_subjects();
$subject_count = mysql_num_rows($sel_subject);
//$subject_count+1 because we are adding a subject
for($count=1; $count <= $subject_count+1; $count++) {
echo "<option value=\"{$count}\"";
if($sel_subject['position'] == $count) {
echo " selected='selected'";
}
echo ">{$count}</option>";
}
?>
</select>
You seem to be relying on $count to be some internal ID that remains static; given that it's just a monotonically advancing integer, this seems like a risky proposition. However, if you're comfortable with that, all you need to do is change the value in the last echo statement to be the subject name instead of $count. I would instead encourage you to use something meaningful to the database - for example, some people create an auto_increment field called subject_id and key off of that; others like to use UUIDs.
I would also generally suggest using a foreach loop instead of a for loop, as it tends to simplify the code (you don't have to worry about maintaining the counter, or creating fence post errors, etc.) Here's a brief example - I'm guessing a bit at what the actual data is that is available to you, hopefully you can extrapolate it to what you actually have.
<p>Position:
<select name="position">
<?php
$sel_subject = get_all_subjects();
foreach($sel_subject as $subject_num => $subject) {
echo '<option value="', $subject_num, '"';
if ($subject['selected']) {
echo 'selected="selected"';
}
echo '>', $subject['subject_name'], '</option>';
}
Ill just explain by showing my code:
if($_POST)
{
for ($records = 1; $records <= $_POST['numberofrecords']; $records++)
{
if((!in_array($_POST['user'][$records], $assigned_users, true))||($_POST['user'][$records]==''))
{
$phonePost['user'] = $_POST['user'][$records];
$phonePost['id'] = $_POST['id'][$records];
$this->autoprov_model->update_phone_user($phonePost);
}
else
{
//other actions.....
}
etc....
$assigned_users is a query listing all IDs currently selected.
the relevant html is
<select name=user[<?=$lines;?>] style="position: relative; right: 120px;" onchange="submitform(this)">
<?php if($phone['user_id']=='')echo '<option value="">Unassigned</option>'?>
<?php foreach ($users_list as $user):?>
<?php if($user['id']==$phone['user'])$selected = 'selected="selected"'; else $selected = '';?>
<option value="<?=$user['id'];?>" <?=$selected?>><?=$user['name'];?></option>
<?php endforeach;?>
</select>
Whats happening is that I am posting all kinds of IDs (relevant to the $assigned_user array) but not actually in the array. and also when I post '' (blank) they never get updated and only reach the second section.
I ask here incase I am missing a trick with posting the values as arrays?
you should put name in quotes in your select tag. Like this: name="user[<?=$lines;?>]".
$assigned_users should be an array.
check existence with isset($_POST['user'][$records]) instead of comparing it with empty string, or simply try this: if((#in_array($_POST['user'][$records], $assigned_users, true)==false){.