Fetch complex table data from database - php

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 }
}

Related

Using OOP with PDO

I am using a class that fetches information from a database and shows that information in a table, along with an edit and delete button. When I click the edit or delete button, I want to add $_GET['id'] to the link, but I am confused on how to accomplish this.
public function select($tName,$from,$where=1){
$key=[];
$con=$this->con;
$sql="SELECT $tName FROM $from WHERE $where";
$data = $con->query($sql);
$fullRec=[];
foreach($data as $k=>$rows){
$fullRec[]=$rows;
}
foreach($rows as $k=>$v){
$key[] = $k;
}
//create table
echo "<table border='1'>";
//create one row with col name selected!!
echo "<tr>";
for($z=0;$z<count($key);$z+=2){
echo "<td>".$key[$z]."</td>";
}
echo "<td>actions</td>";
echo "</tr>";
//create one row for each record comes!!
for($i=0;$i<count($fullRec);$i++){
echo "<tr>";
//create one table data for each record comes!!
for($j=0;$j<count($fullRec[$i])/2;$j++){
echo "<td>".$fullRec[$i][$j]."</td>";
}
echo "<td>EditDelete</td>";
echo "</tr>";
}
echo "</table>";
}
use like this to pass the id of the record in get parameter
echo "<a href='your_route?del=".$rows['id']."'>DELETE</a>";

Writing the attributes of a database in PHP

