Pass value to next page on table row click? - php

I have a table that is not currently clickable. On row click, I would like to go to a detail page from this table, passing the id value in the process.
I understand I need to make an href on the table row and somehow pass the id value on click, but am not clear on how to do this. Relevant table and php:
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="table-responsive m-t-40">
<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<thead><tr><th title="Field #1">id</th>
<th title="Field #2">Organization</th>
<th title="Field #3">xxx</th>
<th title="Field #4">xxx</th>
<th title="Field #5">xxx</th>
<th title="Field #6">xxx</th>
</tr>
</thead>
<tbody>
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo '<script>console.log("Connection successful!")</script>';
}
$SELECT = mysqli_query($conn,"SELECT * FROM `organization`");
if($SELECT != false)
{
while($rows = mysqli_fetch_array($SELECT))
{
echo "
<tr>
<td>".$rows["id"]."</td>
<td>".$rows["name"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
</tr>
";
}
}else{
echo "
<tr>
<td colspan='3'>Something went wrong with the query</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
How do I make my table rows clickable (to go to the detail page) and how do I pass the id value from the table on click to the detail page?

you need to make href tag and pass the id like this
click
in your case it should be
<td>click</td>
your while loop should be like this
while($rows = mysqli_fetch_array($SELECT)){ ?>
<tr>
<td>click</td>
<td><?php echo $rows["name"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td>
<td><?php echo $rows["xxx"]; ?></td> <td>".$rows["xxx"]."</td>
</tr>
<?php }

<td>".$rows["xxx"]."</td>

<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body">
<div class="table-responsive m-t-40">
<table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr><th title="Field #1">id</th>
<th title="Field #2">Organization</th>
<th title="Field #3">xxx</th>
<th title="Field #4">xxx</th>
<th title="Field #5">xxx</th>
<th title="Field #6">xxx</th>
</tr>
</thead>
<tbody>
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo '<script>console.log("Connection successful!")</script>';
}
$SELECT = mysqli_query($conn,"SELECT * FROM `organization`");
if($SELECT != false)
{
while($rows = mysqli_fetch_array($SELECT))
{
echo "
<tr onclick="window.location='detail.php?id=$rows["id"]';">
<td>".$rows["id"]."</td>
<td>".$rows["name"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
</tr>
";
}
}else{
echo "
<tr>
<td colspan='3'>Something went wrong with the query</td>
</tr>
";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
You can set onclick() function to your tag using plain javascript.. try this.

You need to change your while loop as below: add window.location on tr click
<?php
while($rows = mysqli_fetch_array($SELECT))
{
echo "
<tr onclick=\"window.location='detail.php?id=".$rows["id"]."'\">
<td>".$rows["id"]."</td>
<td>".$rows["name"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
<td>".$rows["xxx"]."</td>
</tr>
";
}?>

Related

Need the Specific Row to be stored into Database after Approve Button Click

So regarding my dilemma, I have already coded my website to return a list of rescheduled classes into a table for the admin to approve or disapprove of it. This list of rescheduled classes is retrieved from the database.
What I want to find out is, when the Approve button is clicked, the code will store the specific row's data into the database (let's say I want just the first row's data to be approved). How do I do that? I want to do this if possible with its primary key but from what I understand, the primary keys aren't stable as for example, if an entry from that list is deleted, the key will not get replaced but rather the auto increment continues.
Here's my code for the table:
<div class="dashboard-content__panel " data-panel-id="class_replacement">
<div class="dashboard-list report-content">
<div id="report" class="panel panel-primary panel-table table-responsive reportTable" style="border:none; ">
<div class="panel-body " >
<table id="report-table" class="table table-hover" width="100%" >
<thead>
<tr>
<th>Subject Code</th>
<th>Subject Rescheduling Date and Subject Start Time</th>
<th>Duration (hours)</th>
<th>Status</th>
<th>Select Venue</th>
<th>Approve/Disapprove</th>
</tr>
</thead>
<tbody>
<?php
$conn = mysqli_connect("localhost", "root", "", "fyp1web");
if ($conn -> connect_error) {
die("Connection Failed: ". $conn-> connect_error);
}
$sql = "SELECT * from class_rescheduling";
$result = $conn-> query($sql);
while ($rs = $result-> fetch_assoc()) {
?>
<tr>
<td><?php echo $rs["newSubjectID"]; ?></td>
<td><?php echo $rs["newDateTime"]; ?></td>
<td><?php echo $rs["newDuration"]; ?></td>
<td><?php echo $rs["newStatus"]; ?></td>
<td> <select name="classname">
<?php
$sql2 = "SELECT className FROM class_rooms";
$result2 = $conn-> query($sql2);
while ($rs1 = mysqli_fetch_array($result2)) {
?>
<option value="<?php echo $rs1['className']; ?>"><?php echo $rs1["className"]; ?></option>
<?php
}
?>
</select></td>
<td><button class='btn' type='submit' name='approve'>Approve</button><button class='btn' type='submit' name='disapprove'>Disapprove</button></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
</div>
</div>

Problems getting the record to delete with PHP and access DB

I have a table with a delete action, but when I go to click it and get prompted to delete the item. It gives me an error. This is connecting to Access database.
Here is the code for the table
<table class="table-bordered ">
<tr>
<th width="91"> <div align="center">ID </div></th>
<th width="91"> <div align="center">VendorID </div></th>
<th width="98"> <div align="center">ItemNumber </div></th>
<th width="98"> <div align="center">ItemDescription </div></th>
<th width="98"> <div align="center">UnitDisplay </div></th>
<th width="98"> <div align="center">Cost </div></th>
<th>Action</th>
</tr>
<?php
for($i=$Page_Start;$i<=$Page_End;$i++)
{
$objResult = odbc_fetch_array($objExec,$i);
?>
<tr>
<?php $id = $objResult['ItemID'];?>
<?php echo "<td> <a href='update.php?ItemID=$id'>$id</a></td>"?>
<td><div align="center"><?php echo $objResult["VendorID"];?></div></td>
<td><?php echo $objResult["ItemNumber"];?></td>
<td><?php echo $objResult["ItemDescription"];?></td>
<td><?php echo $objResult["UnitDisplay"];?></td>
<td><?php echo $objResult["Cost"];?></td>
<td align="center">Delete</td>
</tr>
<?php
}
?>
</table>
Here is the code for the deleteRecord.php
<?php
$objConnect = odbc_connect("SupplyRequest","","") or die("Error Connect to Database");
$strSQL = "DELETE FROM tbl_Items ";
$strSQL .="WHERE ItemID = '".$_GET["ItemID"]."' ";
$objExec = odbc_exec($objConnect, $strSQL);
if($objExec)
{
echo "Record Deleted.";
}
else
{
echo "Error Delete [".$strSQL."]";
}
odbc_close($objConnect);
?>
I figured it out did it this way.
<?php
$objConnect = odbc_connect("SupplyRequest","","") or die("Error Connect to Database");
$ItemID=$_GET['ItemID'];
$strSQL="DELETE FROM tbl_Items WHERE ItemID= $ItemID";
$objExec = odbc_exec($objConnect, $strSQL);
if($objExec)
{
echo "Record Deleted.";
}
else
{
echo "Error Delete [".$strSQL."]";
}
odbc_close($objConnect);
?>

Warning: mysql_fetch_row() expects parameter 1

I'm a new learner in PHP. I don't know what's my error with my code. Please help me fix this. :( Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in C:\xampp\htdocs\project\employees.php on line 35.
connection.php
<?php
//print_r($_POST);
// DB Credentials
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "project101";
// Create connection
$con = mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if ( $con->connect_error ) {
die("Connection failed: " . mysqli_connect_error());
}
?>
Employee.php
include ('connection.php');
if(empty($_SESSION)) // if the session not yet started
session_start();
if(!isset($_SESSION['username'])) { //if not yet logged in
header("Location: index.php");// send to login page
exit;
}
?>
<html>
<head>
<title>Employee</title>
</head>
<body>
<center>
<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5">add data here.</th>
</tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Action</th>
</tr>
<?php
$result2 = $con->query("Select * from `employee`");
while($row=mysql_fetch_row($result2))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><img src="b_edit.png" align="EDIT" /></td>
<td align="center"><img src="b_drop.png" align="DELETE" /></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</center>
</body>
</html>
As apparent by your commented additional code, you instanciate a mysqli_ object, but try to use a mysql_ function on it.
those two don't work together. use mysqli_ functions with it instead.
Use the following code
include ('connection.php');
if(empty($_SESSION)) // if the session not yet started
session_start();
if(!isset($_SESSION['username'])) { //if not yet logged in
header("Location: index.php");// send to login page
exit;
}
?>
<html>
<head>
<title>Employee</title>
</head>
<body>
<center>
<div id="body">
<div id="content">
<table align="center">
<tr>
<th colspan="5">add data here.</th>
</tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Action</th>
</tr>
<?php
$result2 = $con->query("Select * from `employee`");
while($row=mysqli_fetch_row($result2))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><img src="b_edit.png" align="EDIT" /></td>
<td align="center"><img src="b_drop.png" align="DELETE" /></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</center>
</body>
</html>

getting data from MYSQL database to a html tabel using php

I design a table with html (bootstrap style) now i want to get data from my database table and fetch it in table using php, here is my html code:
Thanks for you help. :)
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
<th>Province</th>
<th>User</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
PHP code i tried but i didnt get any result it gives me errors:
<?php
$username = "root";
$password = "=";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("user", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table_one ");
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
echo "<td>" . $row['Category'] . "</td>";
<td>{$row\['regdate'\]}</td>
<td>{$row\['occdate'\]}</td>
</tr>\n";
}
?>
Since mysql_* is depreciated I will answer this using mysqli_*.
You have already closed the table in your example before looping through the fetched data so I am unsure where you want these rows to populate however I made a guess.
Another error with your attempt is you have echo " ... echo "";
<?php
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
$query = 'SELECT * FROM table_one';
$data = mysqli_query($mysqli, $query);
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($data))
{
echo " <tr>
<td>" . $row['Category'] . "</td>
<td>" . $row['regdate'] . "</td>
<td>" . $row['occdate'] . "</td>
</tr>";
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
I have indented the echo inside the while loop to keep it inline with the other html.

how do i create html table like on the picture

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>";
}
?>

Categories