I have a HTML table and the data is from the database. I've then added a second table.
The table is shown by clicking the row. The row is expanded and shows the second table.
What is wrong with my code? It gives data from database but it doesn't loop. For example, I'm expecting 3 rows, but it only output one row.
The query is correct.
This is the code for the second table,
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Date filled</th>
<th>Date signed</th>
</tr>
</thead>
<tbody>
<tr>
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $row1['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td><?php echo $row['signatoryname'] ?></td>
<td><?php echo $row['datefilled'] ?></td>
<td><?php echo $row['datesigned'] ?></td>
</tr>
<?php
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</tr>
</tbody>
</table>
</td>
</tr>
Changes are commented :-
<tr>
<td colspan="5">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Date filled</th>
<th>Date signed</th>
</tr>
</thead>
<tbody>
<!-- remove <tr> -->
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $row1['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ ?>
<tr>
<td><?php echo $row['signatoryname']; ?></td><!-- ; missed -->
<td><?php echo $row['datefilled']; ?></td><!-- ; missed -->
<td><?php echo $row['datesigned']; ?></td><!-- ; missed -->
</tr>
<?php}} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}?>
<!-- remove </tr> -->
</tbody>
</table>
</td>
</tr>
Related
Good day, all!
I am trying to figure out how to get this chunk of code to do what I want but it eludes me.
What this script is supposed to do is to look through a database of consignment items, pick out the ones that have sold='n' and list them by item number. It displays all of the items in a list with a "Sell" link at the end of each item row. Once the Buyer Number and Amount are entered, the Sell link is clicked, the UPDATE updates the item and changes sold from 'n' to 'y' and once that happens, the item disappears from the list. It works exactly as I expect, except for one little issue that has eluded me. Each time the page is loaded, it doesn't change the amount and buyernumber, but it does change the sold from 'n' to 'y' for the first item only and after that it works as expected, adding the buyernumber and amount to all subsequent items. Until you browse away from it and then it won't do the first item correctly the next time you come back to the page. I have tried all kinds of things to make it work and this is the closest I have to what I want. Any help would be appreciated!
<?php
/**
* Sell an Item
*/
require "../config.php";
require "../common.php";
$success = null;
if (isset($_POST["submit"])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$itemnumber = $_POST["submit"];
$buyernumber = $_POST["buyernumber"];
$amount = $_POST["amount"];
$sql = "UPDATE consignitem SET sold='y', amount='$amount', buyernumber='$buyernumber' WHERE sold='n' AND itemnumber = $itemnumber";
$statement = $connection->prepare($sql);
$statement->bindValue(':itemnumber', $itemnumber);
$statement->bindValue(':buyernumber', $buyernuumber);
$statement->bindValue(':amount', $amount);
$statement->bindValue(':sold', $sold);
$statement->execute();
$success = "Item successfully updated";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM consignitem WHERE sold='n'";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
?>
<?php require "templates/header.php"; ?>
<h2>Mark Items as Sold</h2>
<?php if ($success) echo $success; ?>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<table>
<thead>
<tr>
<th>Sale Number</th>
<th>Item Number</th>
<th>Lot Number</th>
<th>Category</th>
<th>Item Description</th>
<th>Reserve</th>
<th>Seller Number</th>
<th>Amount</th>
<th>Buyer Number</th>
<th>Sold</th>
<th>Paid</th>
<th>Date</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["salenumber"]); ?></td>
<td><?php echo escape($row["itemnumber"]); ?></td>
<td><?php echo escape($row["lotnumber"]); ?></td>
<td><?php echo escape($row["category"]); ?></td>
<td><?php echo escape($row["itemdescription"]); ?></td>
<td><?php echo escape($row["reserve"]); ?></td>
<td><?php echo escape($row["sellernumber"]); ?></td>
<td><?php echo escape($row["amount"]); ?><br><input type="text" name="amount" id="amount" size="8"></td>
<td><?php echo escape($row["buyernumber"]); ?><br><input type="text" name="buyernumber" id="buyernumber" size="12"></td>
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
<td><button type="submit" name="submit" value="<?php echo escape($row["itemnumber"]); ?>">Sell</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
<br>
<?php require "templates/footer.php"; ?>
You're repeating the same input names in all thr rows of the form. When the form is submitted, the $_POST variables will just contain the value from the last row of the form, not the row that the user clicked the submit button for.
You can give the inputs array-style names that are indexed by itemnumber. $_POST['buyernumber'] and $_POST['amount'] will then be arrays, and you can use $_POST['submit'] to get the appropriate array index.
You also need to use placeholders in the SQL that match the placeholders in bindValue(), rather than substituting the variables directly in the query.
<?php
if (isset($_POST["submit"])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$itemnumber = $_POST["submit"];
$buyernumber = $_POST["buyernumber"][$itemnumber];
$amount = $_POST["amount"][$itemnumber];
$sql = "UPDATE consignitem SET sold='y', amount= :amount, buyernumber= :buyernumber WHERE sold='n' AND itemnumber = :itemnumber";
$statement = $connection->prepare($sql);
$statement->bindValue(':itemnumber', $itemnumber);
$statement->bindValue(':buyernumber', $buyernuumber);
$statement->bindValue(':amount', $amount);
$statement->execute();
$success = "Item successfully updated";
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT * FROM consignitem WHERE sold='n'";
$statement = $connection->prepare($sql);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
?>
<?php require "templates/header.php"; ?>
<h2>Mark Items as Sold</h2>
<?php if ($success) echo $success; ?>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<table>
<thead>
<tr>
<th>Sale Number</th>
<th>Item Number</th>
<th>Lot Number</th>
<th>Category</th>
<th>Item Description</th>
<th>Reserve</th>
<th>Seller Number</th>
<th>Amount</th>
<th>Buyer Number</th>
<th>Sold</th>
<th>Paid</th>
<th>Date</th>
<th>Mark</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["salenumber"]); ?></td>
<td><?php echo escape($row["itemnumber"]); ?></td>
<td><?php echo escape($row["lotnumber"]); ?></td>
<td><?php echo escape($row["category"]); ?></td>
<td><?php echo escape($row["itemdescription"]); ?></td>
<td><?php echo escape($row["reserve"]); ?></td>
<td><?php echo escape($row["sellernumber"]); ?></td>
<td><?php echo escape($row["amount"]); ?><br><input type="text" name="amount[<?php echo escape($row["itemnumber"]); ?>]" size="8"></td>
<td><?php echo escape($row["buyernumber"]); ?><br><input type="text" name="buyernumber[<?php echo escape($row["itemnumber"]); ?>]" size="12"></td>
<td><?php echo escape($row["sold"]); ?></td>
<td><?php echo escape($row["paid"]); ?></td>
<td><?php echo escape($row["date"]); ?> </td>
<td><button type="submit" name="submit" value="<?php echo escape($row["itemnumber"]); ?>">Sell</button></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</form>
<br>
<?php require "templates/footer.php"; ?>
i have problem with my table inside my table. it removes the last data in the second table.
the first table gives the correct output but the second table gives the wrong output. the second table can be shown by clicking the table row. the table row is expandable.
this is my code
<div class="form-group">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$stmt1 = $conn->prepare("select * from table where name=?");
$stmt1->bindParam(1, $_temppp1, PDO::PARAM_STR, 30);
$stmt1->execute();
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table id="report" class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><?php echo $row1['tracknum']; ?></td>
<td><?php echo $row1['doctitle']; ?></td>
<td><?php echo $row1['doctype']; ?></td>
<td><?php echo $row1['datefilled']; ?></td>
<td>
<div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
</td>
</tr>
<tr><td colspan="5">
<table class="table">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>johncarter#mail.com</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>peterparker#mail.com</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>johnrambo#mail.com</td>
</tr>
</tbody>
</table>
</td>
</tr>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
and this is the output
Your code is wrong i think
change this
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
to that
<?php endwhile; ?>
</td>
</tr>
</table>
</div>
and try again or loock in this region..
i have a 3 table and im doing inner join in the output below
how can i achive something like this
so far i already have code for expanding the row. the real problem here is how do i get all the signatoryname for each tracknum. im using php and html
this is my code
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5"><p><?php echo $r['signatoryname'] ?></p>
</td></tr>
<?php endwhile; ?>
</table>
for the table to expand
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function() {
$("td[colspan=5]").find("p").hide();
$("table").click(function(event) {
event.stopPropagation();
var $target = $(event.target);
if ( $target.closest("td").attr("colspan") > 1 ) {
$target.slideUp();
} else {
$target.closest("tr").next().find("p").slideToggle();
}
});
});
});//]]>
</script>
this is the output of the code
i need to group the data below by tracknum but i need the signatoryname
i want the html row to be expandable and list the signatoryname of that tracknum bellow it. thanks.
update: so far this is my code
UPDATE: below is the correct code:
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
// execute the stored procedure
$sql = 'CALL sp_trasactionsignatory()';
$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo $r['tracknum'] ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$_tempp1 = $r['tracknum'];
$stmt = $conn->prepare("CALL sp_gettransactsignatory(?)");
$stmt->bindParam(1, $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Problem:
How do I get all the signatoryname for each tracknum?
Solution:
Your very first initial query would be like this,
(Since you didn't provide the table name, change the table name from the below queries)
$q = $connection->query("SELECT tracknum, doctitle, doctype, datefilled FROM tablename GROUP BY tracknum");
Then comes to your table,
<table class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($r = $q->fetch_assoc()): ?>
<tr>
<td><?php echo $r['tracknum']; ?></td>
<td><?php echo $r['doctitle'] ?></td>
<td><?php echo $r['doctype'] ?></td>
<td><?php echo $r['datefilled'] ?></td>
<td>
<span class="btnshow glyphicon glyphicon-plus-sign"></span>
</td>
</tr>
<tr><td colspan="5">
<?php
$result_set = $connection->query("SELECT signatoryname FROM tablename WHERE tracknum = {$r['tracknum']}");
while ($row = $result_set->fetch_assoc()){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
</td></tr>
<?php endwhile; ?>
</table>
Don't forget to change the tablename in both the queries.
Edited:
If you using PDO extensions to execute your queries then you can do something like this,
<?php
$_tempp1 = $r['tracknum'];
$stmt = $connection->prepare("SELECT signatoryname FROM tablename WHERE tracknum = :tracknum");
$stmt->bindParam(':tracknum', $_tempp1, PDO::PARAM_STR, 30);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<p>" . $row['signatoryname'] . "</p>";
}
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Here's my code
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<td>Id</td>
<td>Articles Category</td>
<td>Update</td>
<td>Delete</td>
</tr>
</thead>
<tbody>
<tr>
<?php
$con = new mysqli("localhost", "root", "", "whatever");
$sql = "SELECT id_kategori, nama_kategori FROM kategori";
$stmt = $con->prepare($sql);
$stmt->execute();
$stmt->bind_result($id, $cat);
while($stmt->fetch())
{
echo "<td>$id</td>";
echo "<td>$cat</td>";
echo "<td>Update</td>";
echo "<td>Delete</td>";
}
$stmt->close();
?>
</tr>
</tbody>
</table>
</div>
The result is showing me this
"prepare($sql); $stmt->execute(); $stmt->bind_result($id, $cat); while($stmt->fetch()) { echo ""; echo ""; echo ""; echo ""; } $stmt->close(); ?> "
I change it to Object Oriented like this
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<td>Id</td>
<td>Articles Category</td>
<td>Update</td>
<td>Delete</td>
</tr>
</thead>
<tbody>
<tr>
<?php
include 'connection.php';
$showall = new connection();
$showall->category();
?>
</tr>
</tbody>
</table>
</div>
My result is just this
category(); ?>
Can someone explain what went wrong with my code ?
first of all you have used very bad coding standards.You just write db connection in the middle of HTML?. Okay You can write you HTML as
echo '<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<td>Id</td>
<td>Articles Category</td>
<td>Update</td>
<td>Delete</td>
</tr>
</thead>
<tbody>
<tr>';
<?php
$con = new mysqli("localhost", "root", "", "whatever");
$sql = "SELECT id_kategori, nama_kategori FROM kategori";
$stmt = $con->prepare($sql);
$stmt->execute();
$stmt->bind_result($id, $cat);
while($stmt->fetch())
{
echo "<td>$id</td>";
echo "<td>$cat</td>";
echo "<td>Update</td>";
echo "<td>Delete</td>";
}
$stmt->close();
?>
echo '</tr>
</tbody>
</table>
</div>';
I'm writing some php that renders a table from a database, this should be simple, but for some reason, it is rendering extra cells and all cells are empty. Here is my code:
<?php
$db= new PDO("mysql:host=localhost;dbname=mydb", "user", "password");
$query= $db->query("SELECT yarnId, yarnName, longYarnDescription, sale_price, cost, contents, onSale, yarnImage, activeFlag FROM yarn");
$result= $query->fetchAll();
?>
<table border="1">
<tr>
<th>yarnId</th>
<th>yarnName</th>
<th>description</th>
<th>sale price</th>
<th>cost</th>
<th>contents</th>
<th>onSale</th>
<th>yarnImage</th>
<th>activeFlag</th>
<th>edit</th>
</tr>
<?php for($r=0; $r<count($result); $r++){?>
<tr>
<?php for($c=0; $c<count($result[0]); $c++){?>
<td><?php echo $result[r][c];?></td>
<?php }?>
<td><button name=edit>edit</button></td>
</tr>
<?php }?>
</table>
If anyone can tell me why it's empty and why there are extra cells, it would be greatly appreciated.
The following code uses while()loop instead of for()
<table border="1">
<tr>
<th>yarnId</th>
<th>yarnName</th>
<th>description</th>
<th>sale price</th>
<th>cost</th>
<th>contents</th>
<th>onSale</th>
<th>yarnImage</th>
<th>activeFlag</th>
<th>edit</th>
</tr>
<?php
while($row = $query->fetch()) {
echo "<tr>";
for ($x=0;$x<= 8; $x++) {
echo "<td>" . $row[$x] . "</td>";
}
echo "<td><button name=\"edit\">edit</button></td></tr>\n";
}
?>
</table>