473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

foreach loop

anfetienne
424 256MB
is it possible to have more than 1 array on a foreach loop?
Mar 18 '09 #1
19 1494
TheServant
1,168 Expert 1GB
What do you mean? Post a sample code so we can see what you're trying to do.
Mar 18 '09 #2
yes, by adding array from while loop
Mar 19 '09 #3
Try to use while loop for getting array
Mar 19 '09 #4
anfetienne
424 256MB
how would i use the while loop to add the extra array to a foreach loop?

at 1st i thought id have to to the array_combine.....that way i can make an extra key and value using the 2 arrays i need. But i still can't figure how to get the extra item into the foreach....once i combined my arrays i tried this

$details = array_combine($_POST['photoT'], $_POST['captionT']);

// Loop through the POST items
foreach ( (($_POST['picT'] as $picT => $picStart)) && ($details as $photoName => $picCaption)) )

{

$nameCapsA = $xmlobj->addChild($picStart);
$nameCapsA->addAttribute("name", $photoName);
$nameCapsA->addAttribute("caption", $picCaption);

}
Mar 19 '09 #5
Markus
6,050 Expert 4TB
You can only use 1 array as a foreach() loop's argument.

Am I right in thinking the amount of files you upload will be the same as the amount of captions you have?

The reason I suggested you do it all in one loop, is because (if you are having the same number of items -- above) you could use the array key given from the foreach loop to access the same index of a different array.

Consider this:

Expand|Select|Wrap|Line Numbers
  1. $array_1 = array ( 1, 2, 3, 4, 5, 6 );
  2. $array_2 = array ( "one", "two", "three", "four", "five", "six" );
  3.  
  4. foreach ( $array_1 as $key => $value )
  5. {
  6.     echo "{$value} = {$array_2[$key]}<br />";
  7. }
  8. // Produces:
  9. // 1 = one
  10. // 2 = two
  11. // ... etc.
  12.  
Mar 19 '09 #6
anfetienne
424 256MB
yes you are right in suggesting that but as i have three arrays it's not that easy......ahhhhh but?

what if i have a while loop to generate one and and the use a foreach loop within the while loop for the $key & $value...would that work better? I will do a test and post my code here
Mar 19 '09 #7
anfetienne
424 256MB
also please bare in mind this is being used towards simpleXML.....it doesn't always take php coding as it should once output.......such a pain
Mar 19 '09 #8
Markus
6,050 Expert 4TB
Do you mean you have a multi-dimensional array, or 3 seperate arrays?

Can I please see your upload form; I can't make sense of what's happening here.
Mar 19 '09 #9
anfetienne
424 256MB
the form fields are generated with php, below is what is generated. I'm trying to make an array that can be used within the foreach....i dont really know how many arrays I need but i have 3 picT, photoT, captionT.....i've named the fields picT[] photoT[] and captionT[] as to use them as an array when i generate the xml file



code....
Expand|Select|Wrap|Line Numbers
  1.             echo '<td><center><p><a href="'.$img_path .'"><img src= "'.$path.$item[$n] .'" height="100" width="100"></a></p></center>';
  2.             echo '<center><p><input type="hidden" name="picT[]" value="pic'.$pn++.'"/></p></center>';
  3.             echo '<center><p><input type="hidden" name="photoT[]" value="PHOTO '.$pto++.'"/></p></center>';
  4.             echo '<center><p><input type="text" name="captionT[]" value=""/></p></center>';
  5.             echo "<center><p><a href=imgEdit.php?img=$img_path> > Edit Image < </a></p></center><br></td>";
  6.  
Mar 19 '09 #10
anfetienne
424 256MB
for some reason simpleXML wouldn't read the values that were brought in normally through post thus why i've had to use the foreach loop......

if i can't have more than 1 array within the foreach can i combine the 3 so i can use the within the loop?
Mar 19 '09 #11
Markus
6,050 Expert 4TB
Like I said before, you can loop through one array using foreach() loop, and then use the key given to you, to access the same index in the other arrays.

Expand|Select|Wrap|Line Numbers
  1.  
  2. foreach ( $_POST['picT'] as $key => $value )
  3. {
  4.     // use $key to access the other elements.
  5.     echo $value . "<br />";
  6.     echo $_POST['photoT'][$key] . "<br />";
  7.     echo $_POST['captionT'][$key] . "<br />";
  8.  
  9.     // Do something with your xml object
  10.     $child = $xml->addChild( $value );
  11.     $child->addAttribute( 'name', $_POST['photoT'][$key] );
  12.     $child->addAttribute( 'caption', $_POST['captionT'][key] );
  13. }
  14.  
  15.  
Mar 19 '09 #12
anfetienne
424 256MB
ahhhhhh i definately see what you are saying now markus.......thanks for that I am going to test this, write it down and practice it so i understand it more.

Thanks again
Mar 19 '09 #13
anfetienne
424 256MB
here is the result......


pic1
PHOTO 1
test01

Notice: Undefined variable: xml in /home/vecre0/public_html/test/8/xmltest.php on line 21

Fatal error: Call to a member function addChild() on a non-object in /home/vecre0/public_html/test/8/xmltest.php on line 21
Mar 19 '09 #14
Markus
6,050 Expert 4TB
Yeah, you're going to have to use your xml object - the one you've shown in previous threads.
Mar 19 '09 #15
Markus
6,050 Expert 4TB
I should mention that your arrays aren't necessarily going to be the same length, some may be shorter, some may be longer. In an ideal world, we would be fine assuming they are the same, but, alas, we aren't, so you should incorporate some checks into your code to prevent any errors.

- Mark.
Mar 20 '09 #16
anfetienne
424 256MB
hi markus.....ive tested it with the different lengths of values stored in the arrays varying from 1 character to 250 characters and i get no errors at all.....thanks again
Mar 20 '09 #17
anfetienne
424 256MB
Here feel free to test the upload sequence @

http://ve-creative.com/test/8/templates.php

Once images are uploaded you can view their directory @

http://ve-creative.com/test/8/upload/randomdigit (please copy and paste the random digit generated on the first page and paste where "randomdigit")

Uploaded images are shown on the edit page.....please feel free to test the captions form underneath the images and click submit

the xml file created can be found @
http://ve-creative.com/test/8/xmlf (you can download the xml to see what is generated)
Mar 20 '09 #18
Markus
6,050 Expert 4TB
Looks like it is all working.

Couple of ideas: if an image has a caption, it would be useful to see that caption in it's caption input area. Currently, when you add captions, the inputs remain blank.

Also, your submit buttons, when not hovered over, appear 'disabled'. If you've ever seen a disabled form element, they have a greyish overlay, like your normal buttons do. This may be distracting.

And congrats.
Mar 20 '09 #19
anfetienne
424 256MB
loool i know exactly what you are saying but I can only work to what a client asks for and its not to distract people from their main objective.

Right now I am just sorting out how I want to structure my database for this app....once i've got my structure I will be adding in the captions so people can view it once they saved them
Mar 20 '09 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.