Post dropbox selected values numerical id -codeigniter - php

I'm new to codeigniter and its developing. I have used html dropbox inside the form. when I press the submit button I want to retrive and post id of selected drop box index value. How do I do that? please find the code I used.
<?php
echo form_open('homepage/askquestionview');
?><div class="form-group">
<select name="cat" id="cato" onchange="activate_match()">
<?php
foreach ($catogories as $cat) {
echo'<option value="' . $cat . '" id="cato" >' . $cat . '</option>';
}
?>
</select>
<input type="submit" class="btn btn-success btn-block" value="Post Your Question" id="postQuestion">
</p>
<?php echo form_close(); ?>
homepage/askquestionview function
public function askquestionview() {
$data = array(
'Student_Email' => $this->input->post('cato'),
);
var_dump($data);
}
output
array(1) { ["Student_Email"]=> bool(false) }

Your select input name is 'cat' not 'cato'
<select name="cat"
Try a var_dump on $this->input->post() :
public function askquestionview() {
var_dump(this->input->post());
}
Tips, use form_helper to create your input : form_dropdown()
https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

I don't know much about the data structure of codeigniter, but you'll probably have to modifiy for loop like this:
foreach ($catogories as $id => $cat) {
echo'<option value="' . $id . '" id="cato" >' . $cat . '</option>';
}

Related

$_SESSION array does not work, can't add an object to it

Good morning, I not sure what is happening with my code but I can't find a solution myself. I made a form that whenever you add a new category, it should be displayed in the select element. But when I tried printing it out, I can see that whenever I add a category, the old one gets replaced and not added to an array as a second item. And even then, my form does not get the array from $_SESSION['categories']. Any suggestions on what am I doing wrong?
index.php
<?php session_start(); ?>
<form action="AddCategory.php" method="post">
<input type="text" name="categoryName" />
<select name="parentName">
<?php
if(isset($_SESSION['categories'])) {
foreach ($_SESSION['categories'] as $category) {
echo "<option>" . $category->name . "</option>";
}
} else {
echo "<option>No categories found</option>";
}
?>
</select>
<input type="submit" name="submit" value="Submit">
</form>
AddCategory.php
<?php
session_start();
require_once "Classes/Category.php";
if(isset($_POST['submit'])) {
if(isset($_POST['categoryName'])) {
$category = new Category(5, $_POST['categoryName']);
$_SESSION['categories'][] = $category;
}
}
header('Location: index.php');
Instead of storing whole object in session. try to store required value as a string if only one field is required and as a array if more fields are required as shown in below.
change $category->id with id of category as per your db field.
Change in AddCategory.php
From
$_SESSION['categories'][] = $category;
To
$_SESSION['categories'][] = ["id" => $category->id, "name" => $category->name];
And change in index.php
From
foreach ($_SESSION['categories'] as $category) {
echo "<option>" . $category->name . "</option>";
}
TO
foreach ($_SESSION['categories'] as $category) {
echo "<option>" . $category['name'] . "</option>";
}

Using ISSET with checkboxes

