I have a drop down that is being populated from a static select. Then when a choice is made in the first drop down a query runs and the second drop down is populated from the database depending on selection in first select box. Here is the code. I'm having a problem displaying the second drop down with the data.
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT '$selectvalue' FROM accounts ");
echo '<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="'.$row['$selectvalue'].'">' . $row['$selectvalue'] . "</option>";
//echo $row['drink_type'] ."<br/>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>
<?php
$selectvalue = mysqli_real_escape_string($mysqli, $_GET['selectvalue']);
$result = mysqli_query($mysqli, "SELECT DISTINCT * FROM accounts WHERE col_name = '".$selectvalue."' ");
echo '
<select name="some_name">
<option value="">Please select...</option>';
while($row = mysqli_fetch_array($result)) {
echo '<option value="'.$row['col_name'].'">'.$row['col_name']."</option>";
}
mysqli_free_result($result);
mysqli_close($connection);
?>
Related
I want to write an IF statement for a dynamically generated dropdown menu, but I keep getting errors or it doesn't just work, what I am trying to do is compare two variables to see if they match from a data in the database
$loma = "Asokoro";
$row['locales'] is from locality table in the database
$row['locales'] = "Asokoro";
$row['locales'] = "Dutse";
$row['locales'] = "Mari";
$row['locales'] = "Cook";
That means if $loma which is Asokoro matches $row['locales'] = "Asokoro"; select it as the option menu
<select name="checkout_area_name" id="checkout_area_name" required>
$query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'";
$sql = mysqli_query($con, $query) or die(mysqli_error($con, $query));
$r = ' <option value="">Please Choose Locality</option>';
?>
<?php
while ( $row = mysqli_fetch_assoc($sql))
{
?>
<?php $r = $r . '<option value="'.$loma.'" if ("'.$loma.' == '.$row["locales"].'") selected="selected" >'.$row['locales'].'</option>'; ?>
<?php
}
echo $r;
?>
</select>
I am trying to select the options menu that has $loma and $row['locales'] matching but I keep getting errors or when I console.log, it does not produce the result i want.
You are outputting php script as html markup, try changing your code to:
<select name="checkout_area_name" id="checkout_area_name" required>
$query = "SELECT * FROM `locality` WHERE state_name = '$hi_state'"; $sql = mysqli_query($con, $query) or die(mysqli_error($con, $query)); $r = '
<option value="">Please Choose Locality</option>'; ?>
<?php
while ( $row = mysqli_fetch_assoc($sql))
{
$r = $r . '<option value="'.$loma.'" '.(($loma==$row["locales"])?'selected':'').'>'.$row['locales'].'</option>';
}
echo $r;
?>
</select>
I wonder if anyone can help.
I am trying to echo some SQL data into a <select> form with each <option> as a different row from the table.
For example if my table has two columns 'username' and 'category' and there are two rows for the same username, with the data:
"username: test, category: test1."
& second row as:
"username: test, category: test2."
How could I echo 'test1' and 'test2' as two options in a select form?
The table name is 'testtable' so my current $sql query is ("SELECT 'category' FROM 'testtable' WHERE 'username = \'$user\'")
$user is currently set to the $_SESSION['username']
Code examples would be really helpful and much appreciated. I just cant seem to get them to echo in examples I have found on forums.
try this
<select name="your_select_name" id="your_select_id">
<option value="">Select</option>
<?php
$username = $_SESSION['username'];
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
while($row = mysql_fetch_assoc($res))
{
echo '<option value="'.$row['category'].'">'.$row['category'].'</option>';
}
?>
</select>
UPDATE 2:
For distinct category use this
$res = mysql_query("SELECT DISTINCT(`category`) as category FROM `testtable` WHERE `username` = '$username' ") or die(mysql_error());
OR
$res = mysql_query("SELECT `category` FROM `testtable` WHERE `username` = '$username' GROUP BY category ") or die(mysql_error());
Try this:
<select>
<?php while($row = mysql_fetch_array($result)
{?>
<option><?php echo $row['category'];?></option>
<?php }?>
</select>
You got from your query all rows:
<?php
$query = "SELECT category FROM testtable WHERE username = '" . $_SESSION['username'] ."'";
$result = mysql_query($query);
$options = array();
while($row = mysql_fetch_array($result))
{
$options[] = "<option>" . $row['category'] . "</option>";
}
?>
<select>
<?php echo implode("",$options); ?>
</select>
Your select query has an error: unpaired single quote before username, and single quotes surrounding category - is that possible that was the reason you couldn't do it?
<?php
$result = mysql_query($query);
echo "<select>";
while($row = mysql_fetch_array($result))
{
echo "<option>".$row['category']."</option>";
}
echo "</select>";
?>
I have two SQL queries that display different results from my database. I'm using these results as navigation links in my nav bar.
At the moment all the results display as a single line of text, I want to make each SQL query display as a drop down menu, with all the results from the query as an option in the drop down.
Here's the code I'm using:
<?php
$q = "SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
echo "<a href='category.php?id=$row[0]'>$row[1]</a> ";
//display product categories
}
mysqli_free_result($result); //free result
$q = "SELECT brand_id, brand_name FROM Brand";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
echo "<a href='brand.php?id=$row[0]'>$row[1]</a> ";
//display product Brands
}
?>
use select option
echo "<select name='category'>";
while ($row = mysqli_fetch_row($result)){
echo "<option value='$row[0]'>$row[1]</option> ";
}
echo "</select>";
$q="SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'],$q);
$option1.="<select name='category'>";
while ($row = mysqli_fetch_row($result)){
$option1.="<option value='$row[0]'>$row[1]</option> ";
}
$option1.="</select>";
same for second
print this $option1 value in your view file
<select onchange="document.location.href='category.php?id='+this.value;">
<?php
$result = mysqli_query($_SESSION['conn'],$q);
while ($row = mysqli_fetch_row($result)):?>
<option value="<?=$row[0];?>"><?=$row[1];?></option>
<?php endwhile;?>
</select>
Just like the title says i'm having difficulties in achieving it.
Here's my dropdownlist:
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
}
echo "</select>";
?>
Here's the buttonclick:
<?php
if(isset($_POST['Submit']))
{
$query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' AND data ='".$_POST['data']."' group by paskirtis";
$result = mysql_query ($query);
echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
while($nt=mysql_fetch_array($result)){
if($nt['SUM(suma)'] != null){
$suma = $nt['SUM(suma)'];
}
echo "<tr><td>$nt[paskirtis]</td>
<td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
}
}
?>
After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!
Try this, you need to place select="selected" in the while loop. See below code how I placed the $selected
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
$selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
}
echo "</select>";
?>
My page stops loaded every time I turn on the code below... it looks correct and the tables and fields are correct.
<select name="common" style="width: 136px;">
<?php
$group1 = mysql_fetch_array(mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order"));
while($row = $group1){
echo "<option value=\"$group1\">$group1</option>\n";
}
?>
</select>
<?php
$group1 = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order");
while($row = mysql_fetch_array($group1)){
echo "<option value=\"$row[country]\">$row[country]</option>\n";
}
?>
Try this:
<select name="common" style="width: 136px;">
<?php
$recordset = mysql_query("SELECT country FROM lang_list WHERE grouping = '1' ORDER BY p_order") or die("Error found: " . mysql_error());
while($row = mysql_fetch_array($recordset)){
echo "<option value=\"".$row['country']."\">".$row['country']."</option>\n";
}
?>
</select>