Database rows not displaying fully in HTML table - php

I am attempting to display mySQL data in a HTML table through php. The first row is displaying correctly, however the other row sets are not being organised and displayed in my table, rather just echoing out at the bottom of the container with no structure.
I think that since the data is being displayed, albeit not in the table, that my query is correct, im just unsure on how to proceed.
Is this an issue with my table structure?
I tried adding a second:
echo '<td>' . $data['studentNumber'] . '</td><td>' . $data['handle'] . '</td><td>' . $data['email'] . '</td>';
underneath my first echo, but it just duplicated everything.
Here is the entire code in question: Screenshot Here
<div class="container-fluid">
<div class="row">
<div class="col-md details">
<p class="details_title"></p>
<?php
$getAllStudentsTable = "SELECT * FROM users WHERE accessLevel = 3";
$result = (mysqli_query($conn, $getAllStudentsTable));
echo '<table class="table">
<thead class="thead-dark">';
echo' <tr>
<th scope="col">Student Number</th>
<th scope="col">Handle</th>
<th scope="col">Email Address</th>
</tr>
</thead>'; //table headers
while ($data = mysqli_fetch_array($result)) {
echo' <tbody> <tr>';
echo '<td>' . $data['studentNumber'] . '</td><td>' . $data['handle'] . '</td><td>' . $data['email'] . '</td>';
echo'</tr> </tbody> </table>';
}
?>
Can anyone point me in the right direction on this?
(I am relatively new to PHP, SO and this is the first attempt ever at trying to display database data into an HTML table.)
I have attached a link for a screenshot of the issue (Not yet allowed to post pictures lol).
Thanks!

