Extracting columns from sql table with php - php

My query selects all columns from my table
$spool = $wpdb->get_results('SELECT * FROM `tablename`');
then I display the results in a table. I need to display all columns. This is the way I do it.
echo "<table>";
if ( !empty( $spool ) ) {
echo "<tr><th> header </th></tr>";
foreach ( $spool as $a ) {
echo "<tr><th>" . $a->columnname1 . "</th><th>" .$a->columnnameN . "</th></tr>";
}
}
echo "</table>";
Now since I have around 40 columns, I'd like to ask if there is a more intelligent and less tedious way of displaying them.

probably you need nested loops, you are getting result probably like this
array(
0:{column1:test,column2:test . . . },
1:{column1:test,column2:test . . . }
)
so you can try this way
echo "<table>";
if ( !empty( $spool ) ) {
echo "<tr><th> header </th></tr>";
foreach ( $spool as $key => $value ) {
echo '<tr>';
foreach ( $value as $a ) {
echo "<th>" . $a. "</th>";
}
echo '</tr>';
}
}
echo "</table>";

As you have objects you could cast them to arrays and use implode.
(But attribute order could be different from what you want.)
<?php
class A
{
public $foo = 'bing';
public $bar = 'bang';
public $baz = 'bong';
}
$spool = [new A, new A];
echo '<table>';
foreach($spool as $a) {
echo '<tr>';
echo '<td>' . implode('</td><td>', (array) $a) . '</td>';
echo '</tr>';
}
echo '</table>';
Output:
<table><tr><td>bing</td><td>bang</td><td>bong</td></tr><tr><td>bing</td><td>bang</td><td>bong</td></tr></table>

Related

Create a bidimensional array in php

