Reputation: 81
I am developing an application using php and the dropbox api and am trying to loop through a multidimensional array and output to a table. This is my code so far:
<?php
session_start();
# Include the Dropbox SDK libraries
require_once "Dropbox/autoload.php";
use \Dropbox as dbx;
// Create connection
$con = mysqli_connect(
"localhost", "sintegra_aggre", "*******", "sintegra_aggregator"
);
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_SESSION['uid'])) {
$password = $_SESSION['pass'];
$username = $_SESSION['user'];
$email = mysqli_query($con, "
SELECT *
FROM main_users
WHERE
password='$password'
AND username='$username'
");
if ($email >= 1) {
// let them stay on page
} else {
header("Location: logout.php");
}
} else {
header("Location: login.php");
}
$accessToken = $_SESSION['accessToken'];
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");
$folderMetadata = $dbxClient->getMetadataWithChildren("/upload");
foreach ($folderMetadata as $value) {
echo $value;
foreach ($value as $val) {
echo $val . "<br />";
}
echo "<br /><br />";
}
?>
and this is the output from the print_r of the array:
Array
(
[hash] => d023a1738d460f667d383cb4f57bc769
[revision] => 65
[rev] => 411389e826
[thumb_exists] =>
[bytes] => 0
[modified] => Wed, 28 Aug 2013 20:28:34 +0000
[path] => /upload
[is_dir] => 1
[icon] => folder
[root] => app_folder
[contents] => Array
(
[0] => Array
(
[revision] => 81
[rev] => 511389e826
[thumb_exists] => 1
[bytes] => 1996564
[modified] => Wed, 28 Aug 2013 21:32:10 +0000
[client_mtime] => Wed, 28 Aug 2013 21:32:11 +0000
[path] => /upload/08-nigellas-chocolate-chip-muffins.jpg
[is_dir] =>
[icon] => page_white_picture
[root] => dropbox
[mime_type] => image/jpeg
[size] => 1.9 MB
)
[1] => Array
(
[revision] => 79
[rev] => 4f1389e826
[thumb_exists] => 1
[bytes] => 22848
[modified] => Wed, 28 Aug 2013 21:14:39 +0000
[client_mtime] => Wed, 28 Aug 2013 21:14:39 +0000
[path] => /upload/1376243030_guestion.png
[is_dir] =>
[icon] => page_white_picture
[root] => dropbox
[mime_type] => image/png
[size] => 22.3 KB
)
[2] => Array
(
[revision] => 80
[rev] => 501389e826
[thumb_exists] =>
[bytes] => 54772
[modified] => Wed, 28 Aug 2013 21:26:19 +0000
[client_mtime] => Wed, 28 Aug 2013 21:26:19 +0000
[path] => /upload/BT_screen_quiz.java
[is_dir] =>
[icon] => page_white_cup
[root] => dropbox
[mime_type] => text/x-java
[size] => 53.5 KB
)
[3] => Array
(
[revision] => 77
[rev] => 4d1389e826
[thumb_exists] =>
[bytes] => 1679
[modified] => Wed, 28 Aug 2013 20:59:53 +0000
[client_mtime] => Wed, 28 Aug 2013 20:59:53 +0000
[path] => /upload/login.php
[is_dir] =>
[icon] => page_white_php
[root] => dropbox
[mime_type] => text/php
[size] => 1.6 KB
)
[4] => Array
(
[revision] => 78
[rev] => 4e1389e826
[thumb_exists] =>
[bytes] => 2037
[modified] => Wed, 28 Aug 2013 21:00:56 +0000
[client_mtime] => Wed, 28 Aug 2013 21:00:56 +0000
[path] => /upload/signup.php
[is_dir] =>
[icon] => page_white_php
[root] => dropbox
[mime_type] => text/php
[size] => 2 KB
)
)
[size] => 0 bytes
)
I have tried a combination of different methods from posts such as:
and none of them have worked.
I was hoping someone would be able to give me a bit of code that will loop through the array and output it into a table. It is also only the contents array that needs to be put into the table
Thanks in advance, Marcus
Upvotes: 3
Views: 1879
Reputation: 1962
A little bit of inline and block php will do the basics. The rest of the formatting is up to you ;D
All foreach have been done with the values as references in an effort to speed things up.
Revised Answer
Based on new information, since you want it laid out with the headings at the top you'd want to try something like this:
<?php $headings = array_keys($array['contents'][0]); ?>
<table>
<tr>
<?php foreach( $headings as &$heading ): ?>
<th><?php echo $heading; ?></th>
<?php endforeach; ?>
</tr>
<?php foreach( $array['contents'] as &$file ): ?>
<tr>
<?php foreach( $file as &$data ): ?>
<td><?php echo $data; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
First line grabs the array_keys as headings, prints out the heading rows and then goes through the data rows directly from the array by reference.
Previous Revision
This was my last answer, it displays the table with the headings on the left, with the data displayed in columns.
<?php
$output = array();
foreach( $test['contents'] as &$file )
{
foreach( $file as $heading => &$value )
{
$output[$heading][] = $value;
}
}
?>
<table>
<?php foreach( $output as $heading => &$data): ?>
<tr>
<th><?php echo $heading; ?></th>
<?php foreach( $data as &$value ): ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
Because of the way tables are laid out, you can just use a little bit of shuffling to get an array to iterate correctly. There's probably a good thousand different ways to write this though.
Upvotes: 1