Basic populating of a select form - php

Basically trying to populate a drop-down select bar from a table called "Cars" with data in the "VIN" column. It's giving me a blank.
<form>
<label>VIN:</label>
<select name="formVIN">
<?php
$sql = "SELECT VIN FROM Cars";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['VIN'] . "'>" . $row['VIN'] . "</option>";
}
echo "</select>";
?>
</form>
I would like to take it a step further by displaying ONLY the VIN's of those cars not sold. I have a column in the table "Cars" that has either a 'Y' or 'N' if sold. So I would like it to show only the ones with 'N' as well.

$sql = "SELECT VIN FROM Cars WHERE VSold='N'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<option value=" . $row["VIN"] . ">" . $row["VIN"] . "</option>";
}
} else {
echo "<option value=''>ALL SOLD</option>";
}
Figured I'd try a different approach, and while the WHILE did make sense, this isn't too bad either. I'm still learning, a lot, and some of your comments helped me see things from a different perspective. I tried a lot of different options, thank you all.

Related

Drop down menu from multiple fields

This is an addition to the previous questions I asked. In my drop down box for engravers I would like the box to collect the surnames from three fields, Engraver1Surname, Engraver2Surname and Engraver3Surname. I also want each name only to appear once and to be in alphabetical order. My code doesn't work which means I've got it wrong again. This stuff is really challenging and I'm loving it but I don't want to be a serial pest.This is what I tried.
$sql = "SELECT DISTINCT Engraver1Surname, Engraver2Surname, Engraver3Surname FROM engravers Where Engraver1Surname, Engraver2Surname, Engraver3Surname <> '' AND Engraver1Surname, Engraver2Surname, Engraver3Surname IS NOT NULL ORDER by Engraver1Surname, Engraver2Surname, Engraver3Surname";
$result = mysql_query($sql);
echo "<select name\\='Engraver1Surname'>";
echo "<option value='$_POST'>Engraver</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['Engraver1Surname'] . "'>" .$row['Engraver1Surname'] . "</option>";
}
echo "</select>";
$name=array('Engraver1Surname','Engraver2Surname','Engraver3Surname');
$option=array();
while ($row = mysql_fetch_array($result)) {
for($x=0;$x<3;$x++){
$option.=$row[$name[$x]];
}
}
asort($option);
for($y=0;$y<sizeof($option);$y++){
echo "<option value='" . $option[$y]. "'>" .$option[$y] . "</option>";
}
echo "</select>";
Hope it fix to your question

Select from dropdown -> compare -> if match -> show picture

