Can someone explain what went wrong with my code? [closed] - php

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>';

Related

How to fix broken line of table when querying information from database

am trying to get data from the database using php i have about four fields in my database, using the code below my table gets broken when querying from database into the table..
this is mycodeenter image description here
public function departments_view($dept_id){
$query = $this->db->prepare("SELECT * FROM materials_tbl WHERE dept_id = $dept_id");
$query->execute();
if($query->rowCount()>0){
?>
<div class="table-responsive">
<?php
while($row=$query->fetch(PDO::FETCH_ASSOC)){
?>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Material Code</th>
<th>Topic</th>
<th>Description</th>
<th>Path</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row["material_code"];?></td>
<td><?php echo $row["topic"];?></td>
<td><?php echo $row["description"];?></td>
<td><?php echo $row["path"];?></td>
</tr>
</tbody>
</table>
<?php
}
?>
</div>
<?php
}else {
echo 'Nothing here.';
}
}
Try this code,
Actually what mistake you've made is you've put table, tbody and table header inside the loop, that's why your table was breaking.
public function departments_view($dept_id){
$query = $this->db->prepare("SELECT * FROM materials_tbl WHERE dept_id = $dept_id");
$query->execute();
if($query->rowCount()>0){
?>
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Material Code</th>
<th>Topic</th>
<th>Description</th>
<th>Path</th>
</tr>
</thead>
<tbody>
<?php
while($row=$query->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row["material_code"];?></td>
<td><?php echo $row["topic"];?></td>
<td><?php echo $row["description"];?></td>
<td><?php echo $row["path"];?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}else {
echo 'Nothing here.';
}
}

table inside table in php wrong output

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>

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

variable which needs a fix

I was trying to echo a variable before it was even defined so literally i'm getting this error "UNDEFINED VARIABLE: tpp"
<tr>
<td colspan='6'>
<h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4>
</td>
</tr>
When i set that line down along with echo "" , it works. But i want it on the place where i set it [Top]
Here is the code which i need help with
<table class="table table-bordered">
<thead>
<tr>
<td colspan='6'>
<h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4>
</td>
</tr>
<td>Number</td>
<td>Username</td>
<td>Last Login</td>
</thead>
<?php
$query = $koneksi->prepare("SELECT `user`, `TP`, `LastOnlineDate` FROM `playerdata` WHERE `banned`=0 AND `TP`=1");
$query->execute();
if($query->rowCount() == 0)
{
echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
}
$tpp = 0;
while($data = $query->fetch())
{
$tpp++;
echo "<tr><td>".$tpp."</td>";
echo "<td>".$data['user']."</td>";
echo "<td>".$data['LastOnlineDate']."</td></tr>";
}
?>
</table>
Any help will be so appreciated
thanks in advance.
$tpp is undefined because its being called before its been created, Try this out instead.
<?php
$tpp = 0;
$content = "";
$query = $koneksi->prepare("SELECT `user`, `TP`, `LastOnlineDate` FROM `playerdata` WHERE `banned`=0 AND `TP`=1");
$query->execute();
while($data = $query->fetch())
{
$tpp++;
$content .= "<tr><td>".$tpp."</td>";
$content .= "<td>".$data['user']."</td>";
$content .= "<td>".$data['LastOnlineDate']."</td></tr>";
}
?>
<table class="table table-bordered">
<thead>
<tr>
<td colspan='6'><h4><small>Trusted Players List <?php echo $tpp ?> Total)</small></h4></td>
</tr>
<td>Number</td>
<td>Username</td>
<td>Last Login</td>
</thead>
<?php
if($query->rowCount() == 0)
{
echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
}
echo $content;
?>
</table>
I've decided to run the loop at the top, placing all the data within $content and then echo $content where you wish to display the content. This allows you to still have your counter run and be displayed at the top of the table.

How to retrieve information from database and display in table?

This is my php code.
<?php
session_start();
foreach($_POST AS $key => $val) {
$_SESSION[$key]=$val;
}
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid=$_POST['cnetid'];
$cpassword=$_POST['cpassword'];
$cnetid = stripslashes($cnetid);
$cpassword = stripslashes($cpassword);
$cnetid = mysql_real_escape_string($cnetid);
$cpassword = mysql_real_escape_string($cpassword);
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$result=mysql_query($sql) OR die(mysql_error());
$row=mysql_fetch_assoc($result);
$rentalid=$row['RentalID'];
$bikeid=$row['BikeID'];
?>
This is the html code. It should be displayihg the query that's been retrieved from the database but for some reason when I run it the table comes up blank. I know I'm only echoing two variables but even those come up empty.
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<table class="table table-striped">
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $rentalid ?></td>
<td><?php echo $bikeid ?></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table><br>
</div><!-- end span -->
Try this:
---------------------------EDITED-----------------------------------------
<?php
session_start();
if(isset($_POST['cnetid'])){
mysql_connect('localhost', 'bikec_user', '4348#TxState');
mysql_select_db('bikecats_database');
$cnetid = mysql_real_escape_string($_POST['cnetid']);
$cpassword = mysql_real_escape_string($_POST['cpassword']);
$table = "<table class='table table-striped' >
<thead>
<tr>
<th>Rental ID</th>
<th>Bike ID
<th>Check-Out Date</th>
<th>Return Date</th>
</tr>
</thead>
<tbody>
";
$sql="SELECT RentalID, BikeID, RentalStartDate, RentalEndDate
FROM rental
WHERE CustTxStateNetID = '$cnetid'";
$body = "";
$result=mysql_query($sql) OR die(mysql_error());
while ($row=mysql_fetch_assoc($result))
{
$body = $body." <tr><td>".$row['RentalID']."</td>
<td>".$row['BikeID']."</td>
<td>".$row['RentalStartDate']."</td>
<td>".$row['RentalEndDate']."</td></tr> ";
}
$table = $table.$body."
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>" ;
}
else
{
$table = "No data..";
}
?>
<div class="span9">
<h2>My Account</h2>
<p><strong>My Rentals</strong></p>
<?php echo $table; ?>
<br>
</div>
PS: Error found, corrected, and code tested xD
Saludos ;)

Categories