How to view data from inner join with friendly view in php - javascript

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.

Related

Foreach loop echoing only one row from database

This is what my code does: when a username is typed in the #type input field, and if that typed value matches a row value from my database table users, then my jquery code will come to action. My jquery code will then reveal the hidden div that contains text of that typed in username. My problem is my current code ignores other typed in usernames, and for some reason will reveal only one username. Example of my issue:
allen <-- "allen" is typed in input field
--------
[allen] <-- hidden div for allen now shows
pete
-------- <-- "pete" is typed in input field
<-- but hidden div for pete does not show. Why?
Is this an event bubbling issue with my js code? Because I did add e.propagation but it didn't do anything. How would I rewrite my current code so that any username that is typed will reveal a hidden div for it. Because currently I'm only able to get a hidden div for "allen" but not for the rest of the usernames. Please help, here is my code:
<input id="type">
<?php foreach (array_combine($userids, $usernames) as $userid => $username): ?>
<div id="border<?php echo $userid; ?>" style="display: none;">
<input id="username<?php echo $userid; ?>" value="<?php echo $username; ?>" type="radio">
<label for="username<?php echo $userid; ?>"><?php echo $username; ?></label>
</div>
<?php endforeach; ?>
$("#type").on('input',function(){
var userid = '<?php echo $userid; ?>';
if (this.value == $("#username"+userid).attr('value')) {
$("#border"+userid).css("display", "block");
}
else {
$("#border"+userid).css("display", "none");
}
});
Sql code:
$stmt = $conn->prepare("SELECT userid, username FROM usern");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$userids[] = $row['userid'];
$usernames[] = $row['username'];
}
$stmt->close();
#MohammadBagheri - Output for code below:
$stmt = $conn->prepare("SELECT userid, username FROM usern");
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
echo '<pre>'.print_r($row, 1).'</pre>';
}
$stmt->close();
Output:
Array
(
[userid] => 35
[username] => pete
)
Array
(
[userid] => 44
[username] => allen
)
I know what the problem is.
<input id="type">
<?php foreach (array_combine($userids, $usernames) as $userid => $username): ?>
<div id="border<?php echo $userid; ?>" style="display: none;">
<input id="username<?php echo $userid; ?>" value="<?php echo $username; ?>" type="radio">
<label for="username<?php echo $userid; ?>"><?php echo $username; ?></label>
</div>
<?php endforeach; ?>
$("#type").on('input',function(){
var username = $(this).val();
var userid = $("input[value='"+username+"']").attr("id");
$("div[id^=border]").css("display", "none");
$("#border"+userid).css("display", "block");
});
You have been selecting the userid of the last occurrence of the loop and set that in jquery code which means it will always show the user with that user id and not checking what you input.
Please let me know if you need more help.

Populating table with select dropdown (from database)

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.

onChange function change the displayed data from a table

I have a table in my website and a dropdown list in my website which according to the selected value from the dropdown list it will change the output of the table.
I did a search and I found this on stackoverflow and I tried to do something similar but it doesn't work. Here is my php file called announcements which I want to display the table.
<?php
include_once 'header.php';
$connection = mysql_connect("localhost", "root", "smogi")?>
<html>
<head>
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>
<?php
$result = queryMysql("SELECT * FROM doctor WHERE username='$username'");
if (mysql_num_rows($result)):
?>
<!-- Koumpi pou se metaferei sti selida gia tin dimiourgia neas anakoinwseis -->
<form action="new_announcement.php">
<input type="submit" value="Create New Announcement">
</form>
<br />
Select Category :<select id="SelectDisease" name="category">
<option value="*">All</option>
<!--emfanizei tis epiloges gia ta specialties me basi auta p exoume sti basi mas -->
<?php
$sql = mysql_query("SELECT name FROM disease");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select><br><br />
<table border="1" style="width:100%">
<tr>
<td><b>Author</b></td>
<td><b>Category</b></td>
<td><b>Subject</b></td>
<td><b>Content</b></td>
</tr>
<?php
if(isset($_GET["selected"])){
$type = $_GET["selected"];
$query = "SELECT author,category,subject,content FROM announcements WHERE category='" . $type . "'";
$announcements = mysql_query($query, $connection);
$counter = 0;
$z = 0;
if ($announcements == FALSE) {
die(mysql_error()); // To get better errors report
}
while ($row = mysql_fetch_assoc($announcements)) {
while ($row = mysql_fetch_assoc($announcements)) {
$counter++;
?>
<tr>
<td><?php echo $row['author'];?></td>
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['content']; ?></td>
</tr>
<?php }
}
}
?>
<?php
else:
?>
Select Category :<select id="SelectDisease" name="category">
<option value="*">All</option>
<!--emfanizei tis epiloges gia ta specialties me basi auta p exoume sti basi mas -->
<?php
$sql = mysql_query("SELECT name FROM disease");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select><br><br />
<table border="1" style="width:100%">
<tr>
<td><b>Author</b></td>
<td><b>Category</b></td>
<td><b>Subject</b></td>
<td><b>Content</b></td>
</tr>
<?php
if(isset($_GET["selected"])){
$type = $_GET["selected"];
$query = "SELECT author,category,subject,content FROM announcements WHERE category='" . $type . "'";
$announcements = mysql_query($query, $connection);
$counter = 0;
$z = 0;
while ($row = mysql_fetch_assoc($announcements)) {
$counter++;
?>
<tr>
<td><?php echo $row['author'];?></td>
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['content']; ?></td>
</tr>
<?php } } endif;?>
</table>
</body>
</html>
And this is my javascript.js file
$(document).ready(function() {
$('#SelectDisease').change(function() {
var selected=$(this).val();
$.get("announcements.php?selected="+selected, function(data){
$('.result').html(data);
});
});
});
Thank you for your time :)
PS: THE INCLUDE_ONCE CODE create the connection with th db
First thing is that unless your user makes a selection, you do not have any $_GET available so it will be undefined. You should get the value if it is available. Like so:
if(isset($_GET['selected'])){
$type = $_GET['selected'];
}
But this is not the only issue here. $.get is an ajax request and it is just sending a request to your php file and gets all the html content of your page.
If you do not care to reload the page each time your user selects an option, instead of an $.get request simply redirect your user to that url. Otherwise if you want to do it without reloading, you need to do an $.get request correctly.
The example that you said you are trying to do something similar, is not sending an $.get request to the same page that the user is viewing. It is sending the get request to another php page which is just designed to get the $_GET["selected"] from its url, send a query to the database, get the result from the database and then just return the result, which will be the data in your $.get request.
See this example: Get data from mysql database using php and jquery ajax
It is trying to do the same thing as you, except with $.POST which you can do the same or change it to a $.GET if you want. See how the other php page return the result and how the $.get request is receiving and viewing the data.
mysql_query() will result in FALSE which is Boolean value if there is any error, do check before while loop, to get better error,
if($announcements == FALSE) {
die(mysql_error()); // To get better errors report
}
while($row = mysql_fetch_assoc($announcements)){
// then you code here
}
mysql_query() returns FALSE in case of error.
You have to choose the data base name.
Like so:
$connection = mysql_connect("localhost", "root", "smogi", "***DATABASE_NAME***");
Remember, mysql functions are deprecated!
Use mysqli or PDO.
Have a nice day