Good day all,
I've got a code that reads the users from a database and puts them in a dropdown menu:
<?php
mysql_connect('', '', '');
mysql_select_db ("");
$sql = "SELECT id,name FROM jos_users";
$result = mysql_query($sql);
echo "<select name='deelnemers' onchange='copyId2textinput(this);'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";
?>
Now i've got another database called jos_comprofiler with also an ID and also a avatar (image).
I was wondering if somebody give me some advise to compare the ID's from the 2 tables and then show the picture.
So for example, if i click on a user in the dropdown, it must look if there's a ID match with the other table, and if there is, show the picture from 'avatar'.
Thank you for your help and excuse me for my bad english!
Query can be:
SELECT `ju`.`id`, `ju`.`name`, `jcp`.`avatar` FROM `jos_users` as `ju`
LEFT JOIN `jos_comprofiler` as `jcp` ON (`ju`.`id` = `jcp`.`id`)
Here we use a left join, which means the jos_comprofiler does not need to exist for every jos_users. In those cases the field 'avatar' will be NULL.
Then you have in row the element 'avatar' which can be either NULL or a value.
if($row['avatar'] != NULL) echo "<img src=\"".$row['avatar']."\">";
or something :) good luck
There would be some ways but I'll show fast one.
<?php
mysql_connect('', '', '');
mysql_select_db ("");
$sql = "SELECT u.id, name, avatar FROM jos_users AS u LEFT JOIN jos_comprofiler USING(id)";
$result = mysql_query($sql);
echo "<div id='imgContainer'></div>";
echo "<select name='deelnemers' onchange='showAvatar(this.value);'>";
$avatars = array();
while ($row = mysql_fetch_array($result)) {
if($row['avatar']){
$avatars[$row['id']] = $row['avatar'];
}
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";
?>
<script>
var avatars = <?=json_encode($avatars)?>;
//alert(avatars[5]);
var avatarContainer = document.getElementById('imgContainer');
function showAvatar(id) {
if(avatars[id]===undefined) return false;
avatarContainer.innerHTML = '<img src="/path/'+avatars[id]+'" />';
}
</script>
This should work, with some modification for your code: img path, etc..

How to get the value from the primary key AND foreign key when joining mySQL tables?

I have two tables, employee as the parent and license as the child. They both have a Lic_ID column for reference, this column is the PK in license and the FK in employee. The license table also has a column Lic_Type which holds the name of the license.
I am trying to create a table with list boxes so the employee table can be updated. The list box value needs to be populated with the license.Lic_ID and the license.Lic_Type is to be displayed in the option. Here is what I have:
(Employee name, Id, etc. called out up here)
<?php
echo "<select name=\"Lic\">";
echo "<option value=\"\">Select...</option>";
$sql = $mysqli->query("SELECT Lic_ID, Lic_Type FROM license");
while($row = $result->fetch_assoc())
{
echo "<option value=\"" . $row['Lic_ID'] . "\">" . $row['Lic_Type'] . "</option>";
}
echo "</select>";
?>
So that works good, it shows the license type and has the value set to the license id. What I want to do is have <option selected="selected"> if the license id is set for an employee. This code doesn't work, but I think it illustrates what I'm trying to do:
<?php
echo "<select name=\"Lic\">";
echo "<option value=\"\">Select...</option>";
$sql = $mysqli->query("SELECT license.Lic_ID, license.Lic_Type, employee.Lic_ID FROM employee INNER JOIN license ON employee.Lic_ID = license.Lic_ID");
while($row = $result->fetch_assoc())
{
echo "<option value=\"" . $row['license.Lic_ID'] . "\"";
if($row['employee.Lic_ID'] = $row['license.Lic_ID']){echo "selected=\"selected\";}
echo ">" . $row['license.Lic_Type'] . "</option>";
}
echo "</select>";
?>
Is there a way to accomplish what I'm trying to do?
I think there may have been some confusion on what exactly I was trying to accomplish, I apologize for not being very clear. Anyways, I stumbled over the answer today, so I thought I should post it.
$sql1 = ("SELECT Emp_Name, Lic_MAT_ID FROM employee");
if(!$result_employee_query = $mysqli->query($sql1))
{
die ("There was an error getting the records from the employee table");
}
while($employee = $result_employee_query->fetch_assoc())
{
echo "Employee Name: " . $employee['Emp_Name'] . "<br>";
echo "License: ";
echo "<select>";
$sql2 = ("SELECT Lic_MAT_ID, Lic_MAT_Type FROM license_mat");
if(!$result_license_query = $mysqli->query($sql2))
{
die ("There was an error getting the records from the license table");
}
while($license = $result_license_query->fetch_assoc())
{
echo "<option value=\"" . $license ['Lic_MAT_ID'] . "\"";
if($license['Lic_MAT_ID'] == $employee['Lic_MAT_ID'])
{
echo " selected=\"selected\"";
}
echo ">" . $license ['Lic_MAT_Type'] . "</option>";
}
echo "</select><br>";
}
As I understand your problem: You want to see if the License has been added to ANY users or has it been unassigned. If any of the users have the license set then it's "selected", othervize not.
First you have to assign the keyword "multiple" to your select object to make it a listbox:
echo "<select name=\"Lic\" multiple=\"multiple\">";
Second: I would write this kind of query:
$sql = $mysqli->query("SELECT l.Lic_ID, l.Lic_Type, e.cnt FROM licence l left outer join (select Lic_id, count(*) cnt from employee group by Lic_id) e on l.Lic_ID=e.Lic_id");
It selects the Lic_ID, Lic_Type and the count of how many employees have the respective Lic_ID set to it (left outer join)
and in the code just check, if the count is higher then 0
if($row['cnt'] > 0){
echo "selected=\"selected\";
}

Populating select and input box based on a table using PHP and MYSQL

I have a table cene_pretplatne_stanarske which has two columns 'lice' and 'cene'. The first column 'lice' should populate a select, dropdown menu, and the other column, 'cene', should populate a input box, based on the selection of the dropdown menu. I tried this:
<?php
mysql_connect('localhost', 'xxxxx', 'xxxxxxx');
mysql_select_db('xxxxxxx');
mysql_set_charset('utf8');
$sql = "SELECT * FROM cene_pretplatne_stanarske";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$cena = $row ["cena"];
$sql = "SELECT lice FROM cene_pretplatne_stanarske WHERE lice LIKE 'C0%'";
$result = mysql_query($sql);
echo "<select name='lice' onchange='document.getElementById(\'form1\').submit();'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['lice'] . "'>" . $row['lice'] . "</option>";
}
echo "</select>";
echo "<input type='text' value='$cena' />";
?>
but it returns empty select box, and the input box with the value of the first row of 'cene' column. Please help.
Check how many (if any) results are being returned:
$i=0;
while ($row = mysql_fetch_assoc($result)) {
echo "<option value='" . $row['lice'] . "'>" . $row['lice'] . "</option>";
$i++;
}
echo $i after the end of </select>

I have PHP populating a Dropdown box, and I want it to also populate two read only Text boxs from the same Table

I have a table called Parts, (PartID, PartName, Cost) and I have PHP populating a Drop down box with the PartID's. I want it to when a user selects a Part ID, it populates one text box with the Part's name and the other box, with the Part's cost.
Here's what my Code look's like for the drop down box if it helps any :)
$sql = "SELECT PartID FROM Parts WHERE PartID LIKE 'C0%'";
$result = mysql_query($sql);
echo "<select name='PartID'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['PartID'] . "'>" . $row['PartID'] . "</option>";
}
echo "</select>";
I don't know what you're exactly trying to do, but this sounds more like javascript, to me.
Set a onchange function on your dropdown and get it to fill the fileds with name and cost that you would have stock into a javascript array before. Sounds ugly but it would work
You'll have to POST back to retrieve the values, either with a simple POST or an AJAX POST
if (!empty($_POST)) {
$partid = $_GET["PartID"];
$sql = "SELECT * FROM Parts WHERE PartID = '$partid'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$part_name = $row ["PartName"];
$part_cost = $row ["PartCost"];
}
$sql = "SELECT PartID FROM Parts WHERE PartID LIKE 'C0%'";
$result = mysql_query($sql);
echo "<select name='PartID' onchange='document.getElementById(\'form1\').submit();'>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='" . $row['PartID'] . "'>" . $row['PartID'] . "</option>";
}
echo "</select>";
echo "<input type='text' value='$part_name' />";
echo "<input type='text' value='$part_cost' />";

Categories