As shown in the code below, I have a dropdown list, populated from table "tabela". Below it I have a table which I need it to be populated depending on the option select from the dropdown?
How can I do this?
<div class="box">
<?php
$pdo = new PDO('mysql:host=localhost;dbname=dbname; charset=utf8', 'user', 'pass');
$sql = "SELECT id, pref, nome FROM tabela GROUP BY pref,nome ORDER BY nome";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll();
?>
<select>
<?php foreach($users as $user): ?>
<option value="<?= $user['id']; ?>"><?= $user['pref']; ?> - <?= $user['nome']; ?></option>
<?php endforeach; ?>
</select>
<table class="gradienttable">
<thead>
<tr>
<th colspan="7" class="tabelas">tipo1</th>
</tr>
<tr>
<th>Tipo1</th>
<th>Modelo</th>
<th>Qtde</th>
<th>Tipos</th>
<th>Pessoas</th>
<th>Vazios</th>
<th>Porcent</th>
</tr>
</thead>
<tbody>
<?php
foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM tabela WHERE prefixo=8510") as $row) {
printf(
"<tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>
<td style='padding:8px;'>%s - %s</td>
<td style='padding:8px;'>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>",
$row->pref, $row->nome, $row->model, $row->qtde, $row->tipos, $row->pessoas, $row->vazios, $ocupacao1 = round($row ->ocupacao1 * 100)/100 . '%');
}
?>
</tbody>
</table>
</div>
I think first of all you should change your foreach() statement into:
<?php foreach($users as $user) { ?>
<option value="<? echo $user['id']; ?>"><? echo $user['pref']; ?> - <?echo $user['nome']; ?></option>
<?php }; ?>
I find this a bit more clean and readable :)
Then you will need to enable the page to "filter" the rows by select - this can be done either by posting the select contents each time you change the select value (so basically you create a form that will submit itself every time you change what is in select - not very modern solution) or use AJAX to send the contents and retrieve the results.
If you decide to go second option (which I would highly recommend) you should take a look at some tutorials on changing page content basing on AJAX response. I would recommend to go to jQuery for that - since you should find quite some functions there that would help you out...
Using the way you are gathering the data you need to echo the results into the html.
<?php foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM infografico WHERE prefixo=8510") as $row) { ?>
<tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>
<td><?php echo $row->pref; ?></td>
...
</tr>
<?php } ?>
It would most likely be better to gather the data you need in the table first and form your array as required, then loop through that array and echo into the table.
Related
I am looking for a tutorial on pagination that only associates with the dependent drop list. In other words, If I made a selection from a dependent drop list with items, {Beavers, Cubs, Scouts, Venturers, Rovers and Leaders}. It will display the associated records and I was able to do that with ajax. However, some of those items have more than 100 records, and I would like to add pagination based on what was selected and can only find the tutorial that has drop list based of total number of record, example {50, 100, 250, 500, 5000 etc}
I have been searching using,
pagination based on the result from a dependent drop list ,
dependent drop list with pagination,
dynamic pagination with a drop list etc
with PHP, MySQL, js and ajax with no success. I am fairly new with this and a tutorial will help me understand better, Can anyone tell me what to look for? thanks
code for AdminPanelFrontEnd.php
<?php
require_once('includes/header.php');
require_once('includes/connection.php');
if(isset($_SESSION['admin']))
{
$query = "SELECT * FROM members";
$result = mysqli_query($con, $query);
}
else
{
header("location:AdminPanelFrontEnd.php");
}
?>
<script type = "text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js"></script>
<script>
function load_data(SectionID)
{
$.ajax({
type: "POST",
url: "AdminPanelBackEnd.php",
data: 'SectionID=' + SectionID,
success: function(data){
$("#details").html(data);
}
});
}
</script>
<div class="card-body">
<table class="table table-striped bg-info text-white" id="details" >
<tr>
Add Member
<div class="form-inline float-left ml-1">
<select class="form-control mb-2" id="section" onChange="load_data(this.value);">
<option value="null">Select Section</option>
<?php
$query = "SELECT * FROM sections ";
$result = mysqli_query($con, $query);
while($row=mysqli_fetch_assoc($result))
{
?>
<option value = "<?php echo $row["SectionID"]; ?>"> <?php echo $row["SectionName"]; ?> </option>
<?php
}
?>
</select>
</div>
</tr>
<tr class="bg-info text-white">
<td>Member Name</td>
<td>Member Email</td>
<td>Member Contact</td>
</tr>
</table>
</div>
code for AdminPanelBackEnd.php
<?php
require_once('includes/connection.php');
$query = "SELECT * FROM sections, members WHERE
sections.SectionID ='".$_POST["SectionID"]."' AND
sections.SectionID = members.SectionID ORDER BY MemberName
";
$result = mysqli_query($con, $query);
?>
<table class="text-center bg-info text-white">
<tr>
<td>Member Name</td>
<td>Member Email</td>
<td>Member Contact</td>
</tr>
<?php
while($row=mysqli_fetch_assoc($result))
{
$Member = $row['MemberName'];
$MemberEmail= $row['Email'];
$MemberContact = $row['Contact'];
?>
<tr class="bg-light text-dark">
<td><?php echo $Member ?></td>
<td><?php echo $MemberEmail ?></td>
<td><?php echo $MemberContact ?></td>
</tr>
<?php
}
?>
</table>
As you said, you already did the ajax, so the query string should be included in the paging navigation.
The link for next page should look like ?page=2&category=some_id
The ajax call will replace the whole content and the navigation.
There are libraries supporting that on every framework.
Let's say for example column dropdown represents a type of membership and depending on the value of dropdown, I would like to be able to display a column next to it that indicates the maximum number of companions I could bring. Is there a way to do it via javascript or PHP or maybe SQL itself?
<?php
foreach ($dbh->query($sql) as $rows){
?>
<tr>
<td><?php echo $rows['name']?></td>
<td><?php echo $rows['email']?></td>
<td><?php echo $rows['number']?></td>
<td><?php echo $rows['org']?></td>
<td><?php echo $rows['dropdown']?></td>
<td><?php echo $rows['date']?></td>
</tr>
<?php
}
?>
Code:
<?php
foreach ($dbh->query($sql) as $rows){
?>
<tr>
<td id="<?=$rows['id']?>"><?php echo $rows['name']?></td>
</tr>
<?php
}
?>
By clicking on the ajax button you will take the ID attribute of the column, send a request with the received id and add the value to the column created next to it.
If I understand you correctly.
I have a page containing a database table with all the rows and columns.
What I am trying to do is to select all the rows I want and then delete them when I click on the button.
This is what I've done so far in the table.php page:
<?php
include "config.php"; //connection to database
incude "home.js";
$funcao="Select * from palavras";
$result=mysqli_query($link, $funcao);
?>
<button id="button_apaga" type="button" onclick="delete()" > DELETE </button>
<?php if($result->num_rows > 0) { ?>
<table class="table">
<tr>
<th>IdPalavra</th>
<th>Palavra</th>
<th>Grau de Dificuldade</th>
<th>Data</th>
<th>Hora</th>
<th>Selecionar</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr role="row">
<td><?php echo $row['idpalavras']; ?></td>
<td><?php echo $row['palavra']; ?></td>
<td><?php echo $row['graudificuldade']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['hora']; ?></td>
<td><input type="checkbox" name="check" id="checkbox" /></td>
</tr>
<?php } ?>
</table>
<?php }
else{
echo "0 resultados";
} ?>
JavaScript Page (home.js):
function delete(id){
var check = document.getElementById('checkbox');
if(check.checked) {
// sql query
}
My question is how can I do que sql query considering it's in a different page. Can I just open php and put the query inside?
Also how can I receive all the IDs from the selected rows to the function?
Thank you in advance.
One approach would be to use AJAX. For the purpose of condensing the code, I'm going to also incorporate jQuery into this solution. I am also going to change your checkbox to an individual button link for the sake of making this a bit less code. A solution to delete multiple at the same time could work similarly to this, but since you're using AJAX most likely this is going to be easier for your users.
Modify table.php
<?php
include "config.php"; //connection to database
incude "home.js";
$funcao="Select * from palavras";
$result=mysqli_query($link, $funcao);
?>
<?php if($result->num_rows > 0) { ?>
<table class="table">
<tr>
<th>IdPalavra</th>
<th>Palavra</th>
<th>Grau de Dificuldade</th>
<th>Data</th>
<th>Hora</th>
<th>Selecionar</th>
</tr>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr role="row" class="palavras_row">
<td><?php echo $row['idpalavras']; ?></td>
<td><?php echo $row['palavra']; ?></td>
<td><?php echo $row['graudificuldade']; ?></td>
<td><?php echo $row['data']; ?></td>
<td><?php echo $row['hora']; ?></td>
<td>Delete</td>
</tr>
<?php } ?>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js?ver=3.3.1"></script>
<script type="text/javascript">
(function($) {
$(document).on('click', '.palavras_row a.palavras_delete', function() {
var _id = $(this).attr('data-id');
var _row = $(this).parent().parent();
$.ajax({
url: 'delete.php',
data: {
idpalavras: _id
},
type: 'POST',
dataType: 'json',
success: function(__resp) {
if (__resp.success) {
_row.remove(); // Deletes the row from the table
}
}
});
});
})(jQuery);
</script>
Create a new file in the same folder as your table.php and name it delete.php
<?php
$idpalavras = filter_input(INPUT_POST, 'idpalavras', FILTER_SANITIZE_NUMBER_INT);
$success = false;
if ($idpalavras) {
include "config.php"; //connection to database
$funcao="delete from palavras where idpalavras = " . $idpalavras;
$result=mysqli_query($link, $funcao);
$success = true;
}
header('Content-Type: application/json');
echo json_encode(array('success' => $success));
The solution above sends a simple command to your PHP backend where the delete query can be run by PHP. You cannot run a mysql command directly from javascript since that is frontend code.
This code is a functional solution, but it is abbreviated; a more complete solution would have more detailed handling of potential errors (either via AJAX or processing your delete query). It should also have some security on your delete.php to make sure unauthorized users aren't able to delete records without the proper permission to do so.
i want to create some simple system for my production which is they can scan bar-code on product and the system can view the JIT that they can use for paste the label on that product. Some of product must use 2 JIT for paste the label and some of JIT can be used on many product. I have create 3 table in mysql which is 'product','jit' and 'production_jit' with many to many relation.
i was created the inner join to pull the data from MySQL by using product number.
$code = $_POST['code'];
$sql = "SELECT product.product_number,product.product_name,product.product_jitqty,product.product_desc,jit.jit_number,jit.jit_name,jit.jit_drawer,jit.jit_port,jit.jit_specpath
FROM product
JOIN production_jit
ON production_jit.product_number = product.product_number
JOIN jit
ON jit.jit_number = production_jit.jit_number
WHERE product.product_number = '$code'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result))
{ ?>
<table>
<tr><td>Product Number</td><td>: <?php echo $row['product_number']; ?></td></tr>
<tr><td>Product Name</td><td>: <?php echo $row['product_name']; ?></td></tr>
<tr><td>Product Description</td><td>: <?php echo $row['product_desc']; ?></td></tr>
<tr><td>Product Jit Quantity</td><td>: <?php echo $row['product_jitqty']; ?></td></tr>
<tr><td>JIT Number</td><td>: <?php echo $row['jit_number']; ?></td></tr>
<tr><td>JIT Name</td><td>: <?php echo $row['jit_name']; ?></td></tr>
<tr><td><font color="red">JIT Drawer</font></td><td><font color="red">: <?php echo $row['jit_drawer']; ?></font></td></tr>
<tr><td><font color="red">JIT Port</font></td><td><font color="red">: <?php echo $row['jit_port']; ?></font></td></tr>
<tr><td>JIT Spec</td><td>: <?php echo $row['jit_specpath']; ?></td></tr>
</table>
<h1><strong>Please scan JIT barcode on JIT</strong></h1>
<table>
<form action="product3.php" method="post" name="check2">
<input type="hidden" name="id" value="<?php echo $row['ID']; ?>">
<input type="hidden" name="productnumber" value="<?php echo $row['productNumber']; ?>">
<input type="hidden" name="jitnumber" value="<?php echo $row['jitNumber2']; ?>">
<tr><td>JIT Barcode</td><td>:</td><td><input type="text" name="jitcode" maxlength="200"/></td></tr>
</table>
<table>
<td></td><td colspan="3" align="right"><input name="hantar" type="submit" value="Check"></td>
</table>
<?php
}
}
else {
?>
<script>
alert("Invalid Barcode");
window.location.href = "product.php";
</script><?php
}
mysqli_close($conn);
If the product used only one JIT, the interface look good,but if the Product used 2 JIT, the system show the looping data.
In logic situation, the view is correct, but how can i customize, the repeated data will not show by the system. for this example, only jit table can show in the looping. as the picture below, one product have 2 JIT, so i just to show about product information once, but only JIT Infomation i want to show follow the data..
IF you're saying you only want one of the datasets to show up, just use break so only the first dataset will show up:
<?php
$phead='';
$pbody='';
while($row = mysqli_fetch_assoc($result))
{
if(empty($phead)){
$phead.='<table>';
$phead.='<tr><td>Product Number</td><td>: '.$row['product_number'].'</td></tr>';
$phead.='<tr><td>Product Name</td><td>: '.$row['product_name'].'</td></tr>';
$phead.='<tr><td>Product Description</td><td>: '.$row['product_desc'].'</td></tr>';
$phead.='<tr><td>Product Jit Quantity</td><td>: '.$row['product_jitqty'].'</td></tr>';
}
$pbody.='<tr><td>JIT Number</td><td>: '.$row['jit_number'].'</td></tr>';
$pbody.='<tr><td>JIT Name</td><td>: '.$row['jit_name'].'</td></tr>';
$pbody.='<tr><td><font color="red">JIT Drawer</font></td><td><font color="red">: '.$row['jit_drawer'].'</font></td></tr>';
$pbody.='<tr><td><font color="red">JIT Port</font></td><td><font color="red">: '.$row['jit_port'].'</font></td></tr>';
$pbody.='<tr><td>JIT Spec</td><td>: '.$row['jit_specpath'].'</td></tr>';
}
//Now, we only have 1 instance of $phead and can have multiple $pbody;
if(!empty($phead)){
echo $phead;
echo $pbody;
echo '</table>';
}
?>
I've updated my answer to match your conditions. 1 set of product header information and multiple JIT info.
I have the following code for a dropbox;
<select name="Symptom" id="Symptomid" onchange="LSC()">
On change in the dropbox i want to run the following php code;
<?php
$data = array();
{
$symptoms_name = $_POST['Symptom'];
$sql1 = mysql_query("SELECT name,description , comments
FROM symptoms WHERE symptoms.name ='$symptoms_name'") or
die(mysql_error());
while($row1 = mysql_fetch_assoc($sql1)){
$data[] = $row1;
}
}
?>
<?php
if($data){
?>
<table border="1">
<tr>
<td>name</td>
<td>description</td>
<td>comments</td>
</tr>
<?php
if($data){
foreach($data as $sy){
?>
<tr>
<td><?php echo $sy['name']; ?> </td>
<td><?php echo $sy['description']; ?> </td>
<td><?php echo $sy['comments']; ?> </td>
</tr>
<?php
}
}
?>
<?php
}
?>
</table>
}
and display this table under the dropbox on the original html .
I think we have to run a java script which will call the php function and retun the table to the original html.
Kindly assist, I know we have to use ajax.
Regards
Azhar
sorted,
see this has a similar problem
http://www.w3schools.com/php/php_ajax_database.asp
Regards
Azhar