Once users fill out the "main_search" form, a list of results should populate, depending on their selection on the previous page. Either the entire list pops up or nothing at all. Below, my HTML starts here and then I need the results to populate on the next page. Please, please, please help!!
For a reference, check this site out: www.lemassif.com
<form name="main_search" action="displaydata.php" id="nl-form" class="nl-form" method="post">
<select name="main1">
<option value="featured" selected>any city</option>
<option value="city1">City 1</option>
</select>
<select name="reason">
<option value="family" selected>family</option>
<option value="romantic">romantic</option>
<option value="business">business</option>
<option value="leisure">leisure</option>
</select>
<select name="budget">
<option value="modest" selected>modest</option>
<option value="moderate">moderate</option>
<option value="lavish">lavish</option>
</select>
<div class="nl-submit-wrap">
<button class="nl-submit" type="submit" name="submit">Create my trip</button>
</div>
Here is my displaydata.php page that should populate the filtered results on the next page.
<?php
// Include the connection file.
include("func.inc.php");
$sql = mysql_query("SELECT * FROM venues");
if(isset($_POST['submit'])) {
$search_term = mysql_real_escape_string($_POST['$venues']);
$sql .= "WHERE city = '{$search_term}'";
$sql .= "OR reason = '{$search_term}'";
$sql .= "OR budget = '{$search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<table cellpadding="5" cellspacing="5">
<tr>
<td><strong>Venue Name</strong></td>
<td><strong>Image</strong></td>
<td><strong>Description</strong></td>
</tr>
<?php
while($row = mysql_fetch_array($sql)) { ?>
<tr>
<td><?php echo $row['venue_name']; ?></td>
<td><?php echo $row['image']; ?></td>
<td><?php echo $row['description']; ?></td>
</tr>
<?php }
?>
</table>
Here is my func.inc.php file.
<?php
include_once 'connection.php';
function close(){
mysql_close();
}
function city_query(){
$myData = mysql_query("SELECT city FROM venues");
while($record = mysql_fetch_array($myData)){
echo '<option value="' . $record['city'] . '">' . $record['city'] . '</option>';
}};
function reason_query(){
$myData2 = mysql_query("SELECT reason FROM venues");
while($record2 = mysql_fetch_array($myData2)){
echo '<option value="' . $record3['reason'] . '">' . $record2['reason'] . '</option>';
}};
function budget_query(){
$myData3 = mysql_query("SELECT budget FROM venues");
while($record3 = mysql_fetch_array($myData3)){
echo '<option value="' . $record3['budget'] . '">' . $record3['budget'] . '</option>';
}};
function search_venues() {
$city = $_POST['city'];
$reason = $_POST['reason'];
$budget = $_POST['budget'];
//Do real escaping here
$query = "SELECT * FROM venues";
$conditions = array();
if($city !="") {
$conditions[] = "city='$city'";
}
if($reason !="") {
$conditions[] = "reason='$reason'";
}
if($budget !="") {
$conditions[] = "budget='$budget'";
}
$sql = $query;
if (count($conditions) > 0) {
$sql .= " WHERE " . implode(' AND ', $conditions);
}
$result = mysql_query($sql);
return $result;
}
?>
What am I missing? Thanks in advance!
I think you're builting a malformed query in displaydata.php
It should be:
<?php
// Include the connection file.
include("func.inc.php");
$sql = "SELECT * FROM venues "; //<----note here: no mysql_query() and a blank at the end
if(isset($_POST['submit'])) {
$search_term = mysql_real_escape_string($_POST['$venues']);
$sql .= "WHERE city = '{$search_term}' "; //<----note here: blank at the end
$sql .= "OR reason = '{$search_term}' "; //<----note here : blank at the end
$sql .= "OR budget = '{$search_term}' "; //<----note here: blank at the end
}
[....]
Related
Hello guys I'm new to OOP PHP and I want to imitate the retaining of user generate data in form from procedural
here is my code:
<?php
class View_LType extends Config{
public function view_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo "
<tr class='table-head'>
<td class='numbers'>$data[leave_id]</td>
<td>$data[leave_code]</td>
<td>$data[leave_name]</td>
<td class='small'>$data[leave_description]</td>
<td><a href='#'></a></td>
</tr>";
}
}
public function select_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo '<option value="' . $data['leave_name'] . '">' . $data['leave_name'] . '</option>';
}
}
}
html page:
<div class="form-group">
<label for="ltype">Leave Type</label>
<select name="ltype" id="ltype" name="ltype" required >
<option>Select leave...</option>
<?php $view_leave->select_ltype(); ?>
</select>
</div>
$data[leave_name] is what the user will select what I want is to echo selected when the user select its prefered leave name. please help me guys
If the current $data['leave_name'] is the same as the submitted "ltype", then select that option:
echo '<option value="' . $data['leave_name'] . '"' . (($data['leave_name'] == $_REQUEST['ltype']) ? ' selected' : '') . '>' . $data['leave_name'] . '</option>';
how to fix this syntax for this logic ?
i want to select my select option to select the another select option
<?php
$query_string = "SELECT * FROM products";
$query_string1 = "SELECT * FROM suppliers where ProductID = // firstSelectoption(value)";
$query_string2 = "SELECT * FROM categories";
$query = mysql_query($query_string);
$query1 = mysql_query($query_string1);
$query2 = mysql_query($query_string2);
?>
and in the body i make
<select name="first" id="first" onchange="childrenOnChange(this.value)">
<?php
while ($row = mysql_fetch_array($query)) {
echo '<option value=' . $row["ProductID"] . '>';
echo $row['ProductID'];
echo '</option>';
}
?>
</select>
<select name="second" id="second">
<?php
while ($row = mysql_fetch_array($query1)) {
echo '<script>';
echo 'var arr = array(';
$row['SupplierID'] . ',';
echo ')';
echo '</script>';
}
?>
</select>
i want to set the second select option value with $query1;
If you get the value from the query with $val = mysql_fetch_array($query1), then you can include the following in your loop:
$selected = '';
if ($row['ProductID'] == $val) {
$selected = "selected";
}
echo '<option value="'.$row['ProductID'].'" '.$selected.'>'.$row['ProductID'].'</option>';
I want for my users to be able to search for multiple criteria from the search form.
Here is the HTML:
<form id="form_search" action="search.php" method="post">
<input class="search" type="text" name="search" placeholder="Search for a property for sell or rent" autocomplete="off">
<select name="min_val" class="control_option option_select min_val">
<option selected>Property Type</option>
<option>Homes for Sale</option>
<option>Homes to Let</option>
<option>Student Accomodation</option>
<option>Commercial</option>
<option>Land and Sites</option>
<option>Auctions</option>
<option>Agricultural</option>
</select>
<select name="max_val" class="control_option option_select max_val">
<option selected>Max Price</option>
<option>£25,000</option>
<option>£50,000</option>
<option>£100,000</option>
<option>£150,000</option>
<option>£200,000</option>
<option>£250,000</option>
<option>£300,000</option>
<option>£350,000</option>
<option>£400,000</option>
<option>£450,000</option>
<option>£500,000</option>
<option>£600,000+</option>
</select>
<select name="beds" class="control_option no_bed">
<option selected>Bedrooms</option>
<option>1 Bedroom</option>
<option>2 Bedrooms</option>
<option>3 Bedrooms</option>
<option>4 Bedrooms</option>
<option>4+ Bedrooms</option>
</select>
<input class="search_btn" name="search_button" type="submit" value="Search" />
</form>
I can get it to search my database okay, but I am stumped for getting it to search for multiple things at once.
Here is the PHP:
<?php
$search = $_GET['search'];
$terms = explode(" ", $search);
$query = "SELECT * FROM test WHERE";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= " keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
if ($i == 1)
$query .= " price BETWEEN 0 AND '%$each%'";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_array($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$price = $row['price'];
$link = $row['link'];
echo "<h1>$title</h1><br />
$description<br />$price<hr />";
}
} else {
echo "<b>Sorry we found no properties matching '$search', please try another term.</b>";
}
mysql_close();
?>
This is arough idea about how to proceed. there are better way to write this with function.
<?php
$search = $_GET['search'];
$min_val = $_GET['min_val'];
$max_val = $_GET['max_val'];
//$terms = explode(" ", $search);
$query = "SELECT * FROM test WHERE";
$init=1;
if (isset($search) ){
$query .= (($init==1)?'':' OR ')." keywords LIKE '%$each%' ";
$init=($init==1)?0:$init;
}
if (isset($min_val) ){
$query .= (($init==1)?'':' OR ')." price BETWEEN $min_val AND '$max_val' ";
$init=($init==1)?0:$init;
}
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0 ) {
while ($row = mysql_fetch_array($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$price = $row['price'];
$link = $row['link'];
echo "<h1>$title</h1><br />
$description<br />$price<hr />";
}
} else {
echo "<b>Sorry we found no properties matching '$search', please try another term.</b>";
}
mysql_close();
?>
I have select (combo boxes) in PHP and after one was selected i fill the second one with data from the database.
the question is how to display my selection from the first combo box after the page reload.
I have the following code:
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value =""></option>
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['name'].'">'.$row1['name'].'</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30" >
<option value =""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
// Connect to server
$con = mysql_connect($ServerAddress,$ServerUser,$ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)){
echo '<option value ="'.$row1['race'].'">'.$row1['race'].'</option>';
}
}
?>
</select>
Method how to get values are defined in form tag in attribute method
In your case you need to get echo $_POST['category'] and compare it with name of option
Your code are not perfect. Look follows: :)
<?php
require_once("config.php");
// Connect to server
$con = mysql_connect($ServerAddress, $ServerUser, $ServerPassword);
//choose DB
mysql_select_db($DbName, $con);
?>
<select name="category" id="category" maxlength="30" onchange="this.form.submit();">
<option value=""></option>
<?php
$val = $_POST['category']?:'';
$sql = "SELECT * FROM `category` ORDER BY `name`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
$selected = ($val == $row1['name'] ? 'selected="selected"' : '');
echo '<option value ="' . $row1['name'] . '" '. $selected .'>' . $row1['name'] . '</option>';
}
?>
</select>
</span>
<label>race:</label>
<span>
<select name="race" id="race" maxlength="30">
<option value=""></option>
<?php
if (isset($_POST['category'])) {
$var = $_POST['category'];
$sql = "SELECT * FROM `race` WHERE `category`='" . sqlSecure($var) . "'ORDER BY `race`";
$res = mysql_query($sql);
while ($row1 = mysql_fetch_array($res)) {
echo '<option value ="' . $row1['race'] . '">' . $row1['race'] . '</option>';
}
}
?>
</select>
I have two tables, tblPlayer(PlayerID, PlayerName,PlayerTeam(int) ) and tblTeams (TeamID, TeamName)
On a PHP page I have a function to find the selected player, function is as follows,
function find_selected_player() {
global $sel_player;
if (isset($_GET['id'])) {
$sel_player = get_player_by_id($_GET['id']);
} else {
$sel_player = NULL;
}
}
my other function as follows,
function get_player_by_id($player_id) {
global $conn;
$query = "SELECT * ";
$query .= "FROM tblPlayer ";
$query .= "WHERE PlayerID =" . $player_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $conn);
confirm_query($result_set);
if ($player = mysql_fetch_array($result_set)) {
return $player;
} else {
return NULL;
}
}
So on the form to edit I can get all values like
<input type="text" name="PlayerName" value="<?php echo $sel_player['PlayerName']; ?>" />
But...:) when I try to fill up a combo box with the reverse way I am stuck there. When adding something this works like a charm but $sel_player['PlayerTeam'] is giving me only ID :(
<?php include "conn.php" ?>
<?php include "function.php" ?>
<?php find_selected_player() ?>
<h2>Edit: <?php echo $sel_player['PlayerName'] ." ". $sel_player['PlayerLname']; ?></h2>
<form action="player.php" method="post">
<table>
<tr>
<td>Name: </td>
<td><input type="text" name="PlayerName" value="<?php echo $sel_player['PlayerName']; ?>" /></td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="PlayerLname" value="<?php echo $sel_player['PlayerLname']; ?>" /></td>
</tr>
<tr>
<td>Team:</td>
<td>
<?php
$sql = "SELECT TeamName, TeamID FROM tblTeam";
$result = mysql_query($sql);
echo '<select name="TeamName"><option>';
echo "Choose a team.</option>";
echo '<option selected>' . $sel_player['PlayerTeam'] . '</option>';
while ($row = mysql_fetch_array($result)) {
$team_name= $row["TeamName"];
$team_id = $row["TeamID"];
echo "<option value=\"$team_id\">$team_name</option>";
}
echo "</select>";
?>
</td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Save" /></td>
<td align="right"> </td>
</tr>
</table>
</form>
<?php ob_end_flush() ?>
<?php include ("footer.php") ?>
You should update your code to use MySQLi_*, or PDO. I've gotten you started here. Try this out and see how it works for you.
function getPlayerByID($pid = '')
{
global $conn;
if($pid == '')
return false;
$sql = mysql_query("SELECT * FROM tblPlayer WHERE PlayerID = '$pid'", $conn);
$row = mysql_fetch_array($sql);
if(mysql_num_rows($sql) == 1)
return $row;
else
return false;
}
$id = isset($_GET['id']) ? $_GET['id'] : '';
$sel_player = getPlayerByID($id);
$sql = mysql_query("SELECT TeamName, TeamID FROM tblTeam");
$select = '<select name=""><option>Choose a Team</option>';
while($row = mysql_fetch_array($sql))
{
$team_id = $row['TeamID'];
$team_name = $row['TeamName'];
$selected = $team_id == $sel_player['PlayerTeam'] ? 'selected' : '';
$select .= '<option ' . $selected . ' value="' . $team_id . '">' . $team_name . '</option>';
}
$select .= '</select>';
echo $select;
$team_adi should be $team_name
Change:
echo "<option value=\"$team_id\">$team_adi</option>";
to:
echo "<option value=\"$team_id\">$team_name</option>";
<?php
$sql = "SELECT TeamName, TeamID FROM tblTeam";
$result = mysql_query($sql);
$player_id = $_GET['id'];
$current_team = mysql_query("SELECT
tblteam.TeamID,
tblteam.TeamName,
tblplayer.PlayerID,
tblplayer.PlayerTeam,
tblplayer.PlayerName
FROM
tblplayer
INNER JOIN tblteam ON tblplayer.PlayerTeam = tblteam.TeamID
WHERE PlayerID = $player_id LIMIT 1 ");
$my_row = mysql_fetch_array($current_team);
?>
<select name="TeamName">
<option selected value="<?php echo $my_row['TeamID']; ?>"> <?php echo $my_row['TeamName']; ?> </option>
<?php
while ($row = mysql_fetch_array($result)) {
$team_name= $row["TeamName"];
$team_id = $row["TeamID"];
echo "<option value=\"$team_id\">$team_name</option>";
}
echo "</select>";
?>