I confess that I am a beginner. My goal is to be able to summarize in a two-dimensional table the amount per month (in column) for each class (in line). After doing a lot of research, I arrived at this stage in my project.
My problem is , I get the result with months repeating in columns like here.
Here is my code.
This is my sql code : "SELECT classe, mois, sum_ec FROM journal"
This is my php code :
<?php
$tableau = array();
$tblClasse = array();
$rt = mysqli_query($db, $req_sit);//execute la requete
while ($row = $rt->fetch_assoc()){ //forme le tableau
$tableau[$row['classe']][$row['mois']] = $row['sum_ec'];
if (!in_array($row['classe'],$tblClasse)) {
$tblClasse[] = $row['mois'];
}
}
echo '<table border="1">
<tr>
<th> </th>';
foreach ($tblClasse as $classe) {
echo '<th>' . htmlspecialchars($classe) . '</th>';
}
echo '</tr>';
foreach ($tableau as $mois=>$value) {
echo '<tr>';
$new_line = TRUE;
foreach ($tblClasse as $classe) {
if ($new_line) {
echo '<td>' . $mois . '</td>';
$new_line = FALSE;
}
$display = isset($value[$classe]) ? $value[$classe] : " ";
echo '<td>' . $display . '</td>';
}
echo '</tr>';
}
?>
Can someone tell me what mistake I made?
The problem is in this part of your code:
if (!in_array($row['classe'],$tblClasse)) {
$tblClasse[] = $row['mois'];
}
You are adding months (Fev, Mar) to the array and trying to check if it contains classes (PS, MS etc.) which is impossible, and this condition always returns true, so Feb is added to the $tblClasse array at each iteration. You must either check months or classes and add months or classes to the array respectively. And if you want the array to contain unique values (for example, months), use keys instead of values:
if (!array_key_exists ($row['mois'],$tblClasse)) {
$tblClasse[$row['mois']] = 1;
}
Where 1 can be replaced with any other value.
In the further part of your code use the keys, not values of $tblClasse array to form the table heading.
foreach (array_keys ($tblClasse) as $mois) {
echo '<th>' . htmlspecialchars($mois) . '</th>';
}
Eureka! It worked. I just had to change the line for the values ​​to "array_keys"
foreach (array_keys($tblClasse) as $classe) {
if ($new_line) {
echo '<td>' . $mois . '</td>';
$new_line = FALSE;
}
as well and boom! It's perfect. Thank you very much my dear. You have given me a lot of happiness.

How do I display the data from the last to the first with a multidimensional array in php

So I'm new to coding and I was looking at arrays and I want to know if it's possible to display the last piece of data from an array first and the first piece of data to be last. This is the code I have.
<?php
$trans = array
(
array("10/3/22",1,54),
array("10/48/32", 54,54),
array("3/29/2018", 54, 128.84)
);
echo '<table border="1">';
echo '<tr><th>Date</th><th>Before Balance</th><th>After Balance</th></tr>';
foreach ($trans as $tran) {
echo '<tr>';
foreach( $tran as $data ) {
echo '<td>' . $data . '</td>';
}
echo '</tr>';
}
echo '</table>';
Use
array_reverse($array);
For more info http://php.net/manual/en/function.array-reverse.php
use array_reverse as follows in your forloop
foreach (array_reverse($trans) as $tran) {
echo '<tr>';
foreach( $tran as $data ) {
echo '<td>' . $data . '</td>';
}
echo '</tr>';
}

Display sql tables in separate html table columns

I'm trying to display data from different mysql tables in an html table. The problem I have is that it currently displays both in the same column. I'd like to have two separate columns in the table one for each of the sql data.
This is my code:
global $wpdb;
$centr = $wpdb->get_results('SELECT meta_value FROM `wp__frm_item_metas` WHERE `item_id` = 2');
$namelist = $wpdb->get_results('SELECT name FROM `wp__frm_fields` WHERE form_id = 2');
echo "<table>";
if ( !empty( $centr ) ) {
foreach ($namelist as $key) {
# code...
echo "<tr><td>" . $key->name . "</tr></td>";
}
foreach ( $centr as $r ) {
echo"<tr><td>" . $r->meta_value . "</tr></td>";
}
}
echo "</table>";
I'd like the second echo to start in a new column, instead of displaying more rows in the same one.
I guess this is what you want
echo "<table>";
if ( !empty( $centr ) ) {
echo "<tr><td>";
foreach ($namelist as $key) {
echo "<table><tr><td>" . $key->name . "</td></tr></table>";
}
echo "</td><td>";
foreach ( $centr as $r ) {
echo"<table><tr><td>" . $r->meta_value . "</td></tr></table>";
}
echo "</td></tr>";
}
echo "</table>";

New line after multiple columns

Hi all i'm not very experienced with programming so this is probably easy to achieve.
I am pulling data from a mysql table using php i want to display the output like so:
$row[1] $row[2] Line break
$row[3] $row[4] Line break
$row[5] $row[6] Line break
And so on
Any help would be appreciated thanks
$user_table = select_table( "users", "user_name ASC" );
$rows = array( );
while( $row = mysqli_fetch_assoc( $user_table ) ) {
$rows[] = $row;
}
$user_groups = array_chunk( $rows, 2 );
foreach ( $user_groups as $user_group ) {
echo "<tr>";
foreach( $user_group as $row ) {
echo "<td class=\"user-box\">{$row["user_name"]}</td>";
}
echo "</tr>";
}
This is my working code. Thanks Tim
Loop through your results and start counting again after 2.
here's an example using a simple array and a table so you can see how it works
<?php
$results = array('one','two','three','four');
echo '<table border="1">';
foreach(array_chunk($results,2) as $row) {
echo '<tr>';
foreach($row as $value) {
echo '<td>'.$value.'</td>';
}
echo '</tr>';
}
echo '</table>';

Output each mongodb record to an html table

I'm building an app which pulls records from a MongoDB. I've built the thead>tr>th as follows:
// building table head with keys
$cursor = $collection->find();
$array = iterator_to_array($cursor);
$keys = array();
foreach ($array as $k => $v) {
foreach ($v as $a => $b) {
$keys[] = $a;
}
}
$keys = array_values(array_unique($keys));
// assuming first key is MongoID so skipping it
foreach (array_slice($keys,1) as $key => $value) {
echo "<th>" . $value . "</th>";
}
This gives me:
<thead>
<tr>
<th>name</th>
<th>address</th>
<th>city</th>
</tr>
</thead>
This works very well, it grabs all the keys and builds the table head. I don't have to specify anything and the thead is built dynamically from the data. The part I'm unable to figure out is building all of the tr>td's
I can easily grab the info and build it like this:
$cursor = $collection->find();
$cursor_count = $cursor->count();
foreach ($cursor as $venue) {
echo "<tr>";
echo "<td>" . $venue['name'] . "</td>";
echo "<td>" . $venue['address'] . "</td>";
echo "<td>" . $venue['city'] . "</td>";
echo "</tr>";
}
Doing so requires me to modify my php every time I add a new field. How can I build the tr>td's automagically based on the data from mongodb like I am with the thead?
My data looks like this:
{
"name": "Some Venue",
"address": "1234 Anywhere Dr.",
"city": "Some City"
}
Have you try to use second foreach as below
$cursor = $collection->find();
$cursor_count = $cursor->count();
foreach ($cursor as $venue) {
echo "<tr>";
foreach (array_slice($keys,1) as $key => $value) {
echo "<td>" . $venue[$value] . "</td>";
}
echo "</tr>";
}

Categories