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
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'm having issues getting the user selected text from the select tag. I've looked at the other similar questions but none really helps.
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<select name="Select" id="vals" onchange="this.form.submit()" class="btn btn-warning">
<option value="">-- Select Country --</option>
<?php
$user = new User();
$sql5 = DB::getInstance()->get('country', array('user', '=', $user->data()->username));
if (!$sql5->count()) {
echo 'No data';
} else {
foreach ($sql5->results() as $sql5) {
?>
<option value="country"><?php echo $sql5->name;?></option>'; <?php
}}
?>
</select>
</form>
</div>
The dropdown has a list of countries assigned to the user but when I click on it to output details on the country, nothing shows.
<?php
if (isset($_POST['Select']) && !empty($_POST['Select'])) {
$userSelection = $_POST['Select'];
$sql = DB::getInstance()->get('country',array('country', '=', $userSelection));
if (!$sql->count()) {
echo 'no data';
} else {
foreach ($sql->results() as $sql) {
echo 'data'; }
The query gets the data of the country selected from the country table. When I change the $userSelection in the query to a specific country name, the data for that country shows. I think the problem might be the option value within the select tag but I'm not sure.
The value is the same on all requests. Update to
<option value="<?php echo $sql5->country ?>"><?php echo $sql5->name;?></option>
This is assuming the identifying column is country since that is your where clause in the select query.
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
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
Hello I am really struggling with this. I was asked to develop a script to calculate oil price but cannot get it to work. I have been able to setup a form to update fuel price.
I have a table called fuel_price. In this table will be cost per litre of fuel which is stored under Price. For example if oil price per litre is £0.50 I need to multiply this value by value selected within form dropdown.
Can anyone please guide me on what I am supposed to do??
Ok heres an update code preview.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select name="fueltype">
<option>- Select fuel type -</option>
<option value="Diesel">Diesel</option>
<option value="Red Diesel">Red Diesel</option>
<option value="Home Heating Oil">Home Heating Oil</option>
</select>
<select name="qtylitres">
<option>- Qty in Litres -</option>
<option value="100">100</option>
<option value="200">200</option>
<option value="400">400</option>
<option value="500">500</option>
<option value="900">900</option>
<option value="1000">1000</option>
</select>
<input type="hidden" name="id" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
include 'mysql_connect.php';
$pdo = '';
$stmt = $pdo->prepare("SELECT `Oil` from `fuel_price` WHERE id = '1'");
if (!$stmt->execute()) { die($stmt->errorInfo[2]); }
$row = $stmt->fetch();
$price = $row['Oil'];
echo $_POST['qtylitres'] * $price;
?>
Anyone know where I am going wrong??
Thanks
<?php
//Connect to database here. In this example, I'll assume you connected using PDO
//Although the same logic applies on any engine.
$stmt = $pdo->prepare("SELECT `price` from `fuel_price` WHERE `type` = :type"); //Prepare a query
$stmt->bindValue(':type', $_POST['type']); //Assuming the first <select> is named type
if (!$stmt->execute()) { die($stmt->errorInfo[2]); } //Display an error and terminate script if query failed.
$row = $stmt->fetch(); //Assuming you have only one row, fetch should only be called once.
$price = $row['price'];
echo $_POST['qtylitres'] * $price; //Multiply quantity with price and print result.
?>
Note that I have not tested it, but it should work. Your markup is incomplete, it lacks the opening for the first <select>. Read the comments and you should be good to go.
assuming that you have a column 'price' in your table, and that the only result contains the correct price:
include 'mysql_connect.php';
if (isset($_POST['submit'])) {
// edit: added fueltype in the where clause
$fueltype = mysql_real_escape_string($_POST['fueltype']);
$q = "SELECT * FROM fuel_price WHERE id = '1' AND fueltype='$fueltype'";
$result = mysql_query($q);
$row= mysql_fetch_array($result);
$price = $row['price'] * $_POST['qtylitres'];
echo $price;