php mysql drop down list - php

I have a PHP form that shows data from a MySQL table. Each row obviously has different data, what I want to do is have a drop down list that displays data relevant to the data entry in each row.
for example, lets say I have two tables. Fruit and Fruit_Colors, as below:
so if my PHP Form was displayed as below, the MySQL data named fruits would display the data in the Fruit column. The color would then be fetched from the Fruit_Colors table depending on the PHP form output value in field 'Fruit'. so the drop down list for each row will be different.
my PHP form table syntax is:
<table id="hor-minimalist-a">
<tr>
<th>ID</th>
<th>Fruit</th>
<th>Color</th>
</tr>
<? while($row = $fruits->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['fruit']; ?></td>
<td><SELECT NAME="fruitcolor" id="fruitcolor">
<OPTION VALUE=0 >
*// what goes here???*
</option>
</SELECT>
</td>
</tr>
<? } ?>
</table>
Any advice how I can complete this would be appreciated. remember this table could be up to 50 rows so need a dynamic way of passing the 'fruit' value to the drop down list.
Syntax I know for the drop down list populations is:
function fruitcolor_dropdown($db)
{
$result = $db->query("select color from Fruit_Color where Fruit=*'outputted value'*");
return $result;
}
$colors= fruitcolor_dropdown($db);
while($row = $colors->fetch(PDO::FETCH_ASSOC)) {
$color=$row["color"];
$optionsfruitcolors.="<OPTION VALUE=\"$color\">".$color;
}
Advice appreciated as always. Thanks and Regards.

It will be bad idea to connect and fire query two time when you can do it in single query-
you can see demo
$query = "select f.*,group_concat(color SEPARATOR '|') as fcolor from fruit F Left join fruit_color fc using (fruit) group by fc.fruit";
Above will be your query and you will loop it like below:
<? while($row = $fruits->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['fruit']; ?></td>
<td><SELECT NAME="fruitcolor" id="fruitcolor">
<OPTION VALUE=0 >
<?php
$array = explode("|", $fcolor);
$count = count($array);
for($loop=0;$loop<$count;$loop) {
echo "<option>".$array[$loop]."</option>";
}
?>
</option>
</SELECT>
</td>
</tr>
<? } ?>

Check this out. We create a function to generate the dropdown options. It accepts the DB and the fruit as parameters --> loops through and makes the DOM --> outputs it to the browser.
PHP Function
function getColors($db, $fruit)
{
$result = $db->query(
sprintf("select color from Fruit_Color where Fruit = '%s'",
$fruit
)
);
$output = '';
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
$output .= sprintf(
'<option value="%s">%s</option>',
$row['color'],
$row['color']
);
}
return $output;
}
Template
<? while($row = $fruits->fetch(PDO::FETCH_ASSOC)) { ?>
<tr>
<td><? echo $row['id']; ?></td>
<td><? echo $row['fruit']; ?></td>
<td><SELECT NAME="fruitcolor" id="fruitcolor">
<?php echo getColors($db, $row['fruit']); ?>
</SELECT>
</td>
</tr>
<? } ?>

use this in selectbox
function fruitcolor_dropdown($db)
{
$result = $db->query("select color from Fruit_Color where Fruit=*'outputted value'*");
return $result;
while($row = $colors->fetch(PDO::FETCH_ASSOC)) {
$color=$row["color"];
$optionsfruitcolors.="<OPTION VALUE=\"$color\">".$color;
}
}
<select name="fruitcolor" id="fruitcolor">
<?php $colors= fruitcolor_dropdown($db); ?>
</select>

Related

Displaying query rows dependent on another query's result

