get element from an array as a field name in PHP - php

I have several fields from a mysql table (width,diameter etc). Instead of building up the strings from returned data one by one
$rows = $wpdb->last_result; // Wordpress version
foreach(array_chunk($rows, 5) as $i => $pair)
{
$width .= "<tr><td>Width</td>";
$diameter .= "<tr><td>Diameter</td>";
foreach($pair as $row)
{
$width .= "<td>$row->width mm</td>";
$diameter .= "<td>$row->diameter mm</td>";
}
$width .= "</tr>";
$diameter .= "</tr>";
}
Can I turn thickness,diameter,width into an array and loop over them like this:
foreach(array_chunk($rows, 5) as $i => $pair)
{
$measures = array("width"=>"Width","diameter"=>"Diameter","thickness"=>"Thickness","hours"=>"Hours");
foreach($measures as $measure=>$title)
{
$measure .= "<td>$title</td>";
}
foreach($pair as $row)
{
foreach($measures as $measure=>$title)
{
$measure .= "<td>$row->".$measure." mm </td>";
}
}
foreach($measures as $measure=>$title)
{
$measure .= "</tr>";
}
}
But I'm getting Object of class stdClass could not be converted to string error, so is it possible to do that?

This is not allowed:
"<td>$row->".$measure." mm </td>";
I think the concatenation is messing it up, try like this:
"<td>".$row->$measure." mm </td>";
This should be helpful.

Related

Transpose rows and columns in a 2D array [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 4 years ago.
I have created a table that displays a series of numbers in a table. I am trying to transpose the matrix (flip rows and columns) using a for each loop and a function named transpose_matrix but it doesnt seem to be working for me. Where am I going wrong with this? I am working with the following code:
//Creating rows and columns for text file
echo "<h1>Data Table</h1>";
echo "<table border = 0 >";
foreach($result as $key=>$value){
echo "<tr>";
foreach($value as $v){
echo "<td>".$v."</td>";
}
echo "</tr>";
}
echo "</table>";
}
function transpose_matrix($result) {
$transpose = array(); //
foreach ($result as $key => $sub) {
foreach ($sub as $subkey => $subvalue) {
$transpose[$subkey][$key] = $subvalue;
}
}
return $transpose;
}
My first table displays as expected and looks somthing like this:
1 2 3 4 5
6 7 8 9 10
I need it to appear as such (i.e rotating the position of the rows and columns):
1 6
2 7
3 8
4 9
5 10
I have searched StackOverflow for similar questions or solutions but cannot seem to find one that works. I am fairly new to PHP also so apologies if it is a simple fix
This should give you what you need.
function transpose($array_one) {
$array_two = [];
foreach ($array_one as $key => $item) {
foreach ($item as $subkey => $subitem) {
$array_two[$subkey][$key] = $subitem;
}
}
return $array_two;
}
Then just pipe your existing array into the function and render the resulting array.
Check this: https://3v4l.org/OnuSu
$table = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
];
$i = 0;
$transpose = [];
while ($columns = array_column($table, $i++))
{
$transpose[] = $columns;
}
$table = '<table border="1">';
$rows = count($transpose);
for ($i = 0; $i < $rows; $i++){
$cols = count($transpose[$i]);
$table .= '<tr>';
for ($j = 0; $j < $cols; $j++)
{
$table .= '<td>' . $transpose[$i][$j] . '</td>';
}
$table .= '</tr>';
}
$table .= '</table>';
echo $table;
You can just extract the columns of the sub-arrays one-by-one:
foreach(reset($result) as $key => $values) {
$transpose[] = array_column($result, $key);
}
However this is probably better and should work even if the keys are different in each array:
$transpose = array_map(null, ...$result);
Your function seems to be working fine you just need to call it.
Add this below the function:
//Creating rows and columns for text file
$result[0] = ['1','2','3','4','5'];
$result[1] = ['6','7','8','9','10'];
$result[2] = ['11','12','13','14','15'];
$result[3] = ['16','17','18','19','20'];
$result[4] = ['21','22','23','24','25'];
echo "<h1>Data Table</h1>";
echo "<table border = 0 >";
foreach($result as $key=>$value){
echo "<tr>";
foreach($value as $v){
echo "<td>".$v."</td>";
}
echo "</tr>";
}
echo "</table>";
function transpose_matrix($result) {
$transpose = array(); //
foreach ($result as $key => $sub) {
foreach ($sub as $subkey => $subvalue) {
$transpose[$subkey][$key] = $subvalue;
}
}
return $transpose;
}
$mat = transpose_matrix($result);
echo "<h1>Data Table</h1>";
echo "<table border = 0 >";
foreach($mat as $key=>$value){
echo "<tr>";
foreach($value as $v){
echo "<td>".$v."</td>";
}
echo "</tr>";
}
echo "</table>";

