Trying to create an array condition - php

I have a text field which the user can enter comma separated list and then php converts it to a drop select list however I want a condition that will show a text input field if only a single value is entered. I tried the code below but it is only returning the select box even with a single entry. How can I achieve this condition?
<?php $listval = explode(",",$vals);
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>

explode will always return an array. So therefor is_array will always be true.
Change your if statement to this
if(sizeof($listval) > 1)

try This
<?php $listval = strpos(',',$vals)?explode(",",$vals):$vals;
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>

Please try to use the follwing code:
<?php $listval = explode(",",$vals);
if(count($listval) > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php } elseif(count($listval) == 1) { ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>

As #Kris indicated, you can check with,
count($array)
or
sizeof($array)
<?php $listval = explode(",",$vals);
$array_count = count($listval);
if($array_count > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>

Related

Disable datalist input if there is no option data

I have datalist like this :
<input type="text" list="colours" id="txt">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
The option that datalist has was populated from a foreach loop using php.
How do I disable the datalist input, if the data that foreach loop's result is null/none using jquery ?
like for example
Have Option
<input type="text" list="colours">
No Option
<input type="text" list="colours" disabled="disabled">
Here is an example of keeping your PHP and HTML separate:
<?php
$option="";
foreach ($kondisi as $kondisi) {
if($kondisi != 'null' || $kondisi != 'none'){
$option .= '<option data-value="'.$kondisi'.->nama_kondisi" value="'.$kondisi.'->id_kondisi">'.$kondisi.'->nama_kondisi</option>';
}else{
$option .= 'your disabled option code here...';
}
}
?>
<input type="text" list="colours">
<datalist id="colours">
<?php echo $option; ?>
</datalist>
If you want to disable it then do like below:-
<?php if(count($kondisi)>0){?>
<input type="text" list="colours">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
<?php }else{?>
<input type="text" list="colours" disabled="disabled"/><!-- or use autocomplete="off"-->
<?php } ?>

Multiselect Checkbox Validation in PHP

I am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.
My Form
<?php
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title;
}
} ?>
try this code
<?php
$k==0;
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title;
$k++;
}
} ?>

how to add check bo0x in selection box with multiple selection box

I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);

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>

Text box limited to array contents

I currently have a drop down box than when one of the options is selected it will echo-
"Your Favourite Car Is (option)
What I need to do now is change this so its a text box but the user can only type in one of the options within the array and if another one was chosen it would say you cannot have this as one of the choices and it would also be able to type in more than one so theoretically it could echo-
"Your Favourite Car is Mazda, Nissan, Renault!"
Here is the code i have now for the drop box that i have working.
<form method="post">
<div id="dropdown">
<?php
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
}
else
{
$mycar="";
}
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="this.form.submit()">';
foreach($array1 as $cars)
{ ?>
<option value="<?php echo $cars; ?>" <?php if($mycar==$cars) echo "
"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
</div></form>';
?>
<div id="result">
<?php
echo "Your favourite car is $mycar";
?>
</div>
EDIT: I have attempted this and what i currently have always echo's "this car isnt among the selection" and nothing else and nothing i enter into the text box seems to effect this
here is the code i have
<?php
$cars = array("Volkswagen","Renault","Land Rover");
?>
<form action="array.php" method="post">
<center> <input type="text" name="cars" id="cars" />
<input type="submit" /> </center>
<?php
if (in_array($_POST, $cars)) {
echo "Your Favourite Car is $_POST";
}
else {
echo "This car is not among the selection";
}
?>
</form>
You can easily control the user input by checking it before:
<form method="post">
<div id="dropdown">
<?php
// Car types
$carTypes = array('Volkswagen' , 'Renault' , 'Land Rover');
$wrongCarChoosen = false ;
if(isset($_POST['cars'])) {
$mycar = $_POST['cars'];
if (!in_array($mycar, $carTypes)) {
$wrongCarChoosen = true ;
}
}
else {
$mycar = "";
}
echo' <select name="cars" onchange="this.form.submit()">';
foreach($carTypes as $carName){ ?>
<option value="<?php echo $cars; ?>"
<?php
if($mycar == $carName) echo "
"selected='selected'"; ?> >
<?php echo $carName;
?></option>
<?php
}
echo'</select>
</div></form>';
?>
<div id="result">
<?php
if ($wrongCarChoosen) {
echo "Your choice ".$mycar." is not contained in ".implode($carTypes, ',') ;
}
else {
echo "Your favourite car is $mycar";
}
?>
</div>
You must edit the form to accept multiple values. And you can check the user values before you echo the text.
<form method="post">
<div id="dropdown">
<?php
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
$error = false;
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
foreach ($mycar as $car)
{
if (!in_array($car, $array1))
{
$error = $car;
}
}
}
else
{
$mycar=Array();
}
echo' <select name="cars[]" multiple="multiple">';
foreach($array1 as $cars){ ?>
<option value="<?php echo $cars; ?>" <?php if(in_array($cars, $mycar)) echo "\"selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
<input type="submit" name="submit" value="submit" />
</div></form>';
?>
<div id="result">
<?php
if ($error===false) echo "Your favourite car is ".implode(', ', $mycar);
else echo $error . ' is not contained by ' . implode(', ', $array1);
?>
</div>

Categories