Okay, as you can see in the code I'm trying to add the total up.. I have used a variable outside a while() before, but not this way. So if someone could point me in the right direction? I know what I need to make the form work, that's not the problem. My formatting is bad, but that's not the problem either so please no comments on it.
<?php
require 'core/config.php';
?>
<form onsubmit="sendToServer();">
CPU <select id="proc">
<?php
$cpuPrice = "";
$query = $con->query("SELECT * FROM partslist WHERE Category = 'CPU'");
while($row = $query->fetch_array(MYSQLI_BOTH))
{
$cpuPrice .= $row['ProductPrice'];
echo '<option value="'.$row['ProductName'].'">'.$row['ProductName'].' - '.$row['ProductPrice'].'</option>';
}
?>
</select><br>
RAM <select id="proc1">
<?php
$ramPrice = "";
$query = $con->query("SELECT * FROM partslist WHERE Category = 'RAM'");
while($row = $query->fetch_array(MYSQLI_BOTH))
{
$ramPrice .= $row['ProductPrice'];
echo '<option value="'.$row['ProductName'].'">'.$row['ProductName'].' - '.$row['ProductPrice'].'</option>';
}
?>
</select><br>
GPU <select id="proc2">
<?php
$gpuPrice = "";
$query = $con->query("SELECT * FROM partslist WHERE Category = 'GPU'");
while($row = $query->fetch_array(MYSQLI_BOTH))
{
$gpuPrice .= $row['ProductPrice'];
echo '<option value="'.$row['ProductName'].'">'.$row['ProductName'].' - '.$row['ProductPrice'].'</option>';
}
?>
</select><br>
HDD <select id="proc3">
<?php
$hddPrice = "";
$query = $con->query("SELECT * FROM partslist WHERE Category = 'HDD'");
while($row = $query->fetch_array(MYSQLI_BOTH))
{
$hddPrice .= $row['ProductPrice'];
echo '<option value="'.$row['ProductName'].'">'.$row['ProductName'].' - '.$row['ProductPrice'].'</option>';
}
?>
</select><br>
SSD <select id="proc4">
<?php
$ssdPrice = "";
$query = $con->query("SELECT * FROM partslist WHERE Category = 'SSD'");
while($row = $query->fetch_array(MYSQLI_BOTH))
{
$ssdPrice .= $row['ProductPrice'];
echo '<option value="'.$row['ProductName'].'">'.$row['ProductName'].' - '.$row['ProductPrice'].'</option>';
}
$total = $cpuPrice + $ramPrice + $gpuPrice + $hddPrice + $ssdPrice;
?>
</select><br><br>
<button type="submit">Build</button>
</form>
<div style="border:1px;border-color:black;">Total: $<?php echo $total; ?></div>
Edit - more in depth..
What I need is to display the total, but it's empty..
Try doing the math in the query itself, for example:
CPU <select id="proc">
<?php
$cpuPrice = 0;
$query = $con->query("SELECT *, (SUM(ProductPrice)) as CpuPrice FROM partslist WHERE Category = 'CPU'");
while($row = $query->fetch_array(MYSQLI_BOTH))
{
$cpuPrice = $row['CpuPrice '];
echo '<option value="'.$row['ProductName'].'">'.$row['ProductName'].' - '.$row['ProductPrice'].'</option>';
}
echo 'cpuPrice'.$cpuPrice;
?>
</select><br>
Related
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 have a dropdown in my form, In which data is being fetch from database, Problem is i want to keep the selected value in the dropdown if page reloads. Any help will be really appreciated.
Here is my code
<select name="ans_type" class="select-form " onChange="checkAnswer(this.value)" style="background-color: #fff !important;width:159px!important;">
<option value="" style="color:#000">Select</option>
<?php
$sql = "select * from (table name)";
$res = mysqli_query($dbhandle,$sql);
$numrows = mysqli_num_rows($res);
if($numrows){
while($obj = mysqli_fetch_object($res)){
if($ansTypeId == $obj->id){
echo '<option value="'.$obj->id.'" style="color:#000" selected>'.($obj->ans_type).'</option>';
}
else{
echo '<option value="'.$obj->id.'" style="color:#000">'.($obj->ans_type).'</option>';
}
}
}
?>
</select>
Try below code.
<select name="ans_type" class="select-form " onChange="checkAnswer(this.value)" style="background-color: #fff !important;width:159px!important;">
<option value="" style="color:#000">Select</option>
<?php
$selectStr = '';
$sql = "select * from (table name)";
$res = mysqli_query($dbhandle,$sql);
$numrows = mysqli_num_rows($res);
if($numrows){
while($obj = mysqli_fetch_object($res)){
$selectStr = ($ansTypeId == $obj->id) ? 'selected' : '';
echo '<option value="'.$obj->id.'" style="color:#000" '.$selectStr.'>'.($obj->ans_type).'</option>';
}
}
?>
</select>
Try this
selected = '';
if($ansTypeId == $obj->id){
$selected = "selected='selected'";
}
echo '<option value="'.$obj->id.'" style="color:#000" $selected>'.
($obj->ans_type).'</option>';
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
}
[....]
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 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>";
?>