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>";
}
Related
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>';
}
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>
Ive got a pretty basic table named 'customers' with four columns:
ID (primary Auto Increment)
businessName
contactName
contactEmail
I call it with:
$result = mysqli_query($con, "SELECT * FROM customers");
and was using mysqli_fetch_array to display it on the page with a foreach loop:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
Which gives you an array like:
Array ( [id] => 1 [businessName] => Microsoft [contactName] => Bill Gates [contactEmail] => bill#microsoft.com ) Array ( [id] => 2 [businessName] => Amazon [contactName] => Jeff Bezos [contactEmail] => jeff#amazon.com )
Is it possible to display results differently based on which column (technically now position in the array) they are in? I would like to make a
a href="mailto:bill#microsoft.com"
link when it gets to contactEmail.
What if I wanted to display just one key or value instead of using foreach?
It doesn't seem possible to call
$row['contactEmail']
in the foreach loop
which confuses me since below that I am able to create a link to
$row['id']
If anyone has any ideas how to do this, or if there is a better way to be displaying this information, I'm open to suggestions, as I'm not very good at programming, especially PHP.
Yes! you can just add like this:
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
foreach ($row as $key => $value) {
if($key == "contactEmail"){
$mailUrl = "mailto:".$value;
echo "<td>".$value."";
}
else{
echo "<td>" . $value . "</td>";
}
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
Yes, You can able to mysqli_fetch_array retrieve data directly using foreach loop. like bellow :
<?php
$result = mysqli_query($con, "SELECT * FROM customers");
?>
<table>
<tr>
<td>Business Name</td>
<td>Contact Name</td>
<td>#</td>
</tr>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo "<tr>";
echo "<td>".$row['businessName']."</td>";
foreach ($row as $key => $value) {
$newValue = $key == "contactEmail" ? ''.$value.'' : $value;
echo "<td>" . $newValue . "</td>";
}
echo "<td><a href='updateform.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "</tr>";
}
?>
</table>
I have this code:
$sql = 'SELECT * from page';
$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
if(count($result)) {
echo '<table><tr>';
foreach ($rows[0] as $columnName => $value) {
echo '<th>' . $columnName . '</th>';
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '<tr>';
}
echo '</table>';
}
This code is working fine. But since my table is huge, it is appearing to be very very clumsy - almost unreadable. And I don't know how to make it appear better. I tried adding spaces and tabs but to no use. I can't understand how to do it. I got this code from my friend. Can anyone modify the code so as to add at least a tab space between every column. Help would be really appreciated.
You can for example do the following:
instead of
echo '<table><tr>';
use
echo '<table border="1"><tr>';
It will put border on your table and it will be easier to differentiate between cells
I am trying to sort an associative array in ascending in order and then transfer it to a HTML table and I am currently stumped at an error. I looked for guidelines here on SO and followed instructions on some posts:
PHP display associative array in HTML table
But still no luck, here is my attempt:
<?php
function format($g){
array_multisort($g, SORT_ASC);
echo "<table>";
foreach($g as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
$bib = array("Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40");
format($bib);
?>
My debugger is telling me there is an error at my for each loop but I don't see how it is wrong unless there is some syntax error that I am not seeing? The error is saying Invalid argument supplied for foreach()
Because your $bib is only single array but you use two foreach to loop this array
At 2nd loop, your $row variable is a string, you can't use foreach for this type
Can you try that for single array ?
<?php
function format($data) {
array_multisort($data, SORT_ASC);
echo "<table>";
foreach($data as $k => $v) {
echo "<tr>";
echo "<td>$k</td>";
echo "<td>$v</td>";
echo "</tr>";
}
echo "</table>";
}
$bib = array("Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40");
format($bib);
?>
$k is Luke John Matt and Mark,
$v is 10 30 20 and 40
You can see the foreach example here: http://php.net/manual/en/control-structures.foreach.php
Hope this helpful ^^
You can try this
<?php
function format($data){
array_multisort($data, SORT_ASC);
echo "<table>";
foreach($data as $key => $row) {
echo "<tr>";
echo "<td>" . $key . "</td>";
echo "<td>" . $row . "</td>";
echo "</tr>";
}
echo "</table>";
}
$bib = array(
"Luke"=>"10",
"John"=>"30",
"Matt"=>"20",
"Mark"=>"40"
);
format($bib);
?>