Displaying database contents from drop down selection - php

In my website I have a page to display testimonials. I wrote this code to display my all testimonials from database.
This is my code so far :
while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {
$testimonial = $row['testimonial'];
//echo $testimonial;
$mytestimonial = nl2br($testimonial);
$city = $row['city_name'];
$name = $row['name'];
$url = $row['web_address'];
$imageName = $row['image_name'];
$type = $row['membership_type'];
}
With this code I can get all my testimonials to the page. Its pretty working for me. My problem is now I need to filter my testimonials according to its type. I have 3 different kind of testimonials in my database. (tutor, institute, student)
I am going to use a select box to filter the data. When selecting an option from select box I need to display testimonials according to that selected type.
<div class="filter-box">
<div id="select_box">
<form method="post" action="">
<div class="variation2">
<label>Filter By</label>
<select class="select">
<option>Tutor</option>
<option>Institute</option>
<option>Student</option>
</select>
</div>
</form>
</div>
</div>
Can anyone get me going in a direction here?
Thank You

Why not reload the select box dynamically (using AJAX) once the user has selected one of the 3 options and display your required testimonial.I guess that will solve your problem.

So you want to then select the testimonials based on their type
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
WHERE type = '$type'
ORDER BY date_added DESC LIMIT $start, $display";
Now you also want to get the type from the user
<select name="type" class="select">
With the POST
$type = $_POST['type'];
Auto submit the form on change example
Javascript:
<script type="text/javascript">
function submitform(){
document.frmType.submit();
}
</script>
Form:
<form name="frmType" method="post" action="">
<select name="type" class="select" onchange="submitform()">
Further examples can be found here

You have a couple options. The first is with ajax.
Second is submitting the form onchange or onsubmit:
<select class="select" name="type" onchange="document.forms[0].submit();">
<option value="Tutor">Tutor</option>
<option value="Institute">Institute</option>
<option value="Student">Student</option>
</select>
And your query:
$type = '';
if(!empty($_POST) && isset($_POST[type])){
$type = " WHERE testimonials.type = '".$_POST[type]."'";
}
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
".$type."
ORDER BY date_added DESC LIMIT $start, $display";
Of course make sure to check if the form was submitted.

add a class by the name to each of the testimonial divs
<div class="student">testimonial goes here</div>
now use jquery to hide/show the categories at one shot.
$('.student').show();
i hope it helps

Related

how to get dropdown value from left join table