Dynamic php rows gives values of 1st row only to javascript function when each row has its own dynamic values

I have been toying with this for a while now and i can not get each row to send its specific values that are displayed to a javascript function only the 1st rows values are sent no matter which row is clicked?
I need to send the values for each specific row dependng on the results of the mysql result.
Below is the code that i have which only sends the values of the 1st row.
<?php
require 'core/init.php';
$records = array();
$result = ("(SELECT * FROM message ORDER BY id DESC LIMIT 25)ORDER BY id ASC");
$results = ($db->query($result));
if($results->num_rows){
while($row = $results->fetch_object()){
$records[] = $row;
}
$results->free();
}
if(!count($records)){
echo 'no records';
}else{
?>
<table>
<?php
foreach ($records as $r){
?>
<tr>
<td><div id="modOptions" onclick="modOptions()"><?php echo escape($r->sender); ?></div></td>
<td><?php echo escape($r->message); ?></td>
<input type="hidden" id="modOptionsIp" value="<?php echo escape($r->ip); ?>"/>
<input type="hidden" id="modOptionsSender" value="<?php echo escape($r->sender); ?>"/>
<input type="hidden" id="modOptionsMessage" value="<?php echo escape($r->message); ?>"/>
</tr>
<?php
}
?>
</table>
<?php
}
?>
It displays everything ok just doesnt give each row its specific values
Any pointers are much appreciated.
Change the js modOptions function so it can take parameters
function modOptions(ip, sender, message) {
//ip sender and message are from the row you clicked on
}
and render the onclick like this:
onclick="modOptions('<?php echo escape($r->ip); ?>', '<?php echo escape($r->sender); ?>', '<?php echo escape($r->message); ?>' );"
those hidden fields you have now are useless

Value not updating when form submitted(PHP, beginner level)

Why does this form not update the values that the items in my session? The session seems to keep track of the value fine, before the form tries to allow users to edit the value. Here's what I wrote out form submit:
<?php
if(isset($_POST['submit'])){
foreach($_POST['quantity'] as $key => $val) {
if($val==0) {
unset($_SESSION['Cart'][$key]);
}else{
$_SESSION['Cart'][$key]['quantity']=$val;
}
}
}
?>
And here's the form:
<?php
$sql="SELECT * FROM products where Product_ID IN (";
foreach($_SESSION['Cart'] as $id => $value){
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY Category ASC";
$query=mysql_query($sql);
$totalquantity=0;
while($row=mysql_fetch_array($query)){
$subtotal=$_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity'];
$totalquantity+=$subtotal;
?>
<tr>
<td><?php echo $row['Name'] ?></td>
<td><input = type="text" name="Quantity [<?php echo $row['Product_ID'] ?>]" size="5" value="<?php echo $_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity'] ?>"/> </td>
</tr>
<?php
}
?>
And of course, the submit button is just
<button type="Submit" name="Submit">Update selection</button>
It looks like it should all work out properly, but it doesn't update.
Submit should be "submit" most probably.Change it either in the input filed or in the $_POST["Submit"]

Categories

Resources