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

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..

Related

How to use a selection to generate an sql query

I need to use the selection from and HTML select element as one of the variables in my sql query. Meaning if the user selects "Iphone" from the list, I want to query to be something like select * where name = Iphone.
I do not know how to go about this, thank you.
This code creates the select based on the query:
<option disabled selected value> -- Select a forum -- </option>
<?php
$con = mysqli_connect("host","user","pass","db");
if (!$con) {
die('Connection failed: ' . mysqli_connect_error() . '<br>');
}
$productName='SELECT p.name
FROM product as p
JOIN ownedproducts as o on o.productID = p.productID
WHERE usersID =2;';
$result=mysqli_query($con, $productName);
while ($row = mysqli_fetch_assoc($result)) {
unset($id);
$id = $row['name'];
echo '<option value="'.$id.'">'.$id.'</option>';
}
</select>
I would like to take what the user selects, including multiple selections, and build a table based on that something like this where the AND p.name = selected is replaced with what would actually work in this case. I think javascript is the way to go but I do not know enough.
<?php
$query1 = $db->query('SELECT p.productID, p.name, p.company, o.prodtype AS Type
FROM ownedproducts AS o
JOIN product as p ON p.productID = o.productID
WHERE o.usersID = 2
AND p.name = selected');
while ($row = $query1->fetch())
{
if . $row['name'] . = selected:
echo "<tr id=" . $row['productID'] . ">";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "</tr>";
}
?>
Edited for more detail.
Thanks in advance
Though, I am not sure what are you asking for but select statement for exact match will be like below=>
select * from tablename where name='Iphone';
or if you want partial match then you can use wildcard(%) and like operator given below =>
select * from tablename where name like '%Iphone%';

Basic populating of a select form

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.

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