I have a function in PHP that prints a list of inventories with their IDs.
Another function lists the products in the inventory with the corresponding ID from the inventory list.
But I don't know how to get the inventory ID from the list correctly into the product list, and it doesn't work for me. I don't know exactly where I'm doing wrong.
When I list the products with a specific ID and refresh the page, I want the list to stay the same.
A function to get a list
<?
php function fetchSeznamInventur() {
global $userCon;
global $rowcount;
global $query;
global $inventuraId;
global $row;
$query = mysqli_query($userCon, "SELECT * FROM seznamInventur");
//$inventuraId = $row["id"];
$rowcount = mysqli_num_rows($query);
}
?>
List listing to HTML
<?php
<table border="1" class="invTable">
<thead>
<th>Datum</th>
<th>ID</th>
<th>Název</th>
</thead>
<?php
for ($inv = 1; $inv <= $rowcount; $inv++) {
$row = mysqli_fetch_array($query);
?>
<tr>
<td><?php echo $row["createdate"]?></td>
<td name="id"><?php echo $row["id"] ?></td>
<td><?php echo $row["name"]?></td>
<td><input type="submit" name="submit" value="Detail"></td>
</tr>
<?php
}
?>
</table>
?>
Obtaining products by inventory ID
<?php
function fetchInventura() {
global $userCon;
global $inventuraId;
global $rowcount;
global $query;
$query = mysqli_query($userCon, "SELECT * FROM inv WHERE inventuraId='$inventuraId'");
$rowcount = mysqli_num_rows($query);
echo $inventuraId;
}
?>
List of products in html
<?php
for ($products = 1; $products <= $rowcount; $products++) {
$row = mysqli_fetch_array($query);
?>
<tr>
<td><?php echo $row["id"]?></td>
<td><?php echo $row["inventuraId"] ?></td>
<td style="display: none;"><?php echo $row["name"]?></td>
<td><?php echo $row["ean"]?></td>
<td style="display: none;"><?php echo $row["plu"]?></td>
<td style="display: none;"><?php echo $row["externalId"]?></td>
<td style="display: none;"><?php echo $row["productId"]?></td>
<td><?php echo $row["quantity"]?></td>
<td><?php echo $row["versiondate"]?></td>
</tr>
<?php
}
?>
</table>
<input type="submit" name="submit" value="Detail">
won't do much, as it's not inside a form.
You probably need a hyperlink which goes to a "products" page and passes the inventory ID as a query parameter, e.g.
Display
And then on the products page, you'd use $_GET["inventoryID"] to get the ID, and pass that into your SQL query (but please use a parameter, don't inject variables directly into the SQL, it's a security risk).

i want to calculate subtraction of row2 & row1

I display mysql table data using php.
I search but find solution for column but not for row.
Below I try to show what I want...
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> Difference from previous row. </td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
$i=0;
$oldval = 0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> <?php echo ($i==0) ? $oldval : $row['total']-$oldval; ?> </td>
</tr>
<?php
$oldval = $row["total"];
$i++;
}
?>
</table>
$oldval variable is used to store current row's total field data so when you go to the next row, you can get the difference for current row because you have previous row's data stored in the $oldval
And the ternary condition I put there is because if $i==0 means the first row so you don't have any data of the previous row so by default difference is 0 and you can notice that I stored the current record's total field data after printing the difference

How to create dynamic links for the dynamic posts in MySQL and PHP