You're echoing most of the table structure in your loop, where you should just be echoding the rows. As a necessary debugging step, take a look at the View Source in your browser and see what the table structure is. You'll find multiple <tbody> elements and multiple closing </table> tags, confusing the browser.
Basically, remove the various <tbody> and <table> tags from your loop and just echo them around the loop. Something like this:
echo '<tbody>';
while ($data = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>' . $data['studentNumber'] . '</td><td>' . $data['handle'] . '</td><td>' . $data['email'] . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
All you want to repeat in the loop is each <tr> element and its children.

Related

Failed to load resource: the server responded with a status of 500 (); works in local environment

I am working on posting a CRUD Application to where the user logs in first. The application is made with 2 MySQL tables; one for logging in and the other for the CRUD values. This example hasn’t worked on my local environment using both XAMP/PC and MAMP/MAC.
When I uploaded it to my host it does work, however the CRUD values don’t populate when it the for each loop is specified. I concatenated the mysql_error(); to explain why this is not working on my host.
On line 34 in logincrud/main.php:
The code from the table in main.php:
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Number</th>
<th>Explanation</th>
<th>Date Accured</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM saftey ORDER BY id DESC';
foreach (($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['ins_n'] . '</td>';
echo '<td>'. $row['explanation'] . '</td>';
echo '<td>'. $row['date'] . '</td>';
echo '<td width=250>';
echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
echo ' ';
echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
The Add, Update, and Delete works on the host as I can see the records from the safety table on myphpadmin. The login/logoff is from a completely different table. Again, the local environment works with no issues.
I opened a help ticket with my host provider, but all I received is they do not troubleshoot Applications.
I have been looking for solutions for a few weeks and all I couldn’t find a solution. In the Chrome browser the console states the error: hp:1 Failed to load resource: the server responded with a status of 500 ()" Server issue? Aware of SQL Injection problems.
I uploaded a GITHUB project if it helps.
Thank you in advance.
I solved the problem why it was not working on my live site. Looking at this Stackoverflow article
The way the PHP Data Object has the forward arrow executes the code. The way this foreach works caused the value return false; in other words not in an array.
I reorganized the PHP Data Object into a variable known as $records.
foreach (($sql) as $row) {
to
$records = $pdo->query($sql);
foreach ($records as $row) {
changed the echo to print
}
Works now. Weird how echo is different than print in this situation.
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Number</th>
<th>Explanation</th>
<th>Date Accured</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM safety ORDER BY id DESC';
$records = $pdo->query($sql);
foreach ($records as $row) {
print '<tr>';
print '<td>'. $row['ins_n'] . '</td>';
print '<td>'. $row['explanation'] . '</td>';
print '<td>'. $row['date'] . '</td>';
print '<td width=250>';
print '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
print ' ';
print '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
print ' ';
print '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
print '</td>';
print '</tr>';
}
Database::disconnect();

How to fetch a specific data from a table and display that specific data in another page?

Hi there:) I have created a table where there's a list of products.
if ($result) {
echo '<table align="center">
<tr><th><b>ID</b></th><th><b>Name</b></th><th><b>Made_In</b>
</th><th><b>Price</b></th></tr>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr><td >' . $row['ID'] . '</td>
<td >' . $row['Name'] . '</td>
<td >' . $row['Made_In'] . '</td>
<td >' . $row['Price'] . '</td></tr>';
}
echo '</table>';
Expected Output:
When the user want to see more details about this particular product, for example Product A. The user just have to click the product name and it will take the user to a new page where the user can see more details about the Product A such as description, ingredient, company's details etc.
Just so you know all of this is using the same table from the database called Product.
I have tried make the product's name as a link but i don't know how to fetch a specific data from the table to a new page.
I know this seems easy but i can't find the answer anywhere. Thank you for your time
<?php
if($result){?>
<table align="center">
<tr><th><b>ID</b></th><th><b>Name</b></th><th><b>Made_In</b>
</th><th><b>Price</b></th></tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {?>
<tr>
<td ><?php echo $row['ID'];?> </td>
<td ><?php echo $row['Name']; ?></td>
<td ><?php echo $row['Made_In'];?></td>
<td ><?php echo $row['Price'];?></td>
</tr>
}
<?php } ?>
you need to fetch the particular record in new page.
eg: $id = $_GET['id']; // this will receive from url eg: example.com/products?id=2
and fetch the records using that $id.
Now you can use show table with fetched detail.
Suggestion: you can use php inside html. So, its better to write html table tags and open/close php tags where necessary. This will make your code easier to understand.

PHP foreach using $variable->property

I have some code that runs reports from $results returning from mysql query.
The queries are relatively standard, but their total number is dynamic. So far I have static HTML where it has the same code repeated, but thats only if ok the number of queries == the number of markup repetitions.
So, I am trying to implement a new foreach loop that will determine how many "queries" there are, and only markup for each one, and then $total at the end.
To date I have had static php foreach loop like this, works well.
<div class="row">
<div class="col-xs-12">
<table class="table table-striped">
<thead>
<tr>
<th>Year</th>
<th>Month</th>
<th>Item</th>
<th class="text-right">Minimum</th>
<th class="text-right">Maximum</th>
<th class="text-right">Change</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $result) :
if ($result->id == 151){
echo '<tr>';
echo '<td>', $result->Year, '</td>';
echo '<td>', $result->Month, '</td>';
echo '<td class="text-centre">', $result->name, '</td>';
echo '<td class="text-right">', $result->min, '</td>';
echo '<td class="text-right">', $result->max, '</td>';
echo '<td class="text-right">', $result->cumulative, '</td>';
$thirdtotal += $result->change;
$total += $result->change;
echo '</tr>';
}
endforeach; ?>
Where I am stuck is, do i put the above inside another for each loop? My thought is, if a query exists, it will have an "id" field in the array, so for the number of "id", thats how many outputs you'll get.
<?php foreach ($results as $key => $value;) :
{
echo '<tr>';
echo '<td>' $value, '</td>';
echo '</tr>';
}
endforeach; ?>
This piece of test code doesnt work, am I using the key value correctly? Is it the correct approach to solve this problem and reduce the amount of php in the controller?
Thanks

Associating HTML's deleteRow() with a table generated by PHP

I have the following PHP code to generate a table where there are rows whose columns have the following attributes: "id", "lastname", and "firstname". I want to add a button for each of the rows that deletes the respective row and have this line to attempt to do so: echo '<td><input type="button" value="Delete" onclick="deleteRow('+$i+')"></td>'; in the following code. However, it's printing the value of $i (1,2,3,4,... which are the row numbers I want associated with the buttons) instead of having delete buttons dedicated to each row (with the follow code below, it doesn't create any buttons unless I specifically remove the '+$i+' part). What is the issue?
<table>
<tr>
<td></td>
<td>id</td>
<td>Last Name</td>
<td>First Name</td>
</tr>
<?php
$i = 1;
foreach ( $names['result'] as $a ){ // $a is an array
echo '<tr>';
echo '<td><input type="button" value="Delete" onclick="deleteRow('+$i+')"></td>'; // issue is here
echo '<td>' . $q['id'] . '</td>';
echo '<td>' . $q['lastName'] . '</td>';
echo '<td>' . $q['firstName'] . '</td>';
echo '</tr>';
}
?>
</table>
The . is the string concatenation operator in php language whereas the + is a concatenation operator in javascript language.
You should use onclick="deleteRow('.$i.')"
And then, just before ending the loop, just use a $i++;

Use data from an SQL query, but don't display column in table

EDIT: If I use $colName instead of $colname, I'd have no issues. Stupid, simple fix.
I have a query pulling 6 columns from multiple tables. The final column is necessary to be used in a dynamic URL I'm generating, but I don't want that column to display on my table. How can I hide it/make it not show up? Is there a way to call the data, but not use it in my table?
Query ex:
SELECT a, b, c, d, e, exTable.f as CID
FROM table exTable
JOIN <other tables>
WHERE stuff happens
Table (you can see that my header row will hide fine, but the content won't):
<table>
<thead>
<tr>
<th class="header"><strong>A</strong></th>
<th class="header"><strong>B</strong></th>
<th class="header"><strong>C</strong></th>
<th class="header"><strong>D</strong></th>
<th class="header"><strong>E</strong></th>
<th class="header" style="display:none"><strong>F</strong></th> <!-- THIS WORKS -->
</tr>
</thead>
<tbody>
<?
foreach($tableData as $row)
{
echo "<tr>";
foreach($tableColNames as $colName)
{
if ($colname=='CID') {
echo "<td style='display:none'>" . $row[$colName] . "</td>"; <!-- THIS DOES NOT -->
}
elseif ($colName=='e') {
echo "<td><a href='http://my.url.here/".$row[CID]."_Document.pdf' target='_blank'>" . $row[$colName] . " </a></td>";
}
else {
echo "<td>" . $row[$colName] . "</td>";
}
}
echo "</tr>";
}
?>
</tbody>
</table>
You have a typo: $colname should be $colName.
if ($colname=='CID') {
Stupid, simple solution: use $colName instead of $colname.

Categories