I have a little problem.
I'm searching the internet now for quite some time to find a proper solution but I was not successful so far.
This is what I want :
First, I choose Category
Then, in second selection contain all the store result from category selection.
this is my first drop down list which is contain Category :
<select name="category" class="form-control" id="select1">
<option value="-1"> - Choose One -</option>
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$stores = $StoreCategoriesAPIAccessor->getStoreCategories();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreCategoryId(); ?>"><?php echo $store->getCategory(); ?></option>
<?php endforeach; ?>
</select>
this is my second drop down list :
<select name="category" class="form-control" id="select2">
<option value="-1"> - Choose One -</option>
<?php
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores();
foreach ($stores as $store):
?>
<option value="<?php echo $store->getStoreId(); ?>"><?php echo $store->getStoreName(); ?></option>
<?php endforeach; ?>
</select>
Anyone know how to implement dynamic drop down list for this case ?
Well first of all I'd like to ask that you separate business logic from view logic as much as possible, so I will do that in this answer.
Secondly, the 2nd dropdown box logic that you have does not contain anything that would retrieve stores for a given category, I will therefore make some assumptions and you can adjust based on that.
<?php
$StoreCategoriesAPIAccessor = new StoreCategoriesAPIService(GuzzleClient::getClient());
$categories = $StoreCategoriesAPIAccessor->getStoreCategories();
if (!empty($_GET['category'])) {
$category_id = $_GET['category'];
$StoreAPIAccessor = new StoreAPIService(GuzzleClient::getClient());
$stores = $StoreAPIAccessor->getStores($category_id); // Assumption, the call to getStores() accepts category_id as a parameter
} else { // Optional as you don't need to declare variables in PHP, but its better practice to do so
$category_id = null;
$stores = array();
}
?>
<select id="select_category" name="category" class="form-control" onchange="window.location='?category=' + this.value">
<option value="">- Choose One -</option>
<?php foreach ($categories as $category): ?>
<option value="<?php echo $category->getStoreCategoryId() ?>"><?php echo $category->getCategory() ?></option>
<?php endforeach ?>
</select>
<select id="select_store" name="store" class="form-control"<?php echo $category_id == null ? " disabled='disabled'" : "">
<option value="">- Choose One -</option>
<?php foreach ($stores as $store): ?>
<option value="<?php echo $store->getStoreId() ?>"><?php echo $store->getStoreName() ?></option>
<?php endforeach ?>
</select>
Related
I have a problem, I have an array of contacts that I send from the controller to the view and when it comes to going through it with a pos loop, everything goes well, but the problem lies when I try to interact within a select type input, creating for each contact a <option> iterates but does not access the values
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact[$id] ?>"><?php echo $contact[$username] ?></option>
<?php endforeach; ?>
</select>
this does work but outside the input select -->
<?php foreach ($contacts as $contact) : ?>
<?php debug($contact); //function for print data
?>
<?php endforeach; ?>
this is what the date looks like -->
There are 3 different method to get value.
using key name with "square brackets []":
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact['id'] ?>"><?php echo $contact['username'] ?></option>
<?php endforeach; ?>
</select>
using key name with "arrow ->":
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact->id ?>"><?php echo $contact->username ?></option>
<?php endforeach; ?>
</select>
using key name with "dots . ":
<select id="members" name="members[]" class="form-select" multiple required>
<option value="tu" selected>Tú</option>
<?php foreach ($contacts as $contact) : ?>
<option value="<?php echo $contact.id ?>"><?php echo $contact.username ?></option>
<?php endforeach; ?>
</select>
Those all methods are same but we can get value using this methods simple.
I need to generate a list of teams from the cups selection box at the top of my page, this code allows me to run a while loop to generate the correct results for the first selection box, however, when i replicate the code in the second it shows no results?
<form class="seed-form">
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
while ($team = mysqli_fetch_assoc($show_teams)) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
while ($team = mysqli_fetch_assoc($show_teams)) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
<input type="submit" name="submit" value="Generate" class="submit">
<?php
if (isset($_POST['submit'])) { //checking if submit button was clicked
include_once 'action/dbcon.php';
$cname = $_POST['cupname'];
if (empty($cname)) {
header("Location: tables.php?field=empty"); //return them if fields are empty
exit();
} else {
$sql = "SELECT * FROM teams WHERE cup_name='$cname'";
$show_teams = mysqli_query($conn, $sql);
$numberCheck = mysqli_num_rows($show_teams);
if ($numberCheck < 8) {
header("Location: tables.php?tables=1"); //Take to cup page if there arent enough teams in the cup
}
}
}
?>
The problem is that fetch_assoc() returns the next row in the resultset, unless there are no more left (in which case it returns NULL - see docs).
Your first while loop runs until there are no more results. Therefore when you start the second while loop, there are still...you guessed it: no more results. You already used them up. Therefore the first call to fetch_assoc() in the second loop returns NULL immediately, and so the loop condition is never met, and the loop never executes.
There are two different ways to solve this:
1) Reset the result pointer to the start of the resultset. Somewhere between the two loops, write
mysqli_data_seek($show_teams, 0);
See docs for more info.
2) Read all the data into a PHP array, which you can then loop through as many times as you like using foreach:
<?php
$teams = array();
while ($team = mysqli_fetch_assoc($show_teams)) {
$teams[] = $team;
}
?>
<form class="seed-form">
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
foreach ($teams as $team) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?><!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
<select name="team" required>
<option value='Holder' disabled selected>Select Team</option> <!--Placeholder for Select-->
<?php
foreach ($teams as $team) { ?>
<option value="<?php echo $team["team_id"]; ?>"><?php echo $team["team_name"]; ?></option>
<?php } ?> <!--FILLS SELECT BOX WITH TEAMS FROM THAT CUP-->
</select>
I want to used set_value for input where type=text and i have no problem with that.
I am confused to use dropdown value. i am fetch dropdown values from database and i could not understand where i use set_value .
My code:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):
if($item->sport_name==$mySport)
{?>
<option value="<?php echo $item->sport_id; ?>" selected><?php echo $item->sport_name; ?></option>
<?php }else{?>
<option value="<?php echo $item->sport_id; ?>"><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
You Can try This:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php foreach($getSport as $item):?>
<option value="<?php echo $item->sport_id; ?>" <?php if($item->sport_name==$mySport)
{ echo "Selected";} ?>><?php echo $item->sport_name; ?></option>
<? } endforeach; ?>
</select>
As per the docs, you would not use set_value for a select element:
"Permits you to set the value of an input form (text input) or textarea.".
Rather, you would use set_select.
If you use a <select> menu, this function permits you to display the menu item that was selected. The first parameter must contain the name of the select menu, the second parameter must contain the value of each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).
set_select($field[, $value = ''[, $default = FALSE]])
Parameters:
$field (string) – Field name
$value (string) – Value to check for
$default (string) – Whether the value is also a default one
Returns: ‘selected’ attribute or an empty string
Return type: string
Example:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<option value="one" <?php echo set_select('sport_name', 'one'); ?> >One</option>
<option value="two" <?php echo set_select('sport_name', 'two'); ?> >Two</option>
<option value="three" <?php echo set_select('sport_name', 'three'); ?> >Three</option>
</select>
your code should be work correctly, but best way: you can declare var called $selected and make comparision to assign it with selected word inside loop when the value of select == current selected value:
<select class="form-control" name="sport_name" id="sport_name">
<option value=''>--Select Sport--</option>
<?php
foreach($getSport as $item):
$selected='';
if($item->sport_name==$mySport)
{
$selected='selected';
}
?>
<option value="<?php echo $item->sport_id; ?>" <?php echo $selected; ?> >
<?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
The best way of doing this, while remaining readability would be as follow:
<select class="form-control" name="sport_name" id="sport_name">
<option selected>--Select Sport--</option>
<?php foreach($getSport as $item): ?>
<option <?php if($item->sport_name == $mySport){ echo "selected"; } value="<?php echo $item->sport_id;?>"><?php echo $item->sport_name; ?></option>
<?php endforeach; ?>
</select>
Ofcourse this is thinking you've got your data correctly set as objects or if you have arrays you'd use
<?php echo $item['sport_name']; ?>
my goal is get current value from database, selected in the dropdown list.
I tried that code but it shows always "Ndone" in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option selected="selected" value=""></option>
<option selected="selected" value="DONE">DONE</option>
<option selected="selected" value="NDONE">NDONE</option>
</select>
Also this one and nothing just the first row that selected in the dropdownlist
<select name="work" id="work" value="<?php echo $work; ?>">
<option value=""></option>
<option value="DONE">DONE</option>
<option value="NDONE">NDONE</option>
</select>
I don't know what to do, any help please
When i use a textfield it works, so there's no problem with the variable $work
<input type="text" name="work" value="<?php echo $work; ?>"/>
<select> does not have a "value" Attribute. You must add the selected attribute to one option.
<?php
$options = array(
'',
'DONE',
'NDONE',
);
?>
<select name="work" id="work">
<?php foreach ($options as $option): ?>
<option value="<?php echo $option ?>"<?php echo $option === $work ? ' selected="selected"' : '' ?>><?php echo $option ?></option>
<?php endforeach ?>
</select>
As per your example you could do this:
You just need to check each option value against the $work variable and if it matches add the selected attribute.
<select name="work" id="work">
<option value=""></option>
<option value="DONE" <?php echo($work == 'DONE'?'selected="selected"':''); ?>>DONE</option>
<option value="NDONE" <?php echo($work == 'NDONE'?'selected="selected"':''); ?>>NDONE</option>
</select>
This is a simple one, i know it is i just cant think of a good logical way to do this.
I have the following code:
<select name="Title[]" class="form-control">
<option value="">Select title...</option>
<option value="Mr">Mr</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Prof">Prof</option>
<option value="Doctor">Doctor</option>
</select>
What i want to do is on my edit client page, have their previously selected option displayed. So for example, if Doctor was selected at sign up, it would be selected by default on the edit page. I know i could do this like:
<option value="Doctor" <?php if($client_title == 'Doctor'){ echo 'selected'; } ?>>Doctor</option>
But it seems like a bit of a redundant way to do it. Can this be done easily with a do while statement?
Sorry for the simple request, having a bit of a slow day today! haha
<select name="title[]" class="form-control">
<?php
$titles = array('Mr', 'Miss', 'Mrs', 'Ms', 'Prof', 'Doctor');
foreach ($titles as $title) {
$selected = $client_title == $title ? ' selected="selected"' : null;
?>
<option value="<?php echo $title; ?>"<?php echo $selected; ?>><?php echo $title; ?></option>
<?php
}
?>
</select>