I am working on a wordpress search form to refine the current search and what Im trying to do is have the search results page with the search from and it's values set based on the query.
So far I've been successful in doing so with single select drop downs and single checkboxes like so --
<!-- SINGLE SELECT -->
<select name="baths" class="form-control">
<?php if (isset($_GET['baths'])) {
$bths = $_GET['baths']; ?>
<option value="<?php echo $bths; ?>"><?php echo $bths; ?></option>
<?php } else { ?>
<option value="Any">Any</option>
<?php } ?>
<option value="Any">Any</option>
<option value="1">1+</option>
<option value="2">2+</option>
<option value="3">3+</option>
<option value="4">4+</option>
<option value="5">5+</option>
<option value="6">6+</option>
<option value="7">7+</option>
<option value="8">8+</option>
<option value="9">9+</option>
<option value="10">10+</option>
</select>
<!-- SINGLE CHECKBOX -->
<input type="checkbox" name="dogs" class="styled" value ="yes" <?php if (isset($_GET['dogs'])) { ?>checked<?php } ?>>
That works, but for the multiple values it doesn't. This is my function to generate a set of checkboxes to select amenities -
<?php
$amenity_array = array();
$id = get_query_var('site');
if (!empty($id)) {
$property_amenities = get_post_meta($id, 'imic_property_amenities', true);
global $imic_options;
foreach ($property_amenities as $properties_amenities_temp) {
if ($properties_amenities_temp != 'Not Selected') {
array_push($amenity_array, $properties_amenities_temp);
}
}
}
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
foreach ($imic_options['properties_amenities'] as $properties_amenities) {
$am_name = strtolower(str_replace(' ', '', $properties_amenities));
$check = '';
if (in_array($properties_amenities, $amenity_array)) {
$check = 'checked="checked"';
}
<!-- HERE I TRY TO FIND THE SELECTED CHECKBOXES AND CHECK THEM OFF -->
if (isset($_GET['p_am'])) {
$ams = $_GET['p_am'];
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
} else {
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
}
<!-- END ISSET -->
}
} else {
_e('There is no Properties Amenities', 'framework');
}
?>
For the multi select drop down I am using bootstrap multiselect, so on my template the code looks like this --
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
}
}
?>
</select>
On the page it renders out as ---
<select name="property_type[]" id="pt-multi" class="form-control multi-select2 iOSselect" multiple="multiple" style="display: none;">
<option value="Any">All</option>
<option value="co-op">Co-Op</option>
<option value="condo">Condo</option>
</select>
<div class="btn-group" style="width: 100%;">
<button type="button" class="multiselect dropdown-toggle btn btn-default form-control multi-select2" data-toggle="dropdown" title="Property Type" style="width: 100%; overflow: hidden; text-overflow: ellipsis;">
<span class="multiselect-selected-text">Property Type</span>
<b class="caret"></b></button>
<ul class="multiselect-container dropdown-menu pull-right">
<li class="multiselect-item multiselect-all">
<a tabindex="0" class="multiselect-all">
<label class="checkbox"><input type="checkbox" value="multiselect-all"> Select all</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox">
<input type="checkbox" value="Any"> All</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox"><input type="checkbox" value="co-op"> Co-Op</label>
</a>
</li>
<li>
<a tabindex="0"><label class="checkbox"><input type="checkbox" value="condo"> Condo</label>
</a>
</li>
</ul>
</div>
Any ideas?
UPDATE
Using my code from above my amenities checkbox renders out on the page like so ---
<div>
<label>amenities</label>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Doorman (Full Time)">
<label for="doorman(fulltime)">Doorman (Full Time)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Doorman (Part Time)"><label for="doorman(parttime)">Doorman (Part Time)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Laundry (In-Unit)"><label for="laundry(in-unit)">Laundry (In-Unit)</label>
</div>
<div class="checkbox">
<input type="checkbox" name="p_am" class="styled" value="Fitness Center"><label for="fitnesscenter">Fitness Center</label>
</div>
</div>
This works with my search function allowing users to check off which amenities to include in the query string --
p_am=Doorman+%28Full+Time%29&p_am=Indoor+Pool
My search function is extensive but as far the relevant parts to the amenities -
$amenities = isset($_GET['p_am'])?$_GET['p_am']:'';
$amenities = ($amenities == __('Any', 'framework')) ? '' : $amenities;
// .....
if (!empty($amenities)) {
array_push($meta_query,array(
'key' => 'imic_property_amenities',
'value' => $amenities,
'compare'=>'LIKE'
));
}
UPDATE
In regards to setting the dropdown options for the bootstrap multiselect, I'm trying to set the options using the following code -
<?php
$taxonomyName = "city-type";
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );
echo "<select name='property_nhb[]' class='form-control multi-select' id='p-nhb' multiple>";
foreach ( $parent_terms as $pterm ) {
// echo "<option data-val='" . $pterm->slug . "' value='Any' " . $selected . ">Any</option>";
//Get the Child terms
$terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
foreach ( $terms as $term ) {
if(isset($_GET['property_nhb[]'])) {
$pnhb = $_GET['property_nhb'];
$selected = 'selected';
}
echo "<option data-val='" . $pterm->slug . "' value='" . $term->slug . "' " . $selected . " >" . $term->name . "</option>";
}
}
echo "</select>";
?>
My current search function code
This seems a little backwards for creating a search form - it seems more like you're generating a form for each individual result? Annotated code and comments below. I've tried to keep to your coding style in my answer.
$amenity_array = array();
$id = get_query_var('site');
// this first section gets the selected meta properties for the queried property (if available)
// [skipping for brevity ]
global $imic_options;
if (isset($imic_options['properties_amenities']) && count($imic_options['properties_amenities']) > 1) {
// this loops over all of the globally defined properties
foreach ($imic_options['properties_amenities'] as $properties_amenities) {
// gets the name of the property
$am_name = strtolower(str_replace(' ', '', $properties_amenities));
// and sets up to check the box if the property has the defined amenity
// if this is for a search form, why check boxes based on a result?
$check = '';
if (in_array($properties_amenities, $amenity_array)) {
// note: this only really needs to be 'checked'
$check = 'checked="checked"';
}
Here's where things go a little cross-purposes, though.
if (isset($_GET['p_am'])) {
$ams = $_GET['p_am'];
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
} else {
echo '<div class="checkbox"><input type="checkbox" name="p_am" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
}
This says that all checkboxes have the same name ("p_am") instead of using the loop name for the amenity ($am_name) or some form combination if you want all amenities in the same search input array (e.g. "p_am[$am_name]").
Each checkbox would also change the value of $check if supplied to the $_GET array.
$ams = $_GET['p_am'];
if (isset($ams[$am_name])) {
$check = 'checked';
}
Each checkbox would have name="p_am['.$am_name.']" as the name.
echo '<div class="checkbox"><input type="checkbox" name="p_am[' . $am_name . ']" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
If you want each of the amenities to have unique names (looking at the original checkbox which isn't called p_am at all) and not be in an array in PHP, then you would just use the loop name for the amenity ($am_name), like so:
if (isset($_GET[$am_name])) {
$check = 'checked';
}
Each checkbox would have name="'.$am_name.'" as the name.
UPDATE: after the OP was updated, it looks like every checkbox needs to have the same name, but not be keyed values. For this situation, your checkboxes should be called p_am[] (on both search page an original page), and you need to use something like in_array() instead of isset() to check the result, like so:
if (in_array($properties_amenities, $_GET['p_am'])) {
$check = 'checked';
}
Additional note - you're also using $am_name for the label, without actually setting the id attribute on the checkbox to match to, as well as not stripping out non-id characters (like parentheses), so the label association won't work.
echo '<div class="checkbox"><input type="checkbox" name="' . $am_name . '" ' . $check . ' class="styled" value ="' . $properties_amenities . '"><label for="' . $am_name . '">' . $properties_amenities . '</label></div>';
For the bootstrap select, you're just missing a check for which options are selected:
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
// if the option is 'checked', you need to add the 'selected' atttibute here
echo "<option value='" . $term->slug . "'>" . $term->name . "</option>";
}
}
?>
</select>
So the inside of the loop would look something like this instead:
// for efficiency's sake should live outside the loop as a one-off, but here for illustrative purposes
$types = $_GET['property_type'];
$selected = isset($types[$term->slug]) ? 'selected' : '';
echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";
If you're using an older version of bootstrap select, you may need to have the selected string be 'selected="selected" or data-selected="true", depending on the version you're using. For a current version, the above string should be fine.
If the <select> isn't meant to be an array of options either (i.e. not multiple), then the [] should be removed from the name, and would operate similarly to the checkbox code:
<select name="property_type" id="pt-multi" class="form-control multi-select2">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
$type = $_GET['property_type'];
$selected = isset($type) && $type == $term-slug ? 'selected' : '';
echo "<option value='" . $term->slug . "'" . $selected . ">" . $term->name . "</option>";
}
}
?>
</select>
The original HTML for the <select> doesn't actually 'work' as such either - all it's doing is adding a duplicate <option> to the top of the options list, and because nothing is selected, the form will treat the duplicate created at the top as 'selected' for the <select>.
UPDATE 2: the reason that the search function is breaking with an array of checkbox inputs is because you are using the LIKE comparison for your meta query. From the documentation on WordPress Meta Query:
It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'.
The documentation also shows examples of how to join these together. Since you haven't supplied how you're actually making the search query this is guessing a little, but it looks like for an array of checkboxes your code should be something similar to:
if (!empty($amenities)) {
foreach ($amenities as $amenity) {
array_push($meta_query, array(
'key' => 'imic_property_amenities',
'value' => $amenity,
'compare' => 'LIKE'
));
}
}
As with the examples in the documentation, you'll need to make sure there's a relation => 'OR' or relation => 'AND' to define how you want your search to operate.
what Fred's meant in the comment is something like this:
<input type="checkbox" value="condo" name="var_name[]">
the [] at the end of the name attribute is to tell PHP that it should treat the variable as an array.
and then in the PHP script, you can just check for the array length using somthing like:
$arr = $_POST['var_name']; echo count($arr);
based on leith's comment, if it is auto generated, then you should not be bothered to check the checkboxes manually.
instead, just make the selected options be selected, for example:
<?php
$selections = $_GET['property_type']; // or something similar
?>
<select name="property_type[]" id="pt-multi" class="form-control multi-select2" multiple="multiple">
<?php
$terms = get_terms( "property-type", array( 'hide_empty' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option value='Any'>All</option>";
foreach ( $terms as $term ) {
$selected = in_array($term->slug, $selections);
echo "<option value='" . $term->slug . "' " . ($selected?' selected':'') . ">" . $term->name . "</option>";
}
}
?>
</select>
see this fiddle https://jsfiddle.net/u1w158tp/
in it you just selected the options and it will automatically checked the checkboxes
Your checkboxes has the same name. Try to add different name or use "p_am[]" to treat them as array.

How I can send customer id from view to controller

I want to send customer id from view to controller when I select customer name from dropdown list it will send customer's id to controller.
here is my code of view file 'customerlifecycleanalytic.php'
<select class="form-control selectpicker customerfilter">
<option value=''>Select Customer</option>
<?php
if (isset($customername)) {
foreach ($customername as $customernames) {
echo '<option value="' . $customernames['id'] . '" >' . $customernames['firstname'].'&nbsp'. $customernames['lastname']. '</option>';
}
}
?>
</select>
here is my controller code 'Customerlifecyclecontroller'
public function actionCustomerlifecycleanalytic() {
$customername = Customer::model()->findAll("store_id='" . Yii::app()->session['store_id'] . "'");
$customerlifecycle = CustomerLifecycle::model()->findAll();
$this->renderPartial('customerlifecycleanalytic', array( 'customername' => $customername, 'customerlifecycle' => $customerlifecycle), false, true);
}
Try like this..Its working. Hope this will help.
View:
//view file code. Just add Onchange select attribute like below:
<select onchange="if(this.value) window.location.href='http://yourdomain.com/yourcontroller/youraction/'+this.value" class="form-control selectpicker customerfilter">
<option value=''>Select Customer</option>
<?php
if (isset($customername)) {
foreach ($customername as $customernames) {
echo '<option value="' . $customernames['id'] . '" >' . $customernames['firstname'].'&nbsp'. $customernames['lastname']. '</option>';
}
}
?>
</select>
Controller:
// controller file code. just add a variable parameter to hold the id in action method.
public function youaction($id){
echo "id : ".$id; // customer id
}

PHP foreach loop to print out html listbox options

Here goes my first post.
I am currently pulling two fields out from my MySQL database with a fetch all and am trying to get the data in those fields to become options in a listbox.
This is my code:
<fieldset class="contact">
<legend>Select a Band</legend>
<!-- Drop List -->
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '">' . $name . '</option>';
}
echo $options;
?>
</select>
and here is the result of echoing out $options:
<option value="1">Rise Against</option><option value="2">Alter Bridge</option><option value="3">Falling In Reverse</option><option value="4">Saosin</option><option value="5">Pennywise</option><option value="6">The Killers</option><option value="7">Thrice</option><option value="8">Four Year Strong</option>
I want to have the foreach loop print out the code that will be the options for my listbox lst1 but it is currently not working. Any ideas as to why?
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"]; ?>
<option value = "<?php echo $id; ?>"><?php echo $name; ?></option>
<?php } ?>
Try this code. In your code '&lt' and '&gt' makes html entity format. So html does't accept it as a tag.
<select id="lst1" name="lst1" tabindex="281" size="1">
<?php
foreach ($bands as $band) {
$name = $band["fldBand"];
$id = $band["pkID"];
$options .= '<option value="' . $id . '>' . $name . '</option>';
}
echo $options;
?>
</select>

