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>';
Related
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 am trying to edit my form. I want to get selected value selected in selection list.
I have created function to store values in database, and it works. Below is html code I use and function below to insert values in database.
// insert values in database
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" required>
<?php dobavljac() ?>
</select>
function dobavljac(){
$sql=mysqli_query($link, "SELECT * FROM `partneri` WHERE `Dobavljac`='1'
order by `PartnerId` asc ");
echo '<option value="">Izaberi dobavljača</option>';
while($record = mysqli_fetch_array($sql)) {
echo '<option value= "' .$record['PartnerId']. '">' . $record['PartnerNaziv'] . ' </option>';
}
}
// edit values
First I retrieve information from database
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = $conn->query($sql);
$r = $q ->fetch();
if ($r) {
$dobavljac=$r['Dobavljac'];
I want to get selected value in box
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; ?>">
<?php dobavljac() ?>
</select>
Probably I am not doing it the right way, any advice would be appreciated
Try this
<label>Dobavljač</label>
<select class="form-control" name="dobavljac" value="<?php echo $dobavljac; selected?>">
<option value=<?php echo $dobavljac?> selected>
<?php dobavljac() ?>
</option>
</select>
Try this...
$id=$_GET['id'];
$sql = "SELECT * FROM materijali where Id=$id ";
$q = mysql_query($query);
echo "<select name="dobavljac" class="form-control">";
while (($row = mysql_fetch_row($q)) != null)
{
echo "<option value = '{$row['Dobavljac']}'>";
echo $row['Dobavljac'];
echo "</option>";
}
echo "</select>";
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>
I wanted to make a dropdown area by getting the values from a mysql table. This code does not seem to work; all it prints out is the box for the dropdown without the content, how can I get this to work? or is there an alternative procedure in doing this?
<?
$connection = mysql_connect("localhost", "root", "");
mysql_select_db("test", $connection);
$query = "SELECT full_name FROM test";
$names = mysql_query($query);
function dropDown($content, $result)
{
while($row = mysql_fetch_row($result))
{
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
}
$content=<<<EOT
<html>
<body>
<select name="name">
EOT;
dropDown($content, $names)
$content.=<<<EOT
</select>
</body>
</html>
EOT;
echo $content;
?>
return the string. PHP is not C where you use out parameters just because they are sometimes handy.
function dropDown($result, $fieldName)
{
$content = '';
while($row = mysql_fetch_row($result)) {
$name = $row[0];
$content .= "<option value='$name'>$name</option>";
}
return '<select name="'.$fieldName.'">'.$content.'</select>';
}
$content = '<html><body>';
$content .= dropDown($names, 'name');
here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT username FROM userregistraton";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>";}
echo "</select>";
here username is the column of my table(userregistration)
it works perfectly
or simply the code is
<?
$sql = "SELECT * FROM userregistraton ";
$result = mysql_query($sql); ?>
<select name="username" id="username">
<option value="">Select user</option>
<?php
while ($row = mysql_fetch_array($result))
{ ?>
<option value="<?php echo $row['uid']?>" <?php if($row['uid']==$_POST["username"]) echo "selected"; ?> >
<?php echo $row['username'];?></option>
<?php
} ?>
<?php echo $form->dropDownList($model,'courseprefer', array(
'(*value in database table*'=>'displaying value',
)); ?>
you can manually add them just make sure that the first value in array is in the database table.. :)
to avoid error!
How do I keep the selected item after I submit the page? I have the country dropdown list with the following code.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]."\"";
if($_POST['country'] == $row['country_id'])
echo 'selected';
echo ">".$row["country_name"]."</option>";
}
?>
<?php
while($row = mysql_fetch_array($Result))
{
if ($_POST['country'] == $row["country_id"]) {
echo "<option value=\"".$row["country_id"]."\" selected="selected">".$row["country_name"]."</option>";
} else {
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
}
?>
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST['country'] == $row["country_id"]) echo 'selected="selected" ';
echo "value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
This code depends if you are trying to keep the value after you submit back to the original page.
<?php
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px">
<option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option ";
if($_REQUEST["yourSelectName"] ==$row["country_id"])
echo ' selected = "selected" ';
echo " value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
}
?>
</select>
Or you can do it all inline... less code, doesn't require all the if/then/else stuff
Change
echo "<option value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
To
echo "<option ". (($_POST['country'] == $row["country_id"]) ? 'selected ' : '') ."value=\"".$row["country_id"]."\">".$row["country_name"]."</option>";
If it is get (passed in URL), the use GET
<?php
$selectedid = 5; //example of selected if before submitting
$SQL = "SELECT countr_id, country_name FROM countries";
$Result = mysql_query($SQL) or die(mysql_error());
?>
<select name="country" style="width:400px"> <option value='-1'></option>
<?php
while($row = mysql_fetch_array($Result))
{
echo "<option value=\"".$row["country_id"]." ".(($selected==$row["country_id"])?"SELECTED":"")."\">".$row["country_name"]."</option>";
}
?>
//assume you have $result = array(your result list);
<select name='question'>
<?php
foreach ($result as $question) {
if ($_POST['question'] == $question) {
$selected = "selected";
} else {
$selected = '';
}
echo "<option value='" . $question . "' $selected>$question</option>";
}
?>
</select>