I have problem when I get value from left join table, database from product table (https://ibb.co/zbpJP0z )and database from farm table !(https://ibb.co/gMKySpP )
I've tried to get the value from dropdown list, but the value is always from joining table
this is my form code
<form class="mb-2" id="addproductform" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="serial-number">Serial Number:</label>
<select class="serial-number form-control" name="serial-number" class="custom-select mb-3" id="newval">
<option>Serial Number</option>
<?php $q=mysqli_query($link,"SELECT produk.nama FROM produk LEFT JOIN farm ON produk.SN=farm.SN WHERE username=\"$_SESSION[username]\"");
while($d=mysqli_fetch_row($q)) {
echo "<option value=$d[0]>$d[0]</option>";
} ?>
</select>
</div>
</form>
..........................
and this is a jquery code
$(document).ready(function(e){
$("#save_addPlant").click(function(e){
e.preventDefault();
console.log("submit productform");
$("select.serial-number").change(function(){
var tp = $(this).children("option:selected").val();
});
namafarm=$("option:selected").val();
console.log(namafarm);
});
});
How can I take a value from farm.SN, while I display produk.nama in the dropdown list?
There are 2 places that need change in your code.
Your are using 2 class attributes in your select tag. The second
class attribute wont work. So you have to combine the 2 class
attributes into 1.
You are only selecting produk.nama column in your select query. You
must also select produk.SN column.
Replace your select block with the code below & check whether it works.
<select class="custom-select mb-3 form-control serial-number" name="serial-number" class="" id="newval">
<option>Serial Number</option>
<?php
$q = mysqli_query( $link, "SELECT produk.SN,produk.nama FROM produk INNER JOIN farm ON produk.SN = farm.SN WHERE username = \"$_SESSION[username]\"" );
while($d=mysqli_fetch_row($q)) {
echo "<option value=$d[0]>$d[1]</option>";
}
?>

How to auto selected save value from database in to select HTML tag

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.

Where clause if null show all, if where = $variable show $variable

I'm a bit stuck on a bit of code im doing.
Here's the code i already have
HTML code:
<form method="post" action="index.php" >
<select name="sortby" class="sortby" >
<option value=" ORDER BY id DESC">Date Added (Newest First)</option>
<option value=" ORDER BY id ASC">Date Added (Oldest First)</option>
<option value="ORDER BY clicks DESC">Website Clicks </option>
<input type="submit" value="Submit" />
</select>
</form>
<form method="post" action="index.php">
<select name="country" class="sortby">
<option value="">Country...</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
.... and so on
</select>
<input type="submit" value="Submit" />
PHP code:
$sortby = $_POST['sortby'];
$sortby = $mysqli->real_escape_string($sortby);
$country = $_POST ['country'];
$country = $mysqli->real_escape_string($country);
$results = $mysqli->query("SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $sortby");
while($row = $results->fetch_assoc()) {
print '<div id="linkmainwrapper"><div id="linkwrapper"><div class="linkimageborder"><div class="linkimage">'.$row["image"].'</div></div>';
print '<div class="linktitle">'.$row["title"].'</div>';
print '<div class="link">('.$row["link"].')</div>';
print '<div class="countryflag"><img src="http://www.hatblocksdirect.co.uk/lib/flags/'.$row["country"].'.gif"></div>';
print '<div class="linkdescription">'.$row["description"].'</div>';
print '<div class="clickcount">Website Clicks ('.$row["clicks"].')</div></div></div>';
what i would like to do is sort colums. the first sort box works, i can sort by website clicks, id etc using $sortby.
Ive passed the $country using a select drop down but i dont know how to implement this into my select query. I need it to display all entries if a country hasn't been selected. once a country is selected only display that country.
Any help very much appreciated
Put the additional condition of the WHERE clause in a variable depending on whether the parameter is supplied.
if (!empty($_POST['country'])) {
$country = "AND country = '" . $mysqli->real_escape_string($country) . "'";
} else {
$country = "";
}
$results = $mysqli->query("SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $country $sortby");
Use an if condition like
if(isset($_POST ['country']))
{ // if country is set
$country = $mysqli->real_escape_string($_POST ['country']);
$sql="SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 and country='$country' $sortby";
}
else
{ // if country is not se
$sql="SELECT id, link, image, title, description, country, clicks FROM isc_links WHERE approved=1 $sortby";
}
$results = $mysqli->query($sql); // execute the query

how to hold a selected list value after clicking go button?

I have a php page where a dropdown list is populated from the mysql database and when a user make a selection and click 'go' button, data regarding that selected item will display on page. Now everything is working perfectly in my page but the list item names are automatically display as default.
For example: there are 4 list items,Featured art, pop art, nature art, fantasy art. when user select pop art and press go button, the data related to this are displayed on page but the dropdown list does not hold pop art name as selected in list. Even then dropdown list shows Featured art as default. There is no problem of fetching data, but I want to display as selected pop art or nature art or fantasy art or featured art in dropdown list (not only the featured art)and its related data on page and so on. I think my point is clear to all. Help me please.
Here is code:
<select name="category">
<?php
$sql = "SELECT id, art_name FROM category;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
<option value="<?= $row['id']; ?>"><?= $row['art_name']; ?></option>
<?php
}
?>
</select>
<input name="go" type="submit" value="Go" /></div>
<div align="center" class="showimage">
<ul class="display">
<?php
$id = (int)$_POST['category'];
$sql_search = "SELECT id, categoryid, path FROM list WHERE categoryid = $id";
$search = mysql_query($sql_search);
$sql = mysql_query("SELECT autodisplay FROM list WHERE categoryid = 2");
if (isset($_POST['go'])) {
while ($row = mysql_fetch_assoc($search)) {
?>
<li><img src="<?= $row['path']; ?>" border="0"></li>
<?php }
}
else {
while ($row = mysql_fetch_assoc($sql)) {
?>
<li><img src="<?= $row['autodisplay']; ?>" border="0"></li>
<?php
}
}
?>
</ul>
This is the running page
Replace your select list with the following:
<select name="category">
<?php
$sql = "SELECT id, art_name FROM category;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
?>
<option value="<?= $row['id']; ?>"<?=($_POST['category']==$row['id'] ? ' selected="selected" : '')?>><?= $row['art_name']; ?></option>
<?php
}
?>
</select>
I've added an if statement to check if the row id matches the posted variable - if so set the option value to selected.

Auto submit form and MYSQL requery

I have been using this code to display some testimonials in my website.
This is code so far :
// Select testimonials from database
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id
ORDER BY date_added DESC LIMIT $start, $display";
$r = mysqli_query($dbc, $q);
if ( $records >= 1 ) {
while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {
$testimonial = $row['testimonial'];
//echo $testimonial;
$mytestimonial = nl2br($testimonial);
$city = $row['city_name'];
$name = $row['name'];
$url = $row['web_address'];
$imageName = $row['image_name'];
$type = $row['membership_type'];
echo '<div class="testimonial-row">
<div class="testimonial-image">
<img src="'.UPLOAD_DIR.$imageName.'" />
</div>
<div class="testimonial">
<h2>'.$name.'</h2>
<h3>';
if($type==1){
echo 'A teacher';
}elseif($type==2){
echo 'An Institute';
}elseif($type==3){
echo 'A Student';
}
echo " from <strong>$city</strong></h3>
<blockquote>$mytestimonial</blockquote>
<p class='user-url'><a href=''>$url</a></p>
</div>
</div>";
}
} else {
echo "There is no any testimonial to display at this time. Please try again later.";
}
After running above code I can display all my testimonials. I have 3 different kind of testimonials and at the moment display all kind of testimonials together in my page.
Now I am going a solution with a select box to filter and display them according to its type.
This is my select box :
<select class="select" name="type">
<option value="1">Tutor</option>
<option value="2">Institute</option>
<option value="3">Student</option>
</select>
When selecting an option from select box my filtering should happen with requering my original query with selected type. I got a solution with jquery show/hide function but its not match with desired result.
NOTE: I cant use a submit button with this select box. Thats why I am looking for a solution with Ajax or Jquery.
Can anyone tell me is it possible or not?
Thank you.
This may be Solution...
Pass your Value like this :
<select name="jumpMenu" class="sort-by-select" id="jumpMenu" onchange="MM_jumpMenu('parent',this,0)">
<option value=""> Select type </option>
<option value ="yourpage?type=1">Tutor</option>
<option value ="yourpage?type=2">Institute</option>
<option value ="yourpage?type=3">Student</option>
</select>
<script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore) //v3.0
{
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
After that get that value like this..
if(isset($_GET['type']) && $_GET['type'] != "")
{
$Q = $_GET['type'];
}
Put this value in Query
SELECT testimonial, city_name, name, web_address, image_name, membership_type
FROM testimonials
INNER JOIN city ON city.city_id = testimonials.city_id where membership_type='$Q'
ORDER BY date_added DESC LIMIT $start, $display
Here is code sample code that can give you some idea what I was suggesting to you ..
$(document).ready(function(){
$('#type').change(function(){
var data = $("#type").val();
$.ajax({
url : "yourdomain/yourfilterpage.php",
data: {'type':data},
type : 'POST' ,
success : function(data){ $('#divtoshowdata').html(data)}
});
})
})
<select class="select" name="type" id='type'>
<option value="1">Tutor</option>
<option value="2">Institute</option>
<option value="3">Student</option>
on yourfilterpage.php get your filtered result

Categories