List Box Should be Drop down

to every one
I have some values in my data base i Want to display them with check boxes.
those values should be display when i click at the button. This should not in combo box.
because I want to post multiple values at one time.
Please help with thanks
<?php
$womenlist=mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
$gs=0;
while($girlslist=mysql_fetch_array($womenlist))
{
$gs++;
?>
<li style="background-color:#CCC; width:150px;"><label for="chk1"><input type="checkbox" name="chk_<?php echo $gs?>" id="chk<?php echo $gs?>" value="<?php echo $girlslist['calName'];?>" <?php if($_REQUEST['chk_'.$gs]==$girlslist['calName']){?> checked="checked"<?php }?>><?php echo $girlslist['calName']." ".$girlslist['calDesc']; ?> </label></li>
<?php }?>
You could limit the number of columns in your query (not SELECT * ...). You've put the <input> inside the <label>. The <label>'s for="" attribute is hardcoded as chk1. You could take out the inline style="" on the <li> and put it into a stylesheet. I've "tidied" it up a bit (untested):
$womenlist = mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
$gs = 0;
while( $girlslist = mysql_fetch_array($womenlist) )
{
$gs++;
echo '<li style="background-color:#CCC; width:150px;">'
. '<label for="chk' . $gs . '">' . $girlslist['calName'] . ' ' . $girlslist['calDesc'] . '</label>'
. '<input type="checkbox" name="chk_' . $gs . '" id="chk' . $gs . '" value="' . $girlslist['calName'] . '"
. (($_REQUEST['chk_'.$gs]==$girlslist['calName']) ? 'checked="checked"' : '') . '></li>';
}
Not sure entirely what you're asking for here (as it looks like you already have this working with checkboxes), but you can in fact post multiple values with a select box. You just use the multiple attribute, and specify the name as an array with the square brackets:
<form action="yourscript.php" method="post">
<select name="women[]" multiple="multiple">
<option value="woman1_name">Woman 1</option>
<option value="woman2_name">Woman 2</option>
<option value="woman3_name">Woman 3</option>
</select>
<input type="submit" />
</form>
If you post a form with this, selecting woman 2 & 3, var_dump($_POST); yields:
array(1) {
["women"]=>
array(2) {
[0]=>
string(11) "woman2_name"
[1]=>
string(11) "woman3_name"
}
}
Alternatively, if you want the values of your checkboxes to come through in a similar fashion, change them so they all have the same name, but with the square brackets on the end. This HTML would yield similar POST data:
<input type="checkbox" name="women[]" value="Woman 1" />
<input type="checkbox" name="women[]" value="Woman 2" />
<input type="checkbox" name="women[]" value="Woman 3" />
So, to create a dropdown using this, here's an adaptation of your code. I believe this is what you're after:
<?php
$options = '';
$womenlist=mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
while($woman=mysql_fetch_array($womenlist)) {
$options .= '<option value="'.$woman['calName'].'"'.((!empty($_REQUEST) && in_array($woman['calName'],$_REQUEST['women'])) ? ' selected="selected"' : '').'>'.$woman['calName'].' '.$woman['calDesc'].'</option>';
}
?>
<label for="women">Women:</label>
<select id="women" name="women[]" multiple="multiple">
<?php echo $options; ?>
</select>

Categories