I'm a noob at php so I need a bit of help. I have a table with a list of hotels, the columns are
name | address | city | stars | price
so far I've made a dynamic drop-down menu where the user selects a city from the table, this is shown in the code below:
<?php
connection stuff blah blah
?>
<?php
$sql = "SELECT city FROM hotels";
$result_db = $db->query($sql);
if (!$result_db) {
echo $db->error . ' Error perform query!';
} else {
echo '<select name="value">';
echo '<option value="">-Select from below-</option>';
while ($row = $result_db->fetch_object()) {
echo '<option city="' . $row->city . '">';
echo $row->city;
echo '</option>';
}
echo '</select>';
}
?>
How would I now create a table where all the details of hotels in the selected city are outputted? I'm assuming I need another sql query, arrays (perhaps?) which then I echo into a table? :S But I have no idea how to.....
you need to use jquery to add event to your dropdown second in your table you should have an ID field..
You're table should be
Id | name | address | city | stars | price
next you need to create a query that will fetch the id and name fields and put it on a dropdown.
next you need to use jquery to add event to dropdown.
$(function() {
$("#hotel").change(function() {
});
})
Related
I'm updating a PHP form field from a dropdown (single value selected) to a multiselect (multiple values selected).
I have 2 DB tables, one where the complete list of team members reside, and the other where the selected team members reside.
On the PHP page, I want to get the values from the full team members table and show the full list as a multiselect form field.
Then I want to be able to get the values from the selected team member table and have them show as selected options in the full multiselect list mentioned above.
Any idea on how I would accomplish this?
Here's my code, although right now it just returns the full team member list without the selected values.
$query = "SELECT walkername FROM Team_Management WHERE active=1 ORDER BY walkername ASC";
$stmt = $mysqli->prepare($query) or die ("Couldn't execute query: ".mysqli_error($mysqli));
$stmt->execute();
$stmt->bind_result($walkers);
echo "<div class='form-group'>";
echo "<label class='col-lg-3 control-label' for='Walkers'>Walkers:</label>";
echo "<div class='col-lg-5'>";
echo "<select multiple class='form-control' name='walkers[]' id='Walkers'>";
while ($stmt->fetch()) {
echo "<option value='$walkers'>$walkers</option>";
}
echo "</select>";
echo "</div>";
echo "</div>";
$stmt->close();
** UPDATED **
So one thing that I should've added, is that the SELECTED_TEAM_MEMBERS field is a comma separated field.
TEAM_MANAGEMENT table
id || walkername
1 || John
2 || Kate
SELECTED_TEAM_MEMBER table
cid || walkers
1 | John,Ray,Kate
2 | Kate,Matt,Joe
In addition, each group in the walkers field in the SELECTED_TEAM_MEMBER table is tied to a unique client id (cid).
So how can I identify the selected walkers from the complete list in the TEAM_MANAGEMENT table by unique client id.
You can get the list of your selected user with a boolean in your SQL request
SELECT walkername,
CASE WHEN **selected_team_members_name** > '' THEN '1' ELSE '0' END as is_selected
FROM Team_Management
LEFT OUTER JOIN **SELECTED_TEAM_MEMBERS** ON **selected_team_members_name** = walkername
WHERE active=1
ORDER BY walkername ASC
With selected_team_members_name the name of the column in your table and SELECTED_TEAM_MEMBERS the name of your table
And now $walker is as array with 2 key: walkername and is_selected
And after you can try a if to put them the attribute 'selected' when you write your 'option' tag
while ($stmt->fetch())
{
$selected = "";
if($walkers['is_selected'] == '1'){ //If your walker is selected
$selected = "selected";
}
echo "<option ".$selected." value=".$walkers['walkername'].">".$walkers['walkername']."</option>";
}
I may not have understand the context but i hope i helped you.
This is my current SQL Table I'm using to grab data for my php dropdown. The main goal of this is to turn this into a dropdown that has an optgroup with the members below and so on...
+-----------+------------+-----------+
| GroupName | MemberName | ValueName |
+-----------+------------+-----------+
| 1st Team | Joe Bob | Joe |
| 1st Team | Catherine | Kat |
| 2nd Team | Tommy | Tom |
| 3rd Team | John Razks | John |
+-----------+------------+-----------+
Table name: Members
Basically at the end result is such of the code below. It will be a dropdown with an optgroup called "1st Team" and have the members below ect. for 2nd Team and 3rd Team and so on.
<optgroup class="1st Team">
<option value="Joe">Joe Bob</option>
<option value="Kat">Catherine</option>
</optgroup>
<optgroup class="2nd Team">
<option value="Tom">Tommy</option>
</optgroup>
<optgroup class="3rd Team">
<option value="John">John Razks</option>
</optgroup>
Right now, this is how I get information from my SQL table. This works fine, but if I want to add a new GroupName then I would have to add a new code to my main page and I don't want to do that.
Trying make it dynamic so if the SQL table gets updated with a new GroupName, then a new optgroup class will appear in the dropdown and the members will be below.
<optgroup class="1st Team">
<?php
$conn = mysqli_connect("#connect_to_sql");
if(!$conn){
die("Connection Failed".myslqi_connect_error());
}
$result = mysqli_query($conn, "SELECT distinct MemberName from Members where GroupName = "1st Team" order by MemberName ASC");
while ($row = mysqli_fetch_assoc($result)){
unset($membername, $groupname);
$groupname = $row['GroupName'];
$membername = $row['MemberName'];
echo '<option value="'.$membername.'">'.$membername.'</option>';
}
?>
</optgroup>
I'm not positive at all on what to do. I've looked at other people's examples, but not sure how to approach this step.
First get all records from the database. create an array with keys as group name. Loop the new array to generate the desired output.
// query to get all records from database table
$result = mysqli_query($conn, "SELECT distinct MemberName,GroupName,ValueName from Members order by MemberName ASC");
while ($row = mysqli_fetch_assoc($result)){
// generate an array with keys as group name
$array[$row['GroupName']][] = $row;
}
// loop the array to create optgroup
foreach($array as $key=>$value){
// check if its an array
if(is_array($value)){
// create optgroup for each groupname
echo "<optgroup class='".$key."'>";
foreach($value as $k=>$v){
echo "<option value='".$v['membername']."'>'".$v['membername']."'</option>";
}
echo "</optgroup>";
}
}
I have not tested this but sure this will help you. And you can make improvements.
It's not the most elegant way, but you could build your group this way, what it's doing is checking the name of the group and changes that to a new optgroup each time the name changes, the $first var is just there so it doesn't add a closing tag the first time around.
I'm sure you can improve on this, but it should get you going.
It does kind of rely on a consistent naming convention for the group name, so as I say, I'm sure you could improve on it. I've also not included the connection checks as you've done that already in your example
$result = mysqli_query($conn, "SELECT * FROM Members ORDER BY GroupName");
$groupName = '';
$first = true;
echo '<select>';
while ($row = mysqli_fetch_assoc($result)){
if ($row['GroupName'] != $groupName) {
$groupName = $row['GroupName']; // Just set the new group name
if (!$first) { // Add a closing tag when we change the group, but only if we're not in the first loop
echo '</optgroup>';
} else {
$first = false; // Make sure we don't close the tag first time, but do after the first loop
}
echo '<optgroup label="' . $groupName . '">';
}
// We want to echo the options every loop so it's outside the if condition
echo '<option value="' . $row['MemberName'] . '">' . $row['ValueName'] . '</option>';
}
echo '</select>';
|room type|price|avroom|
|standard |50 |20 |
| super |60 | 7 |
|suite |80 | 4 |
if reservation is made for "room type" = standard
the value entered in the "no of rooms" field in the form should be subtracted from the value in the "avroom" for the "room type" on the database
the code below outputs the form
</select></td>
<th align=left>ROOM TYPE :</th>
<td>
<?php
echo "<select name=txttype>";
$qup="select * from tariff where avroom > 0";
$rs=mysql_query($qup);
while($res=mysql_fetch_row($rs))
{
echo "<option value='".$res[0]."'>".$res[0]."</option>";
}
echo "<select>";
echo "</td>";
?>
<tr>
<th align=left>NO OF ROOMS :</th>
<td><select name=txtroom>
<?php
for($i=1;$i<=20;$i++)
{
echo "<option value=$i>$i</option>";
}
?>
please if you need any more info let me know
First thing I noticed in your mysql table is there cannot have a column like "room type" in a table. It should be edited to "room_type".
First of all, if you only need to display the room type in the top select box, your sql should be edited to $qup="select room_type from tariff where avroom > 0";. Then post the selected values using a html form.
From the other end where the form is posted, try something like,
$room_type= $_POST['txttype'];
$rooms = mysql_real_escape_string($_POST['txtroom']);
if($room_type == "standard")
{
$query = "UPDATE tariff SET avroom=avroom-{$rooms} WHERE room_type='standard'";
// Now execute the above query here.
}
Basically I have a database with 66 bible books; some from old testament some from new. The bname value is the NAME of the book, while bsect has a value of O or N(new or old), how can I make my dropdown box dynamically display a book into an old or new optgroup based on whether its' bsect is O or N? My teacher said I have to make some array, but i have no idea how to do it. Any thoughts?
My database sample:
+-----------+-------+
| bname | bsect |
+-----------+-------+
| Genesis | O |
| Exodus | O |
| Leviticus | O |
+-----------+-------+
I don't want to have to rely on manually setting opgroups based on the NUMBER OF THE ENTRY, I want it to be dynamic based on value of bsect.
Right now I just have the following query with a select dropdown which puts the book into old or new based on its record number, but It will break if more books were to be added
$query = $mysqli->query("select distinct bname as Name from kjv");
?>
<select name="book">
<?php
$i=1;
while($option = $query->fetch_object()){
if($i==1) echo "<optgroup label='Old Testament'>";
else if($i==40) echo "<optgroup label='New Testament'>";
echo "<option value='$i'>".$option->Name."</option>";
$i++;
}
?>
Simply order by bsect and display different optgroups dynamically
<?php
$query = $mysqli->query("SELECT DISTINCT bsect, bname AS Name FROM kjv ORDER BY bsect");
?>
<select name="book">
<?php
$i = 1;
$bsect = "";
while($option = $query->fetch_object()){
if($bsect != $option->bsect) {
$bsect = $option->bsect;
echo "<optgroup label='{$bsect}'>";
}
else if($i==40) echo "<optgroup label='New Testament'>";
echo "<option value='$i'>".$option->Name."</option>";
$i++;
}
?>
Of course, then your books may be out of order. So what you would want to do is add a book-order column (border) that stores a number defining how to order the books in a given group, e.g.
ALTER TABLE kjy
ADD COLUMN border INT U?NSIGNED NOT NULL DEFAULT 0;
Then you can update the data to have the proper book order and do a query like this:
SELECT DISTINCT bsect, bname AS Name FROM kjv ORDER BY bsect, border;
Of course, this being the Bible, you aren't going to be adding books, so you can probably just define a static Book ID that defines the ordinality of each book. Then you could just sort by ID and know that "Old" and "New" books are coming out in the right order.
ALTER TABLE kjy
ADD COLUMN id INT UNSIGNED NOT NULL PRIMARY KEY BEFORE (bname);
This is how you can create 2 arrays in php,
$query = $mysqli->query("select distinct bname,bsect from kjv");
while($option = $query->fetch_object()){
if ($option->bsect == 'O'){
$books_old[] = $option->bname;
} elseif ($option->bsect == 'N'){
$books_new[] = $option->bname;
} else {
# Error collection - bsect not 'O' or 'N'
}
}
now you have 2 arrays which are lists of books; $books_old and $books_new.
I'd use the name as the value rather than an arbitrary index;
echo "<optgroup label='Old Testament'>";
foreach($books_old as $this_book){
echo "<option value=\"$this_book\">$this_book</option>";
}
echo "<optgroup label='New Testament'>";
foreach($books_new as $this_book){
echo "<option value=\"$this_book\">$this_book</option>";
}
What I want to achieve:
Insert data into database table using chained select list (the options values are taken from database table)
Requirement: for the first select list ("tip_cheltuiala"), the available options values must be only the ones that are not used in the rows already inserted (available options are: option 1, 2 and 3; I already inserted rows with option 1 and 3, and now only option 2 must be available)
1. the select list "tip_cheltuiala":
echo '<select name="tip_cheltuiala'.$row_chelt['id_factura'].'" id="tip_cheltuiala'.$row_chelt['id_factura'].'" class="selectContentIncasare">'.$opt_mod_inchidere->TipCheltuiala().'</select>';
and the function for that select list:
class SelectListModInchidere{
public function TipCheltuiala(){
//looking for options that are already in the table
$stmt_chelt_inchisa = $this->conn->prepare('SELECT tip_cheltuiala FROM cheltuieli_mod_inchidere');
$stmt_chelt_inchisa->execute(array());
$results_chelt_inchisa = $stmt_chelt_inchisa->fetchAll();
foreach($results_chelt_inchisa as $row_chelt_inchisa) {
$chelt_inchisa[] = $row_chelt_inchisa['tip_cheltuiala'];
}
print_r($chelt_inchisa); // returns options 1 and 3
for($i=0; $i < count($chelt_inchisa); $i++){
$stmt_tip_chelt = $this->conn->prepare('SELECT * FROM mi_categ_cheltuiala
WHERE tip_cheltuiala <> :chelt_inchisa');
$stmt_tip_chelt->execute(array('chelt_inchisa' => $chelt_inchisa[$i]));
$tip_cheltuiala = '<option value="0">selectati ...</option>';
while($row_tip_chelt = $stmt_tip_chelt->fetch()) {
$tip_cheltuiala .= '<option value="' . $row_tip_chelt['tip_cheltuiala'] . '">' . $row_tip_chelt['tip_cheltuiala'] . '</option>';
}
return $tip_cheltuiala;
}
}
}
$opt_mod_inchidere = new SelectListModInchidere();
There I have the first issue: the select list is populated with option 2 (that is correct) but also with option 3 - I can't figure out why.
2. the select list "mod_inchidere":
returns the option values according with the selected option in select list "tip_cheltuiala
echo '<select name="mod_inchidere'.$row_chelt['id_factura'].'" id="mod_inchidere'.$row_chelt['id_factura'].'" class="selectContentIncasare">
<option value="0">selectati ...</option>
</select>';
and the function for that select list (part of the same class as function TipCheltuiala):
public function ModInchidere(){
$stmt_mod_inch = $this->conn->prepare('SELECT * FROM mi_mod_inchidere WHERE categorie_cheltuiala = :categorie_cheltuiala');
$stmt_mod_inch->execute(array('categorie_cheltuiala' => $_POST['id_categ_cheltuiala']));
$mod_inchidere = '<option value="0">selectati ...</option>';
while($row_mod_inch = $stmt_mod_inch->fetch()) {
$mod_inchidere .= '<option value="' . $row_mod_inch['mod_inchidere'] . '">' . $row_mod_inch['mod_inchidere'] . '</option>';
}
return $mod_inchidere;
}
3. final step: according with the selected option in select list "mod_inchidere, I need to return a value (also stored in database) correlated with the options in select list "mod_inchidre", and put that values in a input field, so the user can (if he wants) modify that value.
At that step I have no idea how to accomplish that.
I can put the value in another select list, but: the user can't modify that value and is not the way to do it.
Please help me with that.
LE
table structures
mi_categ_cheltuiala -> | id | tip_cheltuiala | categorie_cheltuiala |
mi_mod_inchidere -> | id | categorie_cheltuiala | mod_inchidere |
cheltuieli_mod_inchidere (table where I need to insert the data) -> | id | tip_cheltuiala | categorie_cheltuiala | mod_inchidere | valoare |
to get the value that I need to put in the input field I need to interrogate the table "mi_categ_valoare" for the field "mod_inchidere"
mi_categ_valoare -> | id | mod_inchidere | valoare |
$_POST['id_categ_cheltuiala'] -> explanation:
through jQuery I fetch what is selected in this select list "tip_cheltuiala" and send the data to the method TipCheltuiala()
<script type="text/javascript">
$(document).ready(function(){
$("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").attr("disabled","disabled");
$("select#tip_cheltuiala<?php echo $row_chelt['id_factura']; ?>").change(function(){
$("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").attr("disabled","disabled");
$("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").html("<option>asteptati ...</option>");
var id_categ_cheltuiala = $("select#tip_cheltuiala<?php echo $row_chelt['id_factura']; ?> option:selected").attr('value');
$.post("class/select_mod_inchidere.php", {id_categ_cheltuiala:id_categ_cheltuiala}, function(data){
$("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").removeAttr("disabled");
$("select#mod_inchidere<?php echo $row_chelt['id_factura']; ?>").html(data);
});
});
</script>
and then I use a service file that will invoke the method TipCheltuiala()
To be honest: Non-English naming conventions making me dizzy :)
Issue: There I have the first issue: the select list is populated with option 2 (that is correct) but also with option 3 - I can't figure out why.
use below single query to get missing tip_cheltuila:
SELECT
tip_cheltuila
FROM
mi_categ_cheltuiala
WHERE
tip_cheltuila NOT IN (SELECT
tip_cheltuiala
FROM
cheltuieli_mod_inchidere);
Need to study other issues...