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){.
Related
I have a column in my sql table for sizes, and it's supposed to be a drop down menu that displays the sizes and then links you to a new sql row; however, nothing shows up in the options box and there's nothing displayed in the source code. This is what the code looks like:
<form>
<select onchange="window.location.href=this.form.URL.options[this.form.URL.selectedIndex].value" name="URL">
<?php
$size = unserialize($row['size']);
foreach ($size as $key => $value) {
echo "<option value='$value'>$key</option>\n";
}
?>
</select>
</form>
My arrays look as such:
"a:12:{s:3:"1/8";s:27:"legendproduct.php?id=101300";s:3:"1
/4";s:27:"legendproduct.php?id=101101";s:3:"3
/8";s:27:"legendproduct.php?id=101102";s:3:"1
/2";s:27:"legendproduct.php?id=101023";s:3:"3
/4";s:27:"legendproduct.php?id=101024";s:1:"1";
s:27:"legendproduct.php?id=101025";s:5:"1
1/4";s:27:"legendproduct.php?id=101026";s:5:"1
1/2";s:27:"legendproduct.php?id=101027";s:1:"2";
s:27:"legendproduct.php?id=101028";s:5:"2
1/2";s:27:"legendproduct.php?id=101109";s:1:"3";
s:27:"legendproduct.php?id=101110";s:1:"4";
s:27:"legendproduct.php?id=101111";}"
What am I doing wrong? Any help would be appreciated. Thanks.
I am displaying the list of users in the select box. There are two types of users i-e selected users and non selected users. The values of these users are coming from the database in two arrays i-e One array contains selected users record and other array contains all users record. Now i want if the page loads the selected users record should be shown as selected in the select box and non selected users will be shown as non selected. Here is my code:
if ($selected != false ){
foreach ($selected as $select)
{}
foreach ($data as $rows) { echo $rows->id."<br />"; echo $select->id; ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) echo "selected";?>><?php echo $rows->username; ?></option>
<?php } } else{
foreach ($data as $rows) ?>
<option value="<?php echo $rows->id; ?>" <?php if ($rows->id == $select->id) ?>><?php echo $rows->username; ?></option>
<?php } ?>
The $selected object contains the selected users list and the $data object contains non selected/total number of users
If i am not wrong you have two array like
$arr1 = array(array('id'=>1,"name"=>'abc1',"user"=>'select'),array('id'=>2,"name"=>'abc2',"user"=>'select'),array('id'=>3,"name"=>'abc3',"user"=>'select'),array('id'=>4,"name"=>'abc4',"user"=>'select'),array('id'=>5,"name"=>'abc5',"user"=>'select'),array('id'=>6,"name"=>'abc6',"user"=>'select'),array('id'=>7,"name"=>'abc7',"user"=>'select'));
$arr2 = array(array('id'=>8,"name"=>'abc8',"user"=>'noselect'),array('id'=>9,"name"=>'abc9',"user"=>'noselect'),array('id'=>10,"name"=>'abc10',"user"=>'noselect'),array('id'=>11,"name"=>'abc11',"user"=>'noselect'),array('id'=>12,"name"=>'abc12',"user"=>'noselect'),array('id'=>13,"name"=>'abc13',"user"=>'noselect'),array('id'=>14,"name"=>'abc14',"user"=>'noselect'));
$arraymerege = array_merge($arr1,$arr2); // Merge the array into one
check out the html it display the user selected. i'll just put the condition under select box if user is noselect then it show selected. Please check the image for loop
you are closing your first foreach before you actually get inside the loop...try this. (rewritten with a little shortcode added)
<select name="users" type="multiple">
<?php
//no need to write $selected == false,
//this is the same, a ! will compare this var to false
//also i use : instead because its much nicer to read i.m.o
//its also a little easier to tell the difference from a closing
//if and foreach this way if you have a lot of nested comparisons.
if (!$selected) :
//you want to loop through all rows first.
foreach ($data as $rows) :
//now look at each selected user per user
foreach ($selected as $select) : ?>
//here is where you make the comparison. no need for any other loops
//this is called a turnary comparison. its basically short code for
//if(blah) else this;
//read up on it here:
// http://www.php.net/manual/en/language.operators.comparison.php
//the <?= is the same as <?php echo. This does require for you to have
//short codes turned on in your main php.ini. It usually is though.
<option value="<?= $rows->id; ?>
<?= (($rows->id == $select->id) ? 'selected' :''); ?>
<?= $rows->username; ?>
</option>
endforeach;
endif;
?>
</select>
This is all the code you need. You should only require two loops. The very first loop would iterate through each user, and the nested loop would then compare each of those users to each selected user. Also, the ability to write cleaner code requires planning. I would suggest investing in a white board or a notepad to draw out flow charts, diagrams etc before you start writing your code. You will get a basic picture in your head. There are always 100 million ways to do one thing, but only one way that is right for you. And its up to you to find that way.
From how I read the question, he is showing all users and highlighting the selected ones in a multiple select box. He has multiple selected users so you couldn't merge the arrays together. You would have to compare each user in the table to each selected user then decide if you need to highlight that user or not.
This gives lists all people in the db and give a drop down for each one of them i want to make it so when i hit one submit button it enters individual values for each person.
so if you make yes for bobby no for mark and yes for dustin you can the pres submit and it will enter that for there values
$results = mysql_query("SELECT * FROM `$tabeluser` WHERE buss='$buss' ORDER BY id DESC LIMIT 1");
while($rows = mysql_fetch_array($results) )
{
fn = $_POST['firstname'];
echo $fn;
?>
<form>
<select name="check">
<option>no</option>
<option>yes</option>
</select>
<?php
<input type="submit" name="submit">
?>
<form>
<?php
}
mysql_query("INSERT INTO `$fn` (buss) VALUES ('$_POST[check]')");
First of all, you create a <form> and a submit button for each of the records you have. That is wrong, since you want to update multiple values at once.
What it should look like would be:
<form>
<?php
while($rows = mysql_fetch_array($results)) {
print '<select name="check[]"> .. </select>';
}
?>
<input type="submit" name="submit" />
</form>
Secondly, your code is formatted as if you are expecting to get $_POST[check] right after sending the code to the browser. That is not how PHP works. You need to separate the logic of having posted values, before printing the actual page contents. PHP is server side, which means that it won't get any data, unless the script is called with it from the beginning. So, that should look something like:
<?php
if (isset($_POST["check"])) {
// handle posted data.
}
else {
// show form
}
// or show form here, without an else, if you want to always show a form,
// even when you have posted values.
?>
Last but not least, you need to find a way to know which posted value belongs to each of your records. The way you have it now (<select name="check">') it will just return you one single value.
If you write it the way I intentionally did above (`) you will get all values, but still you won't be able to easily recognize which value is for each record.
Instead, you may want to do a final result of something like:
<?php
// get mysql records into an array (say $my_array)
if (isset($_POST["submit"])) {
foreach($my_array as $record) {
if (isset($_POST["select_of_id_".$record["id"])) {
// insert additional value into db
}
}
}
print '<form>';
foreach($my_array as $record) {
print '<select name="select_of_id_'.$record["id"].'">';
print '<option value="0">no</option><option value="1">yes</option>';
print '</select>';
}
print '<input type="submit" name="submit"/>';
print '</form>';
?>
Changes required in your code :-
<select name="check[]">
<option value="<?php echo $userId;?>_0">no</option>
<option value="<?php echo $userId;?>_1">yes</option>
</select>
You should make changes in you DB It help to easy maintaing your data.
Create new table where you can save multiple user check data with respective Post
for e.g post_id user_id check
101 111 0
101 112 1
How you can store data from you html
In you post array you will get check array in which you will get multiple check value like this
101_0 if no selected and 102_1 if yes selected. Now you need to explode this value.
$_POST['check'] = array(0 => `101_0`, 1 => 102_1);
Inside loop you can extract userid and respective checked value.
for e.g
$checked = explode('_','101_0');
$userId = $checked[0];
$check = $checked[1];
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.
If I have a line like this,
<option value="someval">somval</option>
how can I position the cursor after the last quotation of value and put something like abcdef?
So the output would be
<option value="somval" abcdef>somval</option>
with PHP?
I want to do this dynamically and I can't figure out how to do it. I'm looking at strpos(), but I don't see how it can be done. I'll be posting a bunch of option tags into a textbox and code will be generated. so I'll have a lot of option fields.
#martin - Say I have a huge dropdown and each option lists a country that exists. Rather than having to manually type out something like this:
$query = $db->query("my query....");
while($row = $db->fetch($query)) {
<select name="thename">
<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>
<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>
<option value="someval" <?php if($row['someval'] == 'someval') { print "selected"; } ?> >someval</option>
... Followed by 100 more, because there are a lot of locations to list.
</select>
How can I post all the options I have into a textbox and have the above code automatically generated to save a lot of time?
Using your example you would do:
while($row = $db->fetch($query)) {
printf('<option value="someval"%s>someval</option>',
($row['someval'] == 'someval') ? ' selected="selected" ' : '');
}
This would go through the rows and output an option, replacing the %s with the attribute selected="selected" if $row['someval'] is equal to someval. However, the above is rather pointless, because all option elements will have the same value and text, so try
while($row = $db->fetch($query)) {
printf('<option value="%s"%s>%s</option>',
$row['country-code'],
($row['country-code'] === $selection) ? ' selected="selected" ' : '',
row['country-name']);
}
With $selection being anything you want to compare against. Replace the keys in $row with appropriate keys from in your database.
Note: The usual disclaimers about securing your output apply
You could capture (value=".+?") and replace it with $0 abcdef.
<?php
$string = '<option value="someval">someval</option>';
print preg_replace("/(value=\".+?\")/i", "$0 abcdef", $string);
?>
Which outputs the following:
<option value="someval" abcdef>someval</option>
With PHP, you can generate a whole string with any text you wish. Where do you have your original string? In a variable or a text file?