Make an SQL query a drop down menu - php

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>

Related

Show result of MYSQL select query in two div according to column name, using a single select query

I have a table with column names id, name, category, title,and news. Once select query executes I get all the required data from table but i need to split up this data into two div according to the category they belong. there are two different categories, say A and B.
I need to show all the data with a column category name A in one div and same with category B.
$sql= "SELECT id, title ,news ,category FROM news ";
$query=mysql_query($sql, $ex)or die(mysql_error()) ;
while($row=mysql_fetch_assoc($query)){
$title=$row['title'];
if($row['category']==' Latest News'){
echo $row['title'];
}
}
My code doesn't work. Any idea?
You can first separate out category wise data into separate arrays.
$cat1 = array();
$cat2 = array();
$sql= "SELECT id, title, news, category FROM news";
$query = mysql_query($sql, $ex) or die(mysql_error());
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
if($row['category'] == 'A')
{
$cat['title'] = $row['title'];
$cat['news'] = $row['news'];
$cat1[] = $cat;
}
else
{
$cat['title'] = $row['title'];
$cat['news'] = $row['news'];
$cat2[] = $cat;
}
}
}
Then you can use foreach to print the data into separate div
if(count($cat1) > 0)
{
foreach($cat1 as $category1_data)
{
echo "<div class='cat1'>";
// data of category A
echo "</div>";
}
}
if(count($cat2) > 0)
{
foreach($cat2 as $category2_data)
{
echo "<div class='cat2'>";
// data of category B
echo "</div>";
}
}

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

Inserting into table from dropdown using data from another table

i have a problem, I managed to list the option values for my dropdown from cms.php
$query = 'SELECT * FROM categories';
$result = mysql_query($query) or die(mysql_error());
echo "<select name='categories'>";
echo "<option value =''>Select Category</option>";
while ($row = mysql_fetch_array($result)){
$categories = $row['categories'];
echo "<option value='$categories'>$categories</option>";
}
echo "</select>";
It works, the dropdown menu lists all the data from the "categories" table.
HOwever i can't insert those values from the dropdown into another table.
$categories = isset($_POST['categories']);
echo $categories;
$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql="INSERT INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES
('$_POST[prod_brand]','".mysql_real_escape_string($_POST['prod_name'])."','{$categories}','$_POST[prod_price]',
'".mysql_real_escape_string($_POST['prod_desc'])."','$_POST[prod_quantity]','{$image}')";
this just outputs 1, from the echo $categories; it also stores 1 into the database. what did i do wrong? Help please.
As requested.
Remove the isset and () from $categories = isset($_POST['categories']);
change it to $categories = $_POST['categories'];

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.

Keeping lastly selected value from a dropdownlist after button click

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

Categories