Displaying column names and values - php

I want to display the column names along with the values in the PHP page.
while($get_info = mysql_fetch_row($orderdetails))
{
foreach ($get_info as $field)
{
echo "<td>" . $field . "</td>";
}
echo '</tr>';
}
This fetches just the values. How do I display the column names too?
The column names are order_id, productid, product_discount, amount, customerid, order_date.

while($get_info=mysql_fetch_array($orderdetails))
{
foreach ($get_info as $key => $val)
{
echo "<td>" .$key. ': ' . $val . "</td>";
}
echo '</tr>';
}

You are just missing the key from your foreach :
while($get_info=mysql_fetch_assoc($orderdetails))
{
foreach ($get_info as $field => $value)
{
echo "<td>" .$field.': '.$value."</td>";
}
echo '</tr>';
}
You might want to review your foreach php documentation for more information:
http://php.net/manual/en/control-structures.foreach.php

If you want each field to contain the column name, change it to mysql_fetch_array and do:
foreach($get_info as $key => $value) {
echo "<td>$key: $value</td>";
}
If you want the column names to be at the top of the table, you can either check the first row (if you're sure the table won't be empty):
$first = true;
while($get_info = mysql_fetch_assoc($orderdetails)) {
echo '<tr>';
if($first) {
$first = false;
foreach(array_keys($get_info) as $columnName) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr><tr>';
}
foreach($get_info as $field) {
echo '<td>' . $field . '</td>';
}
echo '</tr>';
}
If you're not sure that the table will have at least one element, I would use a second DESCRIBE query.

while ($get_info=mysql_fetch_assoc($orderdetails))
{
foreach ($get_info as $columnName => $field)
{
echo "<td>$columnName: $field</td>";
}
echo '</tr>';
}
Notice that I am using mysql_fetch_assoc() which fetches the row having the column names as the keys.

<?php
while($get_info=mysql_fetch_array($orderdetails))
{
foreach ($get_info as $key => $val)
{
echo "column is " .$key. 'and value is ' . $val ;
}
echo '</br>';
}
?>

A lot of people are using a key => val demo but your code will work its just that your $field is now an a key so you need to tell it what column to look at.
When echoing just go
echo "<td>" . $field->column . "</td>";
That should work.

Related

Display PHP Query results in table regardless of # of columns returned?

So I'm exploring the wonderful world of PHP and I'm still creating very dirty, poorly build code but I'm trying to get better! So my question is as follows:
Is there a way to automatically calculate the columns in the result set and spit out a pretty HTML table regardless of query used?
Here the current code:
<?php
include '../includes/connect.php';
include '../includes/queries.php';
$stid = oci_parse($conn, $export);
oci_execute($stid);
echo "<table class='pure-table pure-table-striped' style='font-size:11px;'><thead><tr><th>Name</th><th>File Name</th><th>Export Date</th></tr></thead>";
while (oci_fetch($stid)) {
echo "<tr><td>" . oci_result($stid, 'DISPLAY_NAME') . "</td>";
echo "<td>" . oci_result($stid, 'LAST_EXPORT_FILE') . "</td>";
echo "<td>" . oci_result($stid, 'LAST_EXPORT_DATE') . "</td></tr>";
}
echo "</table>\n";
oci_free_statement($stid);
oci_close($conn);
I'd like to use the same table every time, but just have it auto detect column header names and number of columns and return it in a table.
Is it possible or does it make sense?
I do exactly that using PDO.
$out = '';
$q = $conn->prepare($SQL);
if ($q->execute()) {
$rows = $q->fetchAll();
if (!empty($rows)) {
//We have something
$out .= "<table class='pure-table pure-table-striped' style='font-size:11px;'>";
$first = true;
//iterate on rows
foreach($rows as $row) {
//Clean out all the numeric keys - stops duplicate values
foreach ($row as $key => $value) {
if (is_int($key)) {
unset($row[$key]);
}
}
//header
if ($first) {
//write header
$out .= '<thead><tr>';
foreach ($row as $key => $value) {
$out .= '<th>' . $key . '</th>';
}
$out .= '</tr></thead>';
$first = false;
}
//write line
$out .= '<tr>';
foreach($row as $key => $value) {
$out .= '<td>' . $value . '</td>';
}
$out .= '</tr>';
}
$out .= '</table>';
}
}
echo ($out);

Print foreach value of array in one row

I have a following code
foreach($arr['transactions'] as $a){
foreach($a as $key => $value){
echo "<tr><td>" . $value . "</td></tr>";
}
}
Which gives output in one column.
Now what I want is print these values like below
416990962 COMPLETED Business Send 0183366139 0183366139 -1 0 0 655.99
Can anyone help me regarding this?
Try this
foreach($arr['transactions'] as $a){
echo '<tr>';
foreach($a as $key => $value){
echo "<td>" . $value . "</td>";
}
echo '</tr>';
}
You are getting result in one column because you are using <tr> inside the second loop. you need to use <tr> outside the second loop as:
<?php
foreach($arr['transactions'] as $a)
{
?>
<tr>
<?php
foreach($a as $key => $value)
{
?>
<td><?php echo $value;?></td>
<?php
}
?>
</tr>
<?php
}
?>
Like this:-
echo '<tr>';
foreach($a as $key => $value){
echo "<td>" . $value . "</td>";
}
echo '</tr>';

Add links to data in HTML table Created using PHP and a MYSQL database

