Hi all,
Ugh. I'm not a programmer by any means, but I've found myself needing to customize one of the modules, and I'm having a hard time controlling a 'foreach' loop using the node arrays. The module I'm working reads the contents of a folder and builds an array containing the names of the image files in a folder. I'm trying to build 2 'foreach' statements, one which counts the number of files and writes a line of html for each item, and the second loop outputs a few lines of html for each image in the array and inserts the image filename into placeholders in that html.
I've installed the devel module, so I can see the variable/array dump for my node, but can't figure out how to properly get the image count and names. I guess what I'm looking for is an understanding of what to put in the 'foreach' brackets in order to loop through the variables in the [images] array (??)
This is the code generated by the devel module:
content
Array
(
[body] => Array
(
[#value] => </p>
[#weight] => 0
)
[eti_beef] => Array
(
[#type] => eti_beef
[#attributes] => Array
(
[class] => eti_beef align-full location-above
[id] => eti_beef-4
)
[#eti_beef] => Array
(
[settings] => Array
(
[eti_beef-4] => Array
(
[images] => Array
(
[1] => Array
(
[src] => http://myservername.com/files/eti_beef/beef_image01.gif
[title] => beef_image01.gif
)
[2] => Array
(
[src] => http://myservername.com/files/eti_beef/beef_image02.gif
[title] => beef_image02.gif
)
)
[total] => 2
[current] => 1
)
)
[status] => Array
(
[current] => 1
[total] => 2
[previous] => 2
[next] => 2
)
[url] => node/4
[title] => beef_image01.gif
[image] => http://myservername.com/files/eti_beef/beef_image01.gif
)
[#weight] => -5
)
)
For example, here is the first loop (I know the $eti_beef[images'] part is wrong, but I don't know what to do to loop through the [images] variables:
// start loop
foreach ( $eti_beef['images'] as $imagename) {
$key = $key + 1;
if ($key < 10) { $key = "0".$key; }
$output .= "<li><a class='navi' href='#'><span>".$key." - Image name:".$imagename."</span></a></li>\n";
}
$key = 0;
// end loop
I think it's probably a simple solution, and I just don't have the PHP knowlege to figure it out... Any help would be appreciated.
Tx!
Ira
Comments
Whats missing from your question is how you get the filenames
So this example is more of a starting point that a full blow solution
Thanks!
Thanks for the quick response. Your code is very clear, and I see how it would work (I'm learing. slowly.). To clarify a bit, the existing module that I'm modifying is the Slideshow module, so the code to read the folder contents and generate the arrays already exists. I think my main concern is how to reference the existing values (as outputed in the devel results above) within the existing [images] array, within a completely seperate function in the code.
I've been experimenting a bit, and for the first part, the following code seems to be working for me:
which outputs the following (which is what I wanted, but could just be a coincidence!):
Lovely. However, the second 'foreach' call within that function isn't quite working. The code is:
which returns:
I know it's wrooooong, but can't find a way to loop through the [images] array in order to loop through the [title] values....
Anyhoo, a long shot, but maybe you might see what I'm trying to do and know what syntax to use in the foreach loop.
Thanks again for your help and patience.
Ira
Why foreach loop is not working
When you have a foreach loop like this
the
as $image => $valuetells it to place the array index in the first part ($image in this case) and the array element in the second part ($value in this case). Since you do not seem to need the index you could just usewhich walks through each element of the array.
A bigger issue, you loop is not really dealing with filename (images) as you are constructing you own array
array($eti_beef['settings'], 'setting')which has two elements, the value of $eti_beef['settings'] and the constant 'setting'. This is also why your first loop outputs two elements.As for getting the titles, with out a better understanding of what you are modify that would be hard. I looked at slideshow.module and it does not contain a variable $eti_beef so I am guessing this is one you added. If you can provide a function and linenumber for were you are making the changes I can possibly help.
Yup, I figured that the two
Yup, I figured that the two elements returned in the first part was just a coincindence!
Because I'm working with a copy of the Slideshow module, i renamed the copy (and all references within the code) from 'Slideshow/slideshow/SLIDESHOW' to 'Eti_beef/eti_beef/ETI_BEEF', to make sure that the two modules weren't conflicting in any way.
The part of the Slideshow code that I'm trying to modify is the last bit, starting at line 558 ("function theme_slideshow($element)...."). Rather than have it display just one slide at a time, with the next/current/previous links, I'd like it to loop through all of the attached images and output all the divs at once.
Here's the code that I've replaced from 558 on:
I know that "$eti_beef['settings']), 'setting'" is not correct. The only reason I used it is because line 564 has the following line:
which seems to write the image filenames to the javascript section in the template's head section, but I don't get how it works.
Dunno, maybe I should scrap the idea of reusing the Slideshow module and try to make one from scratch.
Ira
Ok, that helps, this should get you going
The variable $slideshow/$eti_beef contains an element called 'settings' which is also an array and one of it's elements is called 'images' which is also an array which contains the information you want. So looking at just the loops, change the first one like this
Note the loop has been changed to use the key value set when building the array of images.
For the second loop the change would look something like
If you want to use the title in the loop it is available as
$image['title']Much abliged! The code
Much abliged! The code makes sense, but it still isn't working for me. :(
Drupal is spitting out the following errors from both of the 'foreach' lines:
"warning: Invalid argument supplied for foreach() in ..."
I've tried adding echo ($image_set); before starting the loop, at it displays as empty as well.
If you have any other ideas, please let me know. tx!!
Ira
Woohooo!!
Oh man. Ok, I looked at the Devel output again, and my structure is actually [settings]['eti_beef-4']['images'].
So I changed the line to read:
and it works beautifully! Thanks again.
Ira
Question on code
I'm new to PHP and know the basics. I pretty well understand the entire issue being discussed, but what does this line do in your examples?
$key = '0' . $count;
I see it setting $key equal to '0' but what does the . $count add to it? Does that append the value of $count to the zero?
Thanks in advance!
In the context of the logic it makes all keys two digits
In the context of the logic it makes all keys two digits (assuming we never reach 100). So '1' becomes '01', 2 becomes '02', etc.
Tracking
I woulds like to keep track of this node!
I'm writing a module to list .asm and .cob source code files.
This code is a lot better than mine.
Thanks
Thank you!
Thank you nevets. Your well written code made my day :)