MarkT Posted August 4, 2014 Share Posted August 4, 2014 Hi, How do I use the PHP command scandir I want to scan the directory and then display them in a table, If anyone could explain it in a way that enables it to be displayed similar to an SQL query return, so $query['filename'] or similar so that I can put it into a table with other details. Thank Link to comment Share on other sites More sharing options...
niche Posted August 4, 2014 Share Posted August 4, 2014 (edited) write a little code and send it to us when you have questions. What you need to do is pretty basic. http://php.net/manual/en/function.scandir.php Edited August 4, 2014 by niche Link to comment Share on other sites More sharing options...
MarkT Posted August 4, 2014 Author Share Posted August 4, 2014 Array( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir) Thats what PHP.net says the result is. But I don't know how to go about fetching the results in the format I want. Heres what I want it to be similar to <table class="table table-bordered table-striped table-highlight"> <thead> <tr> <th>#</th> <th>Date</th> <th>Title</th> <th>Opponent</th> <th>Score</th> <?PHP if($de2['team'] == $_SESSION['team']) {echo "<th>Actions</th>";} ?> </tr> </thead> <tbody> <?PHP while($se2 = mysqli_fetch_array($search)) { echo " <tr> <td>" . $se2['id'] . "</td> <td>" . $se2['date'] . "</td> <td>" . $se2['title'] . "</td> <td>" . $se2['against'] . "</td> <td>" . $se2['score'] . "</td>"; if($de2['team'] == $_SESSION['team']) {echo " <td><a href='edit-report.php?pid=" . $se2['id'] . "' class='btn btn-small btn-tertiary'> <i class='btn-icon-only icon-edit'></i> </a> <a href='manage-reports.php?action=dele&pid=" . $se2['id'] . "&team=" . $se2['team'] . "' class='btn btn-small'> <i class='btn-icon-only icon-remove'></i> </a></td>";} echo " </tr> ";} ?> </tbody> </table> Thats what I use at the moment, with the result being $se2['field'] But I don't know how to get the array in that format so that I can use a similar table. Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted August 4, 2014 Share Posted August 4, 2014 Ditch scandir and use the SPL. Use a DirectoryIterator instead:http://www.php.net/manual/en/class.directoryiterator.phpNote it is only an iterator. If you want the results to be sorted then you'll need to put them all in an array, and then sort them. Like the name implies, DirectoryIterator has the iterator interface, so you can use it in a foreach loop like an array, check the example:http://www.php.net/manual/en/directoryiterator.construct.phpEach item that it iterates through is a SplFileInfo object, which you can see here:http://www.php.net/manual/en/class.splfileinfo.php Link to comment Share on other sites More sharing options...
MarkT Posted August 5, 2014 Author Share Posted August 5, 2014 I think I would prefer to use scandir, it looks simpler and more what I would normally use. Could you give me an example of the code that I could implement into my table code shown above. Thanks Link to comment Share on other sites More sharing options...
niche Posted August 5, 2014 Share Posted August 5, 2014 Start with the table. How would you render a table in php? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 5, 2014 Share Posted August 5, 2014 I think I would prefer to use scandir, it looks simpler and more what I would normally use.Well, ok then. Scandir returns an array of filenames, that's the only thing it will return. It sounds like you don't want to use that though. A DirectoryIterator will give SplFileInfo objects instead of just a string filename. Obviously you can do more with an object than you can with a string. The SplFileInfo class wraps most of the filesystem functions that you would use with individual files, as you can see in the manual. Much more flexible than returning an array of filenames, and it sounds like you're looking for flexibility. If you want an array of filenames though, then you can use the rest of the filesystem functions to get information about each file.http://www.php.net/manual/en/book.filesystem.php Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now