I am writing an application in which user can enter a database name and I should write all of its contents in table with using PHP.I can do it when I know the name of database with the following code.
$result = mysqli_query($con,"SELECT * FROM course");
echo "<table border='1'>
<tr>
<th>blablabla</th>
<th>blabla</th>
<th>blablabla</th>
<th>bla</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['blabla'] . "</td>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['bla'] . "</td>";
echo "</tr>";
}
echo "</table>";
In this example I can show it since I know the name of table is course and it has 4 attributes.But I want to be able to show the result regardless of the name the user entered.So if user wants to view the contents of instructors there should be two columns instead of 4.How can I accomplish this.I get the table name with html.
Table:<input type="text" name="table">
Edit:Denis's answer and GrumpyCroutons' answer are both correct.You can also ask me if you didnt understand something in their solution.
Quickly wrote this up, commented it (This way you can easily learn what's going on, you see), and tested it for you.
<form method="GET">
<input type="text" name="table">
</form>
<?php
//can be done elsewhere, I used this for testing. vv
$config = array(
'SQL-Host' => '',
'SQL-User' => '',
'SQL-Pass' => '',
'SQL-Database' => ''
);
$con = mysqli_connect($config['SQL-Host'], $config['SQL-User'], $config['SQL-Pass'], $config['SQL-Database']) or die("Error " . mysqli_error($con));
//can be done elsewhere, I used this for testing. ^^
if(!isSet($_GET['table'])) { //check if table choser form was submitted.
//In my case, do nothing, but you could display a message saying something like no db chosen etc.
} else {
$table = mysqli_real_escape_string($con, $_GET['table']); //escape it because it's an input, helps prevent sqlinjection.
$sql = "SELECT * FROM " . $table; // SELECT * returns a list of ALL column data
$sql2 = "SHOW COLUMNS FROM " . $table; // SHOW COLUMNS FROM returns a list of columns
$result = mysqli_query($con, $sql);
$Headers = mysqli_query($con, $sql2);
//you could do more checks here to see if anything was returned, and display an error if not or whatever.
echo "<table border='1'>";
echo "<tr>"; //all in one row
$headersList = array(); //create an empty array
while($row = mysqli_fetch_array($Headers)) { //loop through table columns
echo "<td>" . $row['Field'] . "</td>"; // list columns in TD's or TH's.
array_push($headersList, $row['Field']); //Fill array with fields
} //$row = mysqli_fetch_array($Headers)
echo "</tr>";
$amt = count($headersList); // How many headers are there?
while($row = mysqli_fetch_array($result)) {
echo "<tr>"; //each row gets its own tr
for($x = 1; $x <= $amt; $x++) { //nested for loop, based on the $amt variable above, so you don't leave any columns out - should have been <= and not <, my bad
echo "<td>" . $row[$headersList[$x]] . "</td>"; //Fill td's or th's with column data
} //$x = 1; $x < $amt; $x++
echo "</tr>";
} //$row = mysqli_fetch_array($result)
echo "</table>";
}
?>
$tablename = $_POST['table'];
$result = mysqli_query($con,"SELECT * FROM $tablename");
$first = true;
while($row = mysqli_fetch_assoc($result))
{
if ($first)
{
$columns = array_keys($row);
echo "<table border='1'>
<tr>";
foreach ($columns as $c)
{
echo "<th>$c</th>";
}
echo "</tr>";
$first = false;
}
echo "<tr>";
foreach ($row as $v)
{
echo "<td>$v</td>";
}
echo "</tr>";
}
echo "</table>";
<?php
$table_name = do_not_inject($_REQUEST['table_name']);
$result = mysqli_query($con,'SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME='. $table_name);
?>
<table>
<?php
$columns = array();
while ($row = mysql_fetch_assoc($result)){
$columns[]=$row['COLUMN_NAME'];
?>
<tr><th><?php echo $row['COLUMN_NAME']; ?></th></tr>
<?php
}
$result = mysqli_query($con,'SELECT * FROM course'. $table_name);
while($row = mysqli_fetch_assoc($result)){
echo '<tr>';
foreach ($columns as $column){
?>
<td><?php echo $row[$column]; ?></td>
<?php
}
echo '</tr>';
}
?>
</table>

How can I output an HTML table using PHP through an array from MySQL?

I am trying to output this to a single table through an array. I don't want to put one table for each id, instead I want to put all ids and fields into one table.
My code
$query = "SELECT * FROM cars.class";
$resultset = $conn->query($query, PDO::FETCH_OBJ);
while ($class=$resultset->fetch()){
"<p>".$class->class_id." ".$class->name." ".$class->description. "</p>";
$class = array(
array('class_id' => $class->class_id,'name' => $class->name,'description'=>$class->description));
foreach($class as $classes)`
{
echo "<ul>";
foreach($classes as $class_fact=>$value){
//echo "<li>".$class_fact." : ".$value."</li>";
}
echo "</ul>";
}
echo '<table border=3>';`
echo'<tr>
<th>ID</th>
<th>NAME</th>
</tr>';
foreach($classes as $class_fact=>$value){
echo "<tr>
<td>".$class_fact."</td>
<td>".$value."</td>
</tr>";
}
echo "</table>";

looping through table rows php CSS

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

Create multi-row tables in PHP using MySQL

I'm trying to create something like the following:
But my current code has given me:
The part in white is what I get with the code below, and the part in black is what I get when I add in two more <td> rows. The problem is I can't get 3 pieces of data per row to be created - only vertically or horizontally.
$result = mysqli_query($con,"SELECT * FROM show_listing");
while($row = mysqli_fetch_array($result))
{
echo "<table>
<tr>
<td>".$row['name']."'</td>
</tr>
</table>";
}
mysqli_close($con);
It may be simpler than I think but does anyone know how I could do this?
$c = 0;
echo "<table>";
while($row = mysqli_fetch_array($result))
{
if ($c%3 == 0) {
if ($c) echo "</tr>";
echo "<tr>";
}
echo "<td>".$row['name']."</td>";
$c++;
}
if ($c) echo "</tr>";
echo "</table>";
You have the correct idea. You do not want the declaration in the while loop as the result is that you will get multiple tables.
So I moved the table declarations outside of the while loop. I also made the declarations conditional based on the current column. You can now set the variable $columns to however many columns you want.
$result = mysqli_query($con,"SELECT * FROM show_listing");
echo "<table>"
$columns=3;
$current_column=1;
while($row = mysqli_fetch_array($result))
{
if($current_column==1) {
echo "<tr>";
}
echo "<td>".$row['name']."'</td>";
if($current_column==$columns) {
echo "</tr>";
$current_column=1;
} else
$current_column++;
}
echo "</table>
mysqli_close($con);

Categories