I am trying to pull data from a table in PHPmyadmin and convert it to an HTML table based on some customer form input which filters out unneeded rows. The code below does that fine. The issue is that two of my columns need to contain links.
It would be easy enough to use PHP to change the table data into the link using a strtolower() and str_replace() to remove spaces, then concatinating the "www.website.com/" and the ".html". But I'm using a foreach loop to get all of the rows that I need and I don't know how to only alter one value per row.
I have tried using "Broswer Display Transformations" and "Input Transformations" in PHPmyadmin, but that only seems to affect the data in PHPmyadmin and not when I access the data via PHP.
My current code:
//* Code for Table
$query = "SELECT $searchFields FROM `hose_reels` $searchPhrase ORDER BY `model` ASC";
$result = mysqli_query($cxn,$query);
if ($row[$key] != "0") {
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
echo "<th>" . $key . "</th>";
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
echo "<td>$row[$key]</td>";
}
echo "</tr>";
}
echo "</table>";
}
else {
echo "<p>No results match your selection. Please broaden your search.</p>";
}
Just add <a> tag in your php code. Below is the code. One more thing you have error in echo "<td>$row[$key]</td>"; line . it prints <td>$row[$key]</td> not the result you are fetching from DB.
echo '<table width="100%" border="1" class="table"><tr>';
$row = $result->fetch_assoc();
$i = 1;
foreach ($row AS $key => $value) {
$key = ucwords(str_replace('_', ' ', $key));
if($i == 1 || $i ==3){
echo "<th><a href='".key ."'" . $key . "</a></th>";
}else{
echo "<th>" . $key . "</th>";
}
$i++;
}
echo "</tr>";
$result2 = mysqli_query($cxn,$query);
$j =1;
while($row = $result2->fetch_assoc()) {
echo "<tr>";
foreach ($row AS $key => $value) {
$row['$key'] = $value;
if($i == 1 || $i ==3){
echo "<td><a href='".$row[$key]."'".$row[$key]."</a></td>";
}else{
echo "<td>$row[$key]</td>";
}
}
echo "</tr>";
}
echo "</table>";

Building a Table of Data with an Array of Arrays

I have an Array of Arrays and Want to create a Tabular data layout. This is not the exact code, as how their generated is quite the cluster (Coming from COBOL interaction) but this should give me enough to make it work if someone can think of a clean way to code this.
array(
Array(847.0, 97010, 11)
Array(847.0, 97012, 10)
Array(847.1, 97010, 08)
Array(847.1, 97012, 14)
)
So I want to put these into a Table that looks something like
97010 97012
847.0 11 10
847.1 08 14
The first 2 elements of the arrays will always be the two axis and the 3rd the contents of the table.
Any Suggestions? thanks!
$table = array();
$columns = array();
// copy the array (x, y, value) into a table
// keeping track of the unique column names as we go
foreach ($dataSet as $point) {
// provided sample data used floats, ensure it is a string
$x = strval($point[0]);
$y = strval($point[1]);
$data = $point[2];
if (!isset($table[$x])) {
$table[$x] = array();
}
$table[$x][$y] = $data;
// quick and dirty style 'unique on insert'
$columns[$y] = true;
}
// switch the column names from title => true to just titles
$columns = array_flip($columns);
// Display the table
echo '<table>';
// Header row
echo '<tr>';
echo '<th> </th>';
foreach ($columns as $columnTitle) {
echo '<th>' . $columnTitle . '</th>';
}
echo '</tr>';
// Bulk of the table
foreach ($table as $rowTitle => $row) {
echo '<tr>';
echo '<th>' . $rowTitle . '</th>';
foreach ($columns as $columnTitle => $junk) {
if (isset($row[$columnTitle]) {
echo '<td>' . $row[$columnTitle] . '</td>';
} else {
// Handle sparse tables gracefully.
echo '<td> </td>';
}
}
echo '</tr>';
}
echo '</table>';
From what I understood:
$arr[847.0][97010] = '11';
$arr[847.0][97012] = '10';
$arr[847.1][97010] = '08';
$arr[847.1][97012] = '14';
And you may create a table:
$result = "<table>";
foreach($arr as $row_key => $row) {
$result .= "<tr>";
foreach($row as $column_key => $data) {
$result .= "<td>".$data."</td>";
}
$result .= "</tr>";
}
$result .= "</table>";
echo $result;

php - printing keys with embedded function

I need help figuring out these php print (echo) statements and where to place them. I have an embedded function 'strotime' that is transforming time (column 'StartTime') to a format, but I cannot get it to print out correctly. No errors, just no changes or use of the function.
Can someone help me figure out where to place this properly in this foreach loop?
(as you can see, i placed at beginning and tried an if statment too..but no luck). Thanks for
your help.
$keys = array('Server', 'Target','Logdate','Set','StartTime', 'Length','Size','Status');
echo '<table><tr>';
foreach ($keys as $column)
echo '<th>' . $column . '</th>';
echo '</tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($keys as $column)
//if ($column == 'StartTime') {
// echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column =='StartTime') {
echo '<td>' . date("Y-m-d H:i:s",strtotime($row[$column])) . '</td>';
} elseif ($column == 'Status') {
echo '<td> Check for Errors </td>';
} else {
echo '<td> </td>';
}
//}
}
echo '</table>';
In the beginning if foreach ($data as $row){ loop, do this:
$row['StartTime'] = date("Y-m-d H:i:s",strtotime($row['StartTime']));
And then display it like any other column.
Change
if (isset($row[$column])) {
to
if (isset($row[$column]) && $column != "StartTime") {
and by the way: you're missing the </tr> tags.

Categories