How to add a header to a table, and how to add a border in php

I have been writing queries to get information from a table in php. I can print out the information, but it does not look pretty at all. I was wondering how I could make the tables that are outputted better by having black lines as the borders of each cell, and also how to add a column header to my tables. Right now my output for my first query looks like this:
Massachusetts 152082
Missouri 151580
Illinois 111454
And I want my output to look like this (I also want each cell to have a black border):
district population
Massachusetts 152082
Missouri 151580
Illinois 111454
Here is the code for when I print out my table. I dont think you will need any code from the queries so I wont post that. Thanks for the help in advance.
echo "<table>\n";
while($line = pg_fetch_array($result, null, PGSQL_ASSOC)){
echo "\t<tr>\n";
foreach($line as $col_value){
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
try this:
//table header
$table = "<table border='1px'>";
$table .= "<thead>";
$table .= "<tr>";
$i = pg_num_fields($result);
for ($j = 0; $j < $i; $j++) {
$fieldname = pg_field_name($result, $j);
$table .= "<th>$fieldname</th>";
}
$table .= "</tr>";
//table body
$table .= "<tbody>";
while($row = pg_fetch_assoc($result))
{
$table .= "<tr>";
foreach ($row as $key => $value)
{
$table .= "<td>$value</td>";
}
$table .= "</tr>";
}
$table .= "</tbody>";
$table .= "</table>";
//echo table
echo $table;

Using DOM to get dynamic table data but not following tables sort

I've got a table that is default sorted ascendingly by its 2nd column by numeric value.
I'm trying to grab the top 5 rows from this table, but my script doesn't seem to want to follow the tablesorter forced sort order I have, it's just taking the default non sorted data.
So how do I get my script to grab the sorted table data? I don't want to have to grab all the data and sort it again for this script.
here is my code
<?php
include("bestbrokers_array.php");
require('simple_html_dom.php');
$table = array();
$html = file_get_html('http://www.forexfbi.com/best-forex-brokers-comparison/');
$num=1;
foreach($html->find('tr') as $row) {
if($num <= 6)
{
$rating = $row->find('td',1)->innertext;
$name = $row->find('td',0)->plaintext;
$table[$name][$rating] = true;
$num++;
}
}
html_show_array($table);
?>
and..
<?php
function do_offset($level){
$offset = ""; // offset for subarry
for ($i=1; $i<$level;$i++){
$offset = $offset . "<td></td>";
}
return $offset;
}
function show_array($array, $level, $sub){
if (is_array($array) == 1){ // check if input is an array
foreach($array as $key_val => $value) {
$offset = "";
if (is_array($value) == 1){ // array is multidimensional
echo "<tr>";
$offset = do_offset($level);
echo $offset . "<td>" . $key_val . "</td>";
show_array($value, $level+1, 1);
}
else{ // (sub)array is not multidim
if ($sub != 1){ // first entry for subarray
echo "<tr nosub>";
$offset = do_offset($level);
}
$sub = 0;
echo $offset . "<td main ".$sub." width=\"120\">" . $key_val .
"</td>";
echo "</tr>\n";
}
} //foreach $array
}
else{ // argument $array is not an array
return;
}
}
function html_show_array($array){
echo "<table cellspacing=\"0\" border=\"2\">\n";
show_array($array, 1, 0);
echo "</table>\n";
}
?>

Drupal - How to customize theme_user_list

I have a list of users, where I use theme_user_list in order to display them. The problem is that I don't want this long list with users, instead I would like to put them in three columns. I tried with theme_table but it doesn't support objects at all.
Please help me here.
Kind regards
Paste this function to template.php file of your theme:
function phptemplate_user_list($users, $title = NULL) {
if (!empty($users)) {
foreach ($users as $user) {
$items[] = theme('username', $user);
}
}
$output = "";
$output .= "<table><tr>";
for($i = 0; count($items) > $i; $i++){
$output .= "<td>" . $items[$i]. "</td>";
if($i%3 == 0 && $i != 0){
$output .= "</tr><tr>";
}
}
$output .= "</tr></table>";
return $output;
}
You will resive table with 3 columns

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;

Categories