3

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

3
  • How were you expecting your table to layout? One table for the folder information, another following it with the content information? Commented Aug 28, 2013 at 23:32
  • Hi, I was thinking that the left coloumn could have the titles for each file such as bytes, path etc and the right the value. Commented Aug 28, 2013 at 23:33
  • also, I have just realized I forgot to mention that I do not need the folder information (I have now edited the original post) Commented Aug 28, 2013 at 23:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hi, thanks for this and it is all working perfectly so far, this is my fault, but I was trying to get the file names on the left and the headers on-top, and I think that means the code has to be changed around to go from left to right to going from top to bottom. If possible, please could you edit your answer as I would have no idea how to start to go about doing this.
Check the updated answer, this displays the table with the headings at the top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.