is it possible to have more than 1 array on a foreach loop?
19 1494
What do you mean? Post a sample code so we can see what you're trying to do.
yes, by adding array from while loop
Try to use while loop for getting array
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);
}
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: -
$array_1 = array ( 1, 2, 3, 4, 5, 6 );
-
$array_2 = array ( "one", "two", "three", "four", "five", "six" );
-
-
foreach ( $array_1 as $key => $value )
-
{
-
echo "{$value} = {$array_2[$key]}<br />";
-
}
-
// Produces:
-
// 1 = one
-
// 2 = two
-
// ... etc.
-
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
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
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.
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.... -
echo '<td><center><p><a href="'.$img_path .'"><img src= "'.$path.$item[$n] .'" height="100" width="100"></a></p></center>';
-
echo '<center><p><input type="hidden" name="picT[]" value="pic'.$pn++.'"/></p></center>';
-
echo '<center><p><input type="hidden" name="photoT[]" value="PHOTO '.$pto++.'"/></p></center>';
-
echo '<center><p><input type="text" name="captionT[]" value=""/></p></center>';
-
echo "<center><p><a href=imgEdit.php?img=$img_path> > Edit Image < </a></p></center><br></td>";
-
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?
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. -
-
foreach ( $_POST['picT'] as $key => $value )
-
{
-
// use $key to access the other elements.
-
echo $value . "<br />";
-
echo $_POST['photoT'][$key] . "<br />";
-
echo $_POST['captionT'][$key] . "<br />";
-
-
// Do something with your xml object
-
$child = $xml->addChild( $value );
-
$child->addAttribute( 'name', $_POST['photoT'][$key] );
-
$child->addAttribute( 'caption', $_POST['captionT'][key] );
-
}
-
-
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
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
Yeah, you're going to have to use your xml object - the one you've shown in previous threads.
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.
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |