looping through table rows php CSS - php

I am looping through an array of results that I have gathered from the database. There are no problems in regards to displaying the data gained from the database. The problem sits within the styling of the data.
I have some CSS for the code below, it styles the first row of data but the rest are just echoed with no styling although they do look as if they sit within the table that is being defined within the php.
$user = $_SESSION['sess_uid'];
$conn = new mysqli(localhost, root, DBPASS, DBNAME);
$sql = "SELECT * FROM reports WHERE userID = '" . $conn->real_escape_string($user)."';";
// Performs the $sql query on the server
$result=mysqli_query($conn,$sql);
$rows[] = array();
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th> Report Name </th>';
echo '<th> Category Name </th>';
echo '<th> Sub Category Name </th>';
echo '<th> Date Uploaded </th>';
echo '</tr>';
echo '</thead>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tbody>';
$rows[] = $row;
echo '<tr>';
echo '<td>'.$row['reportName'].'</td>'.'<td>'.$row['categoryName'].'</td>'.'<td>'.$row['subcategoryName'].'</td>'.'<td>'.$row['reportDateUploaded'].'</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
};
Any thoughts?
Thanks

You need to make your while statement only contain your tr and td elements.

You need to put the tbody and table tags out of your while loop.
echo '<tbody>';
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
echo '<tr>';
echo '<td>'.$row['reportName'].'</td>'.'<td>'.$row['categoryName'].'</td>'.'<td>'.$row['subcategoryName'].'</td>'.'<td>'.$row['reportDateUploaded'].'</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';

Related

Pulling data from database and into an html table

I'm pulling data from a database but only getting the first row into the dynamically produced html table.
I've tried adding another foreach loop but that isn't the answer… I'm out of ideas...
$conn = new PDO('mysql:host=localhost;dbname=jeuxvideo', $dbUserName, $dbPassword);
$sql = "SELECT * FROM jeuxvideo";
$result = $conn->prepare($sql);
$request = $result->execute();
echo "<table border='1'>";
echo "<tr><td>Id</td><td>Titre</td><td>Prix<td>Date de Sortie</td><td>Genre</td><td>Origine</td><td>Mode</td><td>Connexion</td></tr>\n";
$row = $result->fetch(PDO::FETCH_ASSOC);
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
echo "</table>";
This code pulls all the correct info and puts it in the right place in the html table, but for some Reason it doesn't collect the Following rows data...
This line only fetches one row:
$row = $result->fetch(PDO::FETCH_ASSOC);
You need to fetch as many rows it can give you. Once there are no more rows to fetch the function will return FALSE, so you can use with the while loop, like this:
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
}
echo "</table>";
Since you don't have any variables you need prepared for your query, you can simply do:
<?php
$stmt = $pdo->query('SELECT * FROM jeuxvideo');
echo '<table>';
foreach ($stmt as $row) {
echo '<tr>';
echo "<td>$row['name']</td>";
echo "<td>$row['url']</td>";
echo "<td>$row['timestamp']</td>";
echo '</tr>';
}
echo '</table>';
A 2nd method would be:
<?php
$stmt = $pdo->query('SELECT * FROM jeuxvideo');
echo '<table>';
while ($row = $stmt->fetch()) {
echo '<tr>';
echo "<td>$row['name']</td>";
echo "<td>$row['url']</td>";
echo "<td>$row['timestamp']</td>";
echo '</tr>';
}
echo '</table>';
Try this
$conn = new PDO('mysql:host=localhost;dbname=jeuxvideo', $dbUserName, $dbPassword);
$sql = "SELECT * FROM jeuxvideo";
$result = $conn->prepare($sql);
$request = $result->execute();
echo "<table border='1'>";
echo "<tr><td>Id</td><td>Titre</td><td>Prix<td>Date de Sortie</td><td>Genre</td><td>Origine</td><td>Mode</td><td>Connexion</td></tr>\n";
$row = $request->fetch_all();
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr>";
echo "</table>";

Missing the first row of a SELECT statment

