Writing an IF statement in a dynamic dropdown select menu gives ERROR - php

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>

Related

Displaying mysqli_fetch array results in dropdown php

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);
?>

How do I echo SQL data from database table into a <select> form?

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>";
?>

Population a dropdown list with PHP without duplicates

I am trying to embed a drop down list to an update page, it works fine but I am having problems with option selected part. When an option is shown it also duplicates it in the list.
Is there any way I can say if record is used do not use it again with PHP?
<?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>";
?>
It seems like simple if condition will solve the case for you:
while ($row = mysql_fetch_array($result)) {
$team_name= $row["TeamName"];
$team_id = $row["TeamID"];
if($team_id != $my_row['TeamID']){
echo "<option value=\"$team_id\">$team_name</option>";
}
}
Additionally you should always sanitize $_GET / $_POST params, in your example:
$player_id = intval($_GET['id']);
Intval will return 0 if the given format is not numeric, so your sql query is safe from this moment.

get all values in where clause

Am facing troubles in this code, i just want to get all data from table row if the user selected "show all" from the select drop menu.
here is the select menu !
so, this menu grab data from this table, but if he selects All, what is the suitable code to echoing in between option value :)
<b>speciality:</b> <select id="main_mav" name="speciality">
<option value="none">Select speciality:</option>
<option value=""> All specialities </option>
<?php
$result = mysql_query('SELECT speciality FROM visits') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['speciality'].'">'.$row['speciality'].'</option>';
}
?>
</select><br />
That's the Submit form !
if ($region=="All regions" ){
$region=$_POST['""'];
}
else ( $region=$_POST['region']);
$date1 =$_POST['from_date'];
$date2 = $_POST['to_date'];
$product=$_POST['product'];
$speciality=$_POST['speciality'];
$type=$_POST['visit_type'];
sql="SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
$result=mysql_query($sql); ## This line is new.
$num=mysql_numrows($result);
$row = mysql_fetch_array($result);
What's the correct code to enter if user selected " show all in drop menu " ?!
You really need to sanitize your inputs, at least with mysql_real_escape_string!
On to your actual question: just check if $speciality is empty, and generate a different query without the (speciality ='$speciality') condition.
Since your HTML referenced 'specialties' and your PHP referenced 'regions' I'm gonna just stick with 'regions', but here's the idea.
if ($region=="All regions" ){
$sql = 'SELECT id, customer_name, seller_1_name, seller_2_name, FROM visits';
} else {
$region = mysql_real_escape_string($_POST['region']);
$date1 = mysql_real_escape_string($_POST['from_date']);
$date2 = mysql_real_escape_string($_POST['to_date']);
$product = mysql_real_escape_string($_POST['product']);
$speciality = mysql_real_escape_string($_POST['speciality']);
$type = mysql_real_escape_string($_POST['visit_type']);
$sql = "SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
}
$result = mysql_query($sql); ## This line is new.
$num = mysql_numrows($result);
$row = mysql_fetch_array($result);

mysql_fetch_array problem code not processing

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>

Categories