Drop down list with data from database - php

First of all I would like to let you know that I tried every other solution that I found here regarding to my topic.. But nothing worked for me!
As the title says I want to create a drop down list with data from a database!
The connection with the database is right so I don't show you the code! I have tried many ways, one is below.
The only thing that I am getting is a blank list.Thanks!
<select name="Anaktisi">
<?php
$query2="select name from books WHERE sub_ID=1";
$result2 = mysqli_query($con,$query2);
while ($row2 = mysqli_fetch_array($result2)) { ?>
<option value="<?php echo $row2['name'];?>"> </option>
<?php } ?>
</select>

Related

Use mysql query result in more than one place in code

I'm building a system that tracks contact lenses. I'm storing the contact lens info in a database as sometimes prices/availabilities change and i access this info from multiple points in the program. I'm trying to interface with this list using a dropdown by doing "SELECT * FROM contacts" as a query. my code looks like this :
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
Then I echo that list out in a while loop using PHP to populate the options in the dropdown.
My question is this: I have these dropdowns for each eye on the same form. So it's "Brand Right Eye"....other miscellaneous info about the right eye....then "Brand Left Eye". But ONLY the right eye is populating with the brand info because it appears first in the code. What i'm having to do is copy/paste the exact same query and do
$contact_list2 = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
then later if I need the dropdown again, I need to do $contact_list3..and so on. Why can i not generate a drop down using the same variable? Why does it stop responding to calling the variable after the first execution of it and is there any work around that I can implement that would allow me to not have to copy/paste the same query with a different variable association each time?
just for refernce, my php while code is this:
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
while($row = mysqli_fetch_array($contact_list))
{
?>
<option value = "<?php echo($row['brand'])?>" name = "brandOS">
<?php echo($row['brand']) ?>
</option>
<?php
}
?>
</select>
I have this loop copy/pasted for right eye and left eye. But it only works on which ever drop down appears first in the code.
A possible solution will be more efficient in term of performance could be :
<?php
$left_eye = '<option value="0">Please Select</option>';
$rigth_eye = '<option value="0">Please Select</option>';
while($row = mysqli_fetch_array($contact_list))
{
//logic for left eye
$left_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
//logic for rigth eye
$rigth_eye .= <<<HTML
<option value ="{$row['brand']}" name = "brandOS">
{$row['brand']}
</option>
HTML;
}
?>
<select class="form-control" name = "brandOS">
<?php echo $left_eye ; ?>
</select>
<select class="form-control" name = "brandOS">
<?php echo $rigth_eye ; ?>
</select>
With this solution you get your result in the same while loop. If the left and right select are the same you can use the same variable.
Store the brands in an array, then you can just loop through the array.
<?php
$contact_list = mysqli_query($link, "SELECT brand FROM contacts ORDER BY brand");
$brands = array();
while($row = mysqli_fetch_array($contact_list))
{
array_push($brands, $row['brand']);
}
?>
<select class="form-control" name = "brandOS">
<option value="0">Please Select</option>
<?php
foreach($brands as $brand){
?>
<option value = "<?php echo($brand[0])?>" name = "brandOS">
<?php echo($brand[0]) ?>
</option>
<?php
}
?>
</select>
You can use a PHP array, like the SESSION one, to store values and use them across your site. Be sure you call "session_start()" method on each page you use that array, though.
//Initialize sessions
session_start();
...
//Right after getting result from query
$_SESSION['contact_list'] = $contact_list;
To use it, just be sure to call the method I told you above, and call the variable with the same syntax:
<?php
while($row = mysqli_fetch_array($_SESSION['contact_list'])) { ?>
Hope this helps.

How to populate dependant select menus dynamically using jQuery, PHP and MySQL?

I have searched quite a bit on here about this topic. But I could not find a solution for my problem. I'd appreciate it a lot if you could help me, this is for a school project I am working on.
I have a database with a table ("Main_table") and columns including "sector" and "sub_sector". I want to have two select boxes, first one will load all the records from database in "sector" column and the second one will load all the records from database in "sub_sector" column depending on the selection value of the first select box. (For example: If I select "plastics" on the first select box, then second select box should be updated with sub_sector values where sector value is equal to "plastics").
I have managed to load the options values from database for the first select box but when I click on any selection, it does not load any option to the second select box. You can find the codes below. I did not put "sector_options.php" below, as it seems to work just fine.
index.html shown below:
<script>
$(document).ready(function() {
$('#filter_sector')
.load('/php/sector_options.php'); //This part works fine - uploads options to the first select box
$('#filter_sector').change(function() {
$('#filter_subsector').load('/php/subsector_options.php?filter_sector=' + $("#filter_sector").val()
} //This part does not work - no options on the second select box
);
});
</script>
<body>
<div id="sectors"><p>Sector:</p>
<select id="filter_sector" name="select_sector" multiple="multiple" size="5"> </select>
</div>
<div id="subsectors"><p>Sub Sector:</p>
<select id="filter_subsector" name="select_subsector" multiple="multiple" size="5"> <option value="" data-filter-type="" selected="selected">
-- Make a choice --</option>
</select>
</div>
</body>
</html>
sector_options.php shown below:
<?php
$link = mysqli_connect("*******", "*******","******","********") or die (mysql_error());
$query = "SELECT sector FROM Main_table ";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {
$options .= "<option value=\"".$row['sector']."\">".$row['sector']."</option>\n ";
}
echo $options;
?>
subsector_options.php shown below:
<?php
$link = mysqli_connect("********", "*****,"*******", "********") or die (mysql_error());
$Sectors = $_REQUEST['filter_sector'];
$query = "SELECT sub_sector FROM Main_table WHERE sector='$Sectors'";
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_assoc($result)) {$options .= "<option value=\"".$row['sub_sector']."\">".$row['sub_sector']."</option>\n ";
}
echo $options;
?>
For completeness, the solutions were:
Check how AJAX operations are doing using a browser network monitor
Load AJAX fetcher scripts in a browser tag - in many cases they will render quite happily there, allowing them to be more easily debugged
AJAX scripts that return HTML for injection should only return that HTML, and not a full HTML document.

Drop down menu with data from MySQL datebase as options

i try to get data from mysql db into html dropdown , i execute the query in PHPmyadmin and its work fine , the result is one record,and all the website is connected with theses details of MYSQL my code is :
<?php
mysql_connect("localhost", "root", "1212") or die("Connection Failed");
mysql_select_db("test")or die("Connection Failed");
$query = "SELECT department_name FROM department";
$result = mysql_query($query);
?>
<label for="department" > Department Name </label>
<select name="departments" >
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['field'];?>"> <?php echo $line['field'];?> </option>
<?php
}
?>
</select>
the output is drop-down with one empty record , any one can help me in that ?
First required statement: mysql_ is deprecated. mysqli_ should be used instead.
Second ...
Did you try $line['department_name'] instead of $line['field'] ?
Well first of all you should print the results of the query to ensure the array structure is what you think it is.
This would have shown you that there is no column in the result set named field as you seem to believe due to this line of code:
<option value="<?php echo $line['field'];?>"> <?php echo $line['field'];?> </option>
As to why you only get one option, my first bit of advice will probably shed some light on said issue as well.

MySQL and PHP delete row from dropdown

I wish to use dropdown to list all rows from database. When I select one option, delete that selected row with button.
Here what I have by now, this only shows dropdown:
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Nije se moguće konektovati: ' . mysql_error());
}
mysql_select_db("videoteka", $con);
$result = mysql_query("select ime,id from filmovi");
$options="";
echo "Odaberite film:";
while ($row=mysql_fetch_array($result)) {
$id=$row["id"];
$ime=$row["ime"];
$options.="<OPTION VALUE=\"$id\">".$ime.'</option>';
}
mysql_close($con);
And in body:
<select name=thing>
<option value=0><?=$options?></option>
</select>
Ok, so two things, just to try and put you in the right direction: first, in the body you can remove the <option> tag, since your php variable already contains those.
<select name=thing>
<?=$options?>
</select>
Second, the delete part. There are different ways to do this, but one thing you will certainly need is an HTML form. Your select (and the button) will need to be in this form, which will be submitted to this very same PHP page. In the beginning of your PHP code you will check the $_POST variable to determine what row to delete. I hope you know what $_POST is, otherwise this is going to be a pretty useless explanation.

Multiple initially selected values in a dynamic select list with PHP

Thanks in advance for your help. This list is to update an existing record, so it's populated from a database with $rs_fullcat then checked with another recordset $rs_cat for the initially selected values. I can only get it to initially select one value, the first one in $rs_cat, but I need it to select all of the existing options from the database. I feel like I'm close but I can't find the answer anywhere. Here's the code:
<select name="category" multiple="multiple" id="category">
<?php
do {
?>
<option value="<?php echo $row_rs_fullcat['categoryID']?>"<?php if (!(strcmp($row_rs_fullcat['categoryID'], $row_rs_cat['categoryID']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_fullcat['category_name']?></option>
<?php
} while ($row_rs_fullcat = mysql_fetch_assoc($rs_fullcat));
$rows = mysql_num_rows($rs_fullcat);
if($rows > 0) {
mysql_data_seek($rs_fullcat, 0);
$row_rs_fullcat = mysql_fetch_assoc($rs_fullcat);
}
?>
</select>
What you want to do, is first select (and fetch) all the selected ones from the database, and put them in a variable ($rs_cat in your case). Then, in your while loop it's a simple matter of doing an in_array() check to see if the value is selected.

Categories