I have a database table called 'Modules' and I am trying to select all of the rows from that table and display them in a table. Each row has a column called MOrder which ranges from 1 - how ever many modules are available.
Here is my code:
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
However, for whatever reason the statement is missing out the module with MOrder 1 and always starting with 2?
Why is this?
You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC); in the third line of your pasted code, which is pulling the first array from the results.
This is then being overwritten in your while loop:
while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
//...
}
Because in the 3rd line of your code you call
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
once, and then in the loop you start calling the
$row = mysqli_fetch_array($result)
again, thus overwriting the $row variable with the 2nd row. Get rid of the 1st $row = mysqli_fetch_array($result) line.
Try this code.
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) { //for every fetch we'll get one row.
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
At the beginning, you fetch the first row.
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
In while loop, fetch function returns second row.

Fetch complex table data from database

I have a table in database and I want to fetch the data from it. I know how to fetch data from database using php. But the problem is that
this table is different in structure. For example
and so on...
As you can see this table is like a pivot table. That is the real problem for me. All the entries of a single person should be in single row. But it is in different rows and single column (all entries of single person is in value column).
I have tried to fetch table with regular way like this
<?php
/*
Template Name: Registration
*/
get_header();
$results = $wpdb->get_results( "SELECT * FROM wp_cf_form_entry_values", ARRAY_A );
$count = $results->num_rows;
if(!empty($results)) {
echo "<table width='100%' border='1' cellspacing='1'>";
echo "<tbody>";
foreach($results as $row){
echo "<tr>";
echo "<td><center>" . $row['value'] . "</center></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} else {
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
get_footer();?>
But with the above code it's showing all the results in a single column and that's what I expect from above code.
So I don't know the correct code to represent this table as single row entries of single person. I searched a lot but didn't get much.
I'm pretty sure you looking for something like this:
horizontal representation: (by Person)
<?php
/*
Template Name: Registration
*/
get_header();
global $wpdb;
//Create a mulit dimensional array for each person.
$persons = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
$persons[$field->entry_id][$field->slug] = $field->value;
}
//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($persons as $id=>$values){
//the table header (only in the first loop)
if($i==0){
echo '<tr>';
foreach($values as $key=>$val){
echo '<th>'.$key.'</th>';
}
echo '</tr>';
}
// one line per person
echo '<tr id="'.$id.'">';
foreach($values as $key=>$val){
echo '<td>'.$val.'</td>';
}
echo '</tr>';
$i++;
}
if($i==0){
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
echo '</table>';
get_footer();?>
vertical representation (by field slug)
<?php
/*
Template Name: Registration
*/
get_header();
global $wpdb;
//Create a mulit dimensional array for each slug.
$field_slugs = array();
$fields = $wpdb->get_results( 'SELECT * FROM wp_cf_form_entry_values');
foreach($fields as $field){
$field_slugs[$field->slug][$field->entry_id] = $field->value;
}
//the table
echo '<table width="100%" border="1" cellspacing="1">';
$i=0;
foreach($field_slugs as $slug=>$values){
//the table header (only in the first loop)
if($i==0){
echo '<tr>';
foreach($values as $person_id=>$val){
echo '<th></th>';
echo '<th>Person '.$person_id.'</th>';
}
echo '</tr>';
}
// one line per person
echo '<tr id="'.$slug.'">';
echo '<td><b>'.$slug.'</b></td>';
foreach($values as $person_id=>$val){
echo '<td>'.$val.'</td>';
}
echo '</tr>';
$i++;
}
if($i==0){
echo "<table width='100%' border='1' cellspacing='1'><tr><td>No Records Found</td></tr></table>";
}
echo '</table>';
get_footer();?>
You use foreign key entries_id so you can retrieve from that table ie
$results=$wpdb->get_results("select * from entries_table");
foreach($results as $result){
$entries_id=$result->id;
$entry_detail=$wpdb->get_results(SELECT * from form_field_table where entries_id='$entries_id' ");
foreach($entry_detail as $ent_detail){?>
<td><?php echo $ent_detial->slug;?></td><td><?php echo $ent_detial->value;?></td>
<?php }
}

php PDO read to table statement

here is the code
$sql = "SELECT * FROM $table ";
foreach ($db->query($sql) as $row) {
echo "<tr>"."\n";
echo '<td>'. $row[CODE].'</td>';
echo '<td>'. $row[FIRME_CODE].'</td>';
echo '<td>'. $row[NOM_LEGAL].'</td>';
echo '<td>'. $row[NO_CIVIQUE].'</td>';
echo '<td>'. $row[NOM_RUE].'</td>';
.... and so on ...
echo "</tr>"."\n\n";
}
the question, how it's possible to output all the value found from the query into a table WITHOUT having to tell each array table value individually...
it does not make sens... so there must be a way to do it, fast...
--
after changing it to this.. it output the word : Array in each table cell
<td> Array </td>... not good !
So the question is... what is the error in this code ?
$sql = "SELECT * FROM $table ";
$rows = $db->query($sql);
echo '<tr>';
foreach ($rows as $key => $value) {
echo '<td>', $key, '</td>';
}
echo '</tr>';
echo '<tr>';
foreach ($row as $key => $value) {
echo '<td>', htmlspecialchars($value), '</td>';
}
echo '</tr>';
Note that I've added htmlspecialchars() which will escape your arbitrary data for use in HTML, generating valid HTML and preventing against potential XSS attacks (depending on where that data comes from).
put the query result in an array
and then foreach the array
$sql = "SELECT * FROM $table ";
$rows = $db->query($sql);
foreach ($rows as $row) {
echo '<td>'.implode('</td><td>',$row).'</td>';
}

Creating grid rather than a table

For the table below, rather than paginate, I would like to create a grid of results.
So I would like to echo out a set of 10 results, then continue the process 200 pixels to the right. Then after 10 more results, continue 200 pixels to the right of that.
How can I do this?
$query2 = "SELECT street1, city, state, zip, phone, website
FROM addresses
WHERE submissionid=$submissionid
ORDER BY street1 DESC";
$result2 = mysql_query($query2);
$arr2 = array();
echo "<table class=\"commentecho3\">";
while ($row2 = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td>'.strtoupper($row2["street1"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["city"]).', '.strtoupper($row2["state"]).' '.strtoupper($row2["zip"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["phone"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td>'.strtoupper($row2["website"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
echo '<td></td>';
echo '</tr>';
}
echo "</table>";
Is it really necessary to use tables there? Like Webgal suggested, could be really easy done using floated divs:
Create a class in css
.foo{float:left;padding-right:200px;}
Update PHP (this can be done different ways):
$i=0;
echo('<div class="foo">');
while ($row2 = mysql_fetch_array($result2)) {
echo '<p>'.strtoupper($row2["street1"]).'</p>';
.................................
$i++;
if ($i=10){
echo('</div><div class="foo">');
}
}
echo('</div>');

Categories