I'll try to explain the problem straight away. I have one HTML form which takes input just like a comment form and it saves the xyz data into a MySQL database using PHP. Now, what I want is to create and display links for those comments on a page.
I mean the comments which have been saved including the user's email and name, should be opened by clicking a link.
I don't want to display all the details on a single page from the database for all the users. There should be a page on which links are shown, when a user click a link, the full post should be displayed in next page.
There is not something which I know about this process. Please help me out.
// $rows = set of result from your database query
foreach($rows as $row){
echo '<a'
. ' href="my_link_to_display_comment?id='.$row['id'].'">'
. 'Comment from '.$row['user_name']
. '</a>';
}
First a page to display all the links like the below example -
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"".$row['event_name'].""
;}
and then in event.php(the next page after clicking link)
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];} ?>
<?php echo $title ?>
<?php echo $content ?>
If you want to show details of a single user just do this.
You can make a search box by using a form.
eg. like if I want to display a details of a student, I will search him by using his roll number and run these queries.
<?php //to search student
require_once './secure.inc.php';
$status = 0;
if(isset($_POST['submit'])){
$roll_number = $_POST['roll_number'];
$query = "select * from students where roll_number=$role_number";
require_once '../includes/db.inc.php';
$result = mysql_query($query);
if(mysql_num_rows($result)==1){
$status = 1;
$row = mysql_fetch_assoc($result); //mysql_fetch_array - both numeric and key index
}else{
$status=2;
}
}
?>
//to display
<?php } else if($status==1) { ?>
<table>
<tbody>
<tr>
<td>Roll Number : </td>
<td><?php echo $row['roll_number']; ?></td>
</tr>
<tr>
<td>Name : </td>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<td>Gender : </td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Email : </td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Mobile Number : </td>
<td><?php echo $row['mobile_number']; ?></td>
</tr>
<tr>
<td>Course : </td>
<td><?php echo $row['course']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>

PHP retrieving from SQL database?

I'm really stuck on this question, I have given it my all and I still cant work out whats wrong, even my friends have attempted to help me, I'm beginning to think its unsolvable... Here's what is wants
Create a PHP script, named task3a.php, that retrieves the names and id of each driver, and outputs the
information differently to the previous two tasks. Instead of displaying a table, the output should contain
an HTML form. The form should contain a submit button and a drop-down list input. The drop-down
input should contain the driver names, and the form should submit via the GET method to task4.php
when the submit button is pressed. Name the select input driver.
And here's what I've got -
<!DOCTYPE HTMl>
<html>
<body>
<?php
try {
$dbhandle = new PDO('mysql:host=<...>.ac.uk;dbname=user','user','pass');
} catch (PDOExeption $e) {
die('Error Connecting to Database: ' . $e->getMessage());
}
$driver = 'SELECT forename, surname, d.nationality, name FROM Drivers d JOIN Teams t ON d.id = t.id';
$query = $dbhandle->prepare($driver);
if ($query->execute() === FALSE ) {
die('Error Running Query: ' . implode($query->errorInfo(), ' '));
}
$query->execute();
$result = $query->fetchAll();
?>
<table>
<tr>
<th>Forename</th>
<th>Surname</th>
<th>Nationality</th>
<th>Team</th>
</tr>
<?php foreach($result as $row) { ?>
<tr>
<td><?php echo $row['forename']; ?></td>
<td><?php echo $row['surname']; ?></td>
<td><?php echo $row['nationality']; ?><td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php } ?>
</table>
<form action='task3a.php' method='GET'>
<select name=''driver>
<?php foreach($results as $row) { ?>
<option value='<?php echo $row['id'];?>' > <?php echo $row['name']; ?> </option>
<?php } ?>
</select>
</form>
</body>
</html>
It's giving me a table with everything in it at the moment and then under it a drop down box with nothing in it, I'm so confused
$result = $query->fetchAll();
^--- no S
<?php foreach($result as $row) { ?>
^---no S
<?php foreach($results as $row) { ?>
^----where did this S come from?
Consider changing this
<select name=''driver>
to
<select name='driver'>

unique id for dynamic table

I will be generating a HTML table with data pulled from MySQL.The number of rows in my MySQL table are not fixed.
<?php
while($row=mysql_fetch_assoc($result))
{ ?>
<tr>
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php } ?>
Now how do I have the table rows and table data elements assigned unique id ??
Another loop to generate them won't work as I can't set an exit condition for the new loop as number of rows are not fixed.
Please guide me as to how to go forward about it. I can only use Javascript and not JQUERY.
Why can't you do something like this ?
<?php
$i = 1;
while($row=mysql_fetch_assoc($result))
{ ?>
<tr id="row<?php echo $i;?>">
<td id="cell-left-<?php echo $i;?>"><?php echo $row['col1'];?></td>
<td id="cell-right-<?php echo $i;?>"><?php echo $row['col2'];?></td>
</tr>
<?php
$i++;
} ?>
Please note, I have added ids row, cell-left- and cell-right- by myself. You may change them as per your requirements.
You can use a counter when iterating through the rows, maybe something like this:
<?php
$rowCount = 0;
while($row=mysql_fetch_assoc($result))
{
$rowCount++;
?>
<tr id="<?php echo 'row' . $rowCount;?>">
<td><?php echo $row['col1'];?></td>
<td><?php echo $row['col2'];?></td>
</tr>
<?php
}
?>
You can now select an element with
var rowID = 1;
document.getElementById("row" + rowID);
Hope this helps.

Categories