Help with incrementing an object index in for loop (its only writing the last object in the folder)?

You could try something like that assuming that arrays are being serialized before being saved in the database and then unserialized when retrieved.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

The script below is designed to scan a folder called "widgets" for its subfolders and the . Txt files they contain. Each subfolder is a proxy for one of my theme's registed "sidebar widgets" and the .

Txt files in each folder are the "text widgets" that will be inserted into the registered sidebar represented by its parent folder. The script is working fine to place a single text widget into each registered sidebar. However, when a folder contains multiple text files, its not incrementing the widget_id value as needed and the only text widget that gets written to the sidebar is the last one in the folder.

How can I set up a proper counter for the widget_id in this case? /*Install Widgets from . Txt files in child folders of "/widgets/" -------------------------------------*/ $sidebars_widgets = get_option('sidebars_widgets'); $widget_ops = get_option('widget_text'); $widget_id = count($widget_ops)+1; $base = dirname(__FILE__).

'/widgets/'; $rdi = new RecursiveDirectoryIterator($base); foreach(new RecursiveIteratorIterator($rdi) as $files_widgets) { if ($files_widgets->isFile()) { $file_name_widget = $files_widgets->getPathname(); $sidebar_id = basename($files_widgets->getPath()); $widget_text = file_get_contents($file_name_widget); $sidebars_widgets$sidebar_id = array("text-". $widget_id); //Do I need another loop here? //Only the last widget in the folder is created $widget_ops$widget_id = array('title' => $files_widgets->getBasename('.

Txt'),'text' => $widget_text,); update_option('widget_text', $widget_ops); update_option('sidebars_widgets', $sidebars_widgets); $widget_id = $widget_id +1; } } Background: The "widgets" folder contains 5 folders, each representing a registered sidebar that my theme creates and named for the id of that sidebar. For example, this sidebar is registered in functions. Php register_sidebar(array( 'name' => 'Home Sidebar', 'id' => 'home-sidebar-widget', 'before_widget' => '', 'after_widget' => '', 'before_title' => '', 'after_title' => '', )); And the corresponding folder that contains all the widgets that will prepopulate that widget is... widgets/home-sidebar-widget/ And it contains 3 .

Txt files, each representing a text widget that should be added to that sidebar. The problem is that my counter for widget_id is not right apparently and I'm only getting the last . Txt file in each folder written to the sidebar.

Php for-loop counter link|improve this question edited Mar 23 '11 at 14:00EAMann2,0521323 asked Mar 23 '11 at 3:48Scott B2,29311645 83% accept rate.

Your code seems fine but I don't know what the update_option() function do – Sly Mar 23 '11 at 4:10 Its a built in function to WordPress. It just updates the value of widget_text and passes the array in $widget_ops to the database. The code works fine, its just overwriting each file in the folder until it reaches the last one.

That's the only one written to the database. Which makes me think I'm missing a counter or I need another loop. – Scott B Mar 23 '11 at 4:22 Well in that case I'm guessing that update_option() will update the option named widget_text in the database with the the value you pass it.

So at each iteration the same option is being updated and overwritten by the next one. Thus only the last one is kept in the DB. I suppose there is only one unique entry for the option widget_text and sidebars_widgets.

Let me know if I'm wrong – Sly Mar 23 '11 at 4:28 See my answer below and let me know if that works for you – Sly Mar 23 '11 at 4:40 @sly: thanks! Checking it out now. – Scott B Mar 23 '11 at 12:18.

You could try something like that assuming that arrays are being serialized before being saved in the database and then unserialized when retrieved. Try something like that /*Install Widgets from . Txt files in child folders of "/widgets/" -------------------------------------*/ // Remove the line below as we will fetch the value in the loop //$sidebars_widgets = get_option('sidebars_widgets'); $widget_ops = get_option('widget_text', array()); $widget_id = count($widget_ops) + 1; $base = dirname(__FILE__).

'/widgets/'; $rdi = new RecursiveDirectoryIterator($base); foreach(new RecursiveIteratorIterator($rdi) as $files_widgets) { if ($files_widgets->isFile()) { $file_name_widget = $files_widgets->getPathname(); $sidebar_id = basename($files_widgets->getPath()); $widget_text = file_get_contents($file_name_widget); // Retrieve the last value of the 'sidebars_widgets' option $sidebars_widgets = get_option('sidebars_widgets', array()); // Add the current widget to the sidebar $sidebars_widgets$sidebar_id = "text-". $widget_id; // Retrieve the last value of the 'widget_text' option $widget_ops = get_option('widget_text', array()); // Add the new widget to the list of widgets $widget_ops$widget_id = array('title' => $files_widgets->getBasename('. Txt'),'text' => $widget_text,); // Update the options with the updated arrays update_option('widget_text', $widget_ops); update_option('sidebars_widgets', $sidebars_widgets); $widget_id = $widget_id + 1; } } EDIT - Replaced array_merge() by the + operator.

Sly, I tried it both ways: straight (as is) and with your $sidebar_id suggestion. The latter throws an error (strip_tags() expects parameter 1 to be string...)...continued... – Scott B Mar 23 '11 at 12:29 ...The as is version does not error, but it does not work correctly either. What it does right, is that it does place all the widgets located in one of the folders into widgets.

However, it writes them to the wrong sidebars. It places one widget into each sidebar, but they should all be in the same sidebar (since they are located in the same folder). – Scott B Mar 23 '11 at 12:35 @Scott Yeah I understand.

Try replacing $sidebars_widgets$sidebar_id = array("text-". $widget_id); by $sidebars_widgets$sidebar_id = "text-". $widget_id;.

I have updated my answer's code accordingly. Let me know if that works. – Sly Mar 23 '11 at 16:38 @Scott I've simplified the code a bit.

Hope it helps. – Sly Mar 23 '11 at 16:46 @Sly: you da man! Checking it now.

– Scott B Mar 23 '11 at 16:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions