I have about 10 categories and building a search form, But i want to show all categories & even a single category in option. How i can do that? So user can search in all categories with a single phrase.
<input type="text" class="form-control" value="" name="searchterm" id="searchterm" placeholder="Start Searching..." style="width: 60% "/>
<select id="fcat" class="form-control" name="catname" style="width:40%;" required />
<option value="">Select Category</option>
<?php $fetch_category = mysql_query("SELECT * FROM category ORDER BY categoryname ASC");
while($throw_category = mysql_fetch_array($fetch_category)) {
echo '<option value="">All Categories</option> <option value="'.$throw_category['categoryname'].'">'.$throw_category['categoryname'].'</option>'; } ?>
</select>
That would be more helpful if you guys could tell me a mysql query to search with a phrase in all categories. I have another table called companies and categories so if someone is search a phrase like "some business name" and select all categories then should show all companies within all categories.
Thanks in advance...
Remove 'All Categories' option from while loop.
Add some value for 'All Categories' option.
In the form action page based on the select box option write/generate a query.
Form code
<?php $fetch_category = mysql_query("SELECT * FROM category ORDER BY categoryname ASC");
while($throw_category = mysql_fetch_array($fetch_category)) {
echo ' <option value="'.$throw_category['categoryname'].'">'.$throw_category['categoryname'].'</option>';
} ?>
<option value="all">All Categories</option>
</select>
Related
How to auto select the save value fetched from database in to select HTML tag.
I have a list of rooms with Hotel name , room type , facilities , descriptions in a table. i want to edit that record . when i click on edit button it fetch room_id to edit row accordingly, all other values successfully auto fetched in textboxes except values in select tag.
Here is my code to fetch values from database and echo to corresponding textboxes , except select box . i would like to auto select value in select box.
$query = "SELECT * from room where room_id = '$id'";
$result = mysqli_query($connection, $query) or die ( mysqli_error());
while($row=mysqli_fetch_assoc($result))
{
$room_id=$row['room_id'];
$room_type_id=$row['room_type_id'];
$facilities = $row['facilities'];
$long_description=$row['long_description'];
}
?>
<form action="Edit_Room_Script.php" method="post" enctype="multipart/form-data">
<label class="form-label">Hotel Name</label>
<select class="form-control" name="name" id="name">
<?php
$sel_cus = "select hotel_name from hotels ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['hotel_id'];?>"><?php echo $row['hotel_name'];?></option><?php}?>
</select>
<label class="form-label">Room Type</label>
<select class="form-control" name="room_type" id="room_type">
<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>">
<?php echo $row['Room_Type_Name'];?></option>
<?php
}
?>
</select>
<label class="form-label">Facilities</label>
<input class="form-control" type="text" id="facilities" value="<?php echo $facilities;?>" name="facilities" autocomplete="Off" required >
<button class="btn btn-success btn-cons" type="submit" name="update" id="update"> Update Room</button>
</form>
i have a row in my html table is like below
Sr Hotel Name Roomtype Facility Action
1 Serena Super ABC Edit
When i click to edit button it take me to edit from where facility value successfully auto fetched from database and set in text box but hotel name and room type is not set. In select tag for hotel name and room type it populates all the hotel name and room type except serena and super how could i achieve this Please guide with some code.
Thank you
Some modifications:
1) Get $hotel_id
2) To show <select> element selected, you need to add selected="selected" attribute in your <option> tag.
If you have a drop down HTML:
<select class="form-control" name="room_type" id="room_type">
<option value="1">Villa</option>
<option value="2">Terrace</option>
<option value="3" selected="selected">Gallery</option>
<option value="4">Delux</option>
</select>
Drop down will show Gallery selected.
In your case, you can show it by:
<?php
$sel_cus = "select Room_Type_Id,Room_Type_Name from room_type ";
$res_cus = mysqli_query($connection, $sel_cus);
while ($row = mysqli_fetch_array($res_cus)) {?>
<option value="<?php echo $row['Room_Type_Id'];?>"
<?php if ($room_type_id == $row['Room_Type_Id']) { echo 'selected="selected"';}?>>
<?php echo $row['Room_Type_Name'];?></option>
<?php
}
?>
3) Also, first fetch the arrays from database and then loop over them in the <select> element as writing fetching code directly in <select> is a bad practise and in case of any issue, it my expose your database field names.
I have an query that I am trying to do search with multiple parameters to sort my data response from sql.
The query code :
$sql = "SELECT * FROM `apartments` WHERE `building_num`='{$i}' ORDER by `apartment_num` ASC";
The search form :
<form method="GET">
<input type="hidden" name="page" value="price">
<select class="styled-select2" name="type">
<option value="0">Apartment type</option>
<option value="דופלקס">Duplex</option>
<option value="דירה">Apartments</option>
<option value="דירת גן">דירת גן</option>
</select>
<select class="styled-select2" name="rooms">
<option value="0">rooms</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="5">5</option>
</select>
<select class="styled-select2" name="price">
<option value="0">Price</option>
<option value="700000-800000">700,000-800,000 ₪ </option>
<option value="800000-900000">800,000-900,000 ₪</option>
<option value="900000-1000000">900,000-1,000,000 ₪</option>
<option value="1000000-1100000">1,000,000-1,100,000 ₪</option>
<option value="1100000-1200000">1,100,000-1,200,000 ₪</option>
<option value="1200000-1300000">1,200,000-1,300,000 ₪</option>
<option value="1300000-1400000">1,300,000-1,400,000 ₪</option>
<option value="1400000">1,400,000 up</option>
</select>
<input type="hidden" name="action" value="search">
<button type="submit">Search</button>
</form>
There are three parameters but they are not required, could the customer only use 'apartment type' AND 'Price' OR only one parameter, I tried for few hours to write query with no success, Any one can help ?
// Vars on table apartments
// Rooms - > Rooms
// Price -> price
// Apartment type -> type
1st of all, there's no direct link between and query (or queries) and tge options you're giving to the customer...
If you want to then you can allow the customer to fill some of the fields and if you don't then no... it's a matter of decision, from which derives the development
There's missing example code, so I can obly answer regarding the query and add some remarks
Since you've given specific select options, then you should use it for specific searches
I would also use some validations on the $_POST information returned
Regarding the query, I need to assume that all of the options are saved in the apartments table, under that, then the query should be something like this :
$sql = "SELECT * FROM `apartments` WHERE
rooms = $rooms
OR `type` = $type
OR (price BETWEEN *$range_min* AND *$range_max*)
ORDER by `apartment_num` ASC";
If you want all parameters combined you should use AND instead of OR
and, again, you can decide you want both and use different queries for each scenario the user givea
In this case you have to build dynamic query as below :-
$sql = "SELECT * FROM `apartments` WHERE ";
$condition = "";
$condition .= ($rooms=='')?"":" or 'rooms'= $rooms ";
$condition .= ($type=='')?"":" or 'type'= $type ";
$condition .= ($price=='')?"":" or prince bewteen 'range_min' AND 'range_max'";
$sql .= $condition;
$sql .= " ORDER by `apartment_num` ASC";
In this way you can make all three filters free to select or not.
I want to convert my dropdown to ajax auto search.
Here is my dropdown, when i select any company name it displays other 2 dropdowns by taking the id.
<select name="company" class="form-control" onChange="showSubcat(this);" required>
<option value="">Select Company</option>
<?php $s1 = mysqli_query($con, "select * from company") or die (mysqli_error($con));
while($s2 = mysqli_fetch_array($s1)) { ?>
<option value="<?php echo $s2['id']; ?>"><?php echo $s2['company']; ?></option>
<?php } ?>
</select>
In showSubcat(), i display 2 dropdowns from database.
As i have huge company names stored in database, its difficult to select from dropdown , so i want to convert this into ajax auto search. I tried to do like this
<input type="text" name="company" class="typeahead tt-query" autocomplete="off" spellcheck="false" placeholder="Company Name">
Here i am facing 2 issues
This is taking company name , but i want to take company id so that
i can display other 2 dropdowns
Not getting how to display 2 drop down based on this value, not getting how to call onChange()
I am working with creating an ad posting page. I have included dependable select boxes as the category selection system. All the values are being pulled from MySQL database. The table is composed of an id (auto increment) bridged by master_id and identified by category name. Every category has a subcategory. So the user is forced to pick a subcategory before being able to submit the info. I am currently looking for ideas of how to post info that has been input by user depending on the category chosen. I have created an individual page for each subcategory. So every time an input is submitted, I want it to display in the page accordingly. For example, if I pick category: Books and subcategory:Textbooks then post it in textbooks.php. Live EXAMPLE
Table Structure
Drop down menus
<form action="" method="post">
<select name="category" id="category" size="7" class="updateCategory" >
<option selected="selected" value="">Select one:</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="updateCategory"
disabled="disabled" hidden="hidden" >
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
</br>
Insert your name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
This is a little bit of a shot in the dark, as the question was vaguely worded.
I think that, first, you will need to make an update to the database. You will need to create either a new table or a new column. I would personally choose the table as that provides more flexibility, like so: id, category_id, category_page (and you could put other columns in too). Obviously, category_id references back to the main table category.
Then, do a select like this:
SELECT [COLUMNS HERE] FROM category RIGHT JOIN category_page ON category_page.category_id = category.id
You now have the values you need.
Then, in your PHP, return the url to the page (if you are using JS to dynamically show the new page):
<option value="xyz" data-url="url-for-page.php">Category Name</option>
I'm making a website, where I should be able to delete some brands from a database, the brands is stored in categories.
In my form, I have a that echo's out all the categories in my database.
The next should then contain all brands that is within' my selected category.
My categories contains a categoryID which is the value of the in the first dropdown - The brands is having it's own ID, but also having the categoryID, from the category they're in.
How would you do this? - And can you give an example, it might need javascript or jquery, which is fine with me.
This is my code:
<form action="deleteBrand.php" method="post">
<fieldset class="delete">
<legend>Delete Brand</legend>
<div>
<label for="categoryid">Category:<br />
<?php
$stmt = $db->prepare("SELECT categoryid, category FROM categories");
$stmt->execute();
echo "<select name='categoryid' onchange='this.form.submit()'>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='{$row['categoryid']}'>{$row['category']}</option>";
}
echo "</select>";
?>
</label>
</div>
<div>
<label for="brand">Brand Name:<br />
<?php
$stmt = $db->prepare("SELECT brands.id, brand FROM brands WHERE brands.categoryid = ")
?>
</label>
</div>
<div>
<input type="submit" value="Delete Brand" />
</div>
</fieldset>
</form>
I'm still missing the last dropdown, but if I just can get the value from the selected item from the first dropdown, into a variable, it shouldn't be hard for the rest of the code!
Thank you a lot
The below code is an example of how you can get the selected option value for a dropdown box. Depending on your needs, you can either preload your category id->brand mappings into a javascript variable that you can refer to, or use an ajax request to get the appropriate brands for your second combo box on demand, when the box is clicked.
<script>
function getSelected(obj)
{
alert(obj.options[obj.selectedIndex].value);
// Now use AJAX or preloaded variable to figure out
// what brands map to this category, and populate
// your second combobox
}
</script>
<form action="deleteBrand.php" method="post">
<label for="categoryid">Category:<br />
<select name='categoryid' onchange=getSelected(this)>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
</form>