Jump to content

salathe

Staff Alumni
  • Posts

    1,832
  • Joined

  • Last visited

  • Days Won

    3

Everything posted by salathe

  1. First, please don't bump! Your thread is not more important than any other in this forum. Now, on to being helpful. I think you're approaching things from the wrong direction. The easiest way to get a nested array structure out of a recursive iterator like that is to build it in pieces with a simple recursive function (see below). The RecursiveIteratorIterator is designed to make it easy to move over a recursive structure, in a "flat" way, which is entirely not what you want here. Here's a recursive formatting function which takes in a RecursiveDirectoryIterator and spits out the nested array that you would like. The idea is very simple. function array_formatter(RecursiveDirectoryIterator $iterator) { $array = array(); foreach ($iterator as $fileinfo) { // Directories and files have labels $current = array( 'label' => $fileinfo->getFilename() ); // Only directories have children if ($fileinfo->isDir()) { $current['children'] = array_formatter($iterator->getChildren()); } // Append the current item to this level $array[] = $current; } return $array; } P.S. If you have any questions, thoughts, opinions on using the recursive iterators please do feel free to start other threads (and maybe ping me about them). I'm generally more than happy to go on and on about them—anything to get more folks aware of, and using, them—mostly as penance for not finishing the documentation in the PHP manual yet.
  2. You might also like to use array_rand which fetches a specified number of random keys from an array.
  3. Lets step through (the relevant bits of) your code: [*]Get first result from database [*]Output the first result [*]Determine what the background colour should be [*]Get the second result [*]Output the second result [*]Determine what the background colour should be [*]... Hopefully you can see the problem there. The solution is to simply move the few lines of code which decide what the background colour should be to a place above where you use it in the HTML.
  4. You only give $bgc a value after already outputting the first result.
  5. The main problem is that you're not echo-ing the value (see the echos for the row values).
  6. JavaScript's Date object does not accept that format of date. Either use a format that it can use, or preferably use one of either a millisecond timestamp or individual date-part arguments (new Date(year, month, date)).
  7. Aye, they're ridiculously active! It seems folks like to just post their threads in whichever forum they're currently browsing (usually the main PHP one) regardless of the actual topic, so you'll definitely find lots of "topic moved" notices around the place. Are you looking more for a place to kick around general developer-y discussions, or to help the hoards of newbies, to ask your own questions (or, all of the above)? P.S. If you're old school and like to hang around on IRC then we can accommodate you there too! It's much quieter than this place... homely, would be the word.
  8. Hi Dan, welcome to PHPFreaks. Kick back and make yourself at home here... at least for a little whaile.
  9. You'll have to find some way of reading what the response, if any, was. file_get_contents() only returns the body so you'll have to figure out a way of accessing the headers (to determine whether the HTTP status was 999). The PHP manual page for file_get_contents() is a good start.
  10. It can take a while to get used to, do come back if you have trouble.
  11. There are a number of different things that you could do. An idea would be to split apart the different arrays (e.g. one containing just _site items, another containing just _id items, and the left-overs), sort those individually and then put them all back into one big array in whichever order you want. Another idea is to use the existing array sorting functions which allow you to provide a custom comparison function to determine what gets sorted "higher" than other values. To give you an example, this sorts your array alphabetically by filepath but pushes the _site items to the top (also in alphabetical order). uasort($array, 'sorter'); print_r($array); function sorter($a, $b) { $a_site = strpos($a['filepath'], '_site') !== FALSE; $b_site = strpos($b['filepath'], '_site') !== FALSE; if ($a_site && !$b_site) { // Only $a is site, move $a up return -1; } elseif (!$a_site && $b_site) { // Only $b is site, move $a down return 1; } else { // Both site, or both not site, compare alphabetically return strcmp($a['filepath'], $b['filepath']); } } The basic idea of a comparison function like that is to return less than zero (can be anything but is traditionally -1) if the first value is less than the second value (think: it is "lighter" so floats towards the top), zero if they are both considered the same, and greater than zero otherwise.
  12. Or, read the message since both options for resolving the cause of the warning (and the reason for it) are very clearly stated. Use that function to set a default timezone at runtime, or use the date.timezone INI setting (in httpd.conf, php.ini, a user.ini, .htaccess or via ini_set()).
  13. How do you know that 4339a108-f907-4661-9aab-d6f3f00e736e means the street? Did you have a look at SimpleXML yet?
  14. Have you looked at SimpleXML? If you're having trouble, please let us know precisely what you want to do with the $xml string.
  15. dreamwest, and anyone else stumbling on this thread, please see http://php.net/regexp.reference.anchors for details on using "anchors" with your regular expressions.
  16. include() will evaluate the file as PHP (if there is no PHP in there, then it will just be outputted). To retrieve the contents of the file, the file_get_contents function should be used.
  17. Hello.
  18. Thorpe, it would be entirely possible. However, the changes to the language, and performance impact of such changes, would make using the dot operator out of the question. Good luck trying to work out whether the aim is to concatenate constants or use some namespaced item! Nightslyr, if your main problem is the syntax being "ugly" then I'd say that's a big win for the implementation! Personally, I quite like the backslash as namespace separator. TomTees, as thorpe pointed out, you can use namespaces as of PHP 5.3.0 much like you would with your example in the first post. The keyword to look for, rather than import is use.
  19. No. Yes. No. I'd say there are more Wordpress installs than forums. And many, many other types of uses which are more prolific than forums. Sure, why not. HTML, JavaScript and CSS.
  20. See http://www.phpfreaks.com/forums/php-coding-help/'can-you-do-*insert-thing*'/
  21. This is a pretty common problem, particularly recently: any chance something could be put in place so that it doesn't remain an issue? (If something hasn't already been done.)
  22. No-one said it doesn't (it does).
  23. You were nearly there. \f means something special when within a double-quoted string, just remove that slash and it would have worked. For what it's worth, you also (obviously, after JAY6390's post) need any of the parentheses nor a slash before the equals character. It would also be good to "anchor" the expression so that the | can only be matched at the start of a line. Also, read up on the m pattern modifier that JAY6390 introduced and see if you think it is necessary or not.
  24. It looks like your error reporting level was E_ALL & ~E_DEPRECATED --- this level does not include E_STRICT (since it is not part of E_ALL). For what it's worth, the manual page for error_reporting() says (in a big "Tip" box):
  25. You're probably missing something, mistakes in the manual do happen but it doesn't look to be the case this time. Make sure that error reporting is turned on (the simplest way is to put error_reporting(-1); ini_set('display_errors', true); at the top of your script) when you're trying to figure out problems with code.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.