anf.etienne Posted March 18, 2009 Share Posted March 18, 2009 is it possible to have more than 1 array on a foreach loop? e.g. // Loop through the POST items foreach ( $_POST['picT'] as $key => $picStart) { foreach ( $_POST['photoT'] as $var => $photoName) { foreach ( $_POST['captionT'] as $var => $picCaption) { $nameCapsA = $xmlobj->addChild($picStart); $nameCapsA->addAttribute("name", $photoName); $nameCapsA->addAttribute("caption", $picCaption); } } } Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/ Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 ??? Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787660 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 can anyone help ??? Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787698 Share on other sites More sharing options...
Zane Posted March 18, 2009 Share Posted March 18, 2009 is it possible to have more than 1 array on a foreach loop? yes Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787699 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 could you point me in the right direction please? this is my original code below, the problem with this is it doesn't bring all values sent from the form for thephoto name and caption only for the line name (picT). I have to have all three as an array so it finds all the values for my variable to output to xml My code // Loop through the POST items foreach ( $_POST['picT'] as $key => $picStart) { $photoName = $_POST['photoT']; $picCaption = $_POST['captionT']; $nameCapsA = $xmlobj->addChild($picStart); $nameCapsA->addAttribute("name", $photoName); $nameCapsA->addAttribute("caption", $picCaption); } The result <?xml version="1.0" encoding="UTF-8"?> <gallery><pic1 name="PHOTO 3" caption="test03"/><pic2 name="PHOTO 3" caption="test03"/><pic3 name="PHOTO 3" caption="test03"/></gallery> Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787708 Share on other sites More sharing options...
Zane Posted March 18, 2009 Share Posted March 18, 2009 I could point you in the right direction but I'm clueless as to what you're asking. Your code doesn't make any sense. $photoName = $_POST['photoT']; $picCaption = $_POST['captionT']; shouldn't even be in the foreach loop. and where is picT coming from. is it a radio button list? what is it. explain more what you are trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787723 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 this is generated in php 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>"; picT is hidden as shown above with a value.....it makes it easier when generating the xml with simpleXML Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787750 Share on other sites More sharing options...
Zane Posted March 18, 2009 Share Posted March 18, 2009 This is the most I can give you, because I still don't know what you are doing although now I do know what your HTML looks like so.... use this to your advantage...it is probably what you are looking for foreach($_POST as $k=>$v) { if(is_array($v)) { echo "Contents of " . $k . " \n"; foreach($v as $theKey=>$theValue) echo $theKey . " -> " . $theValue . " \n"; echo " \n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787760 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 thanks....ill give that a go.....so you understand though here is my process. 1. Users upload pics 2. pics are renamed and relocated 3. pics are displayed using a table which is also generated as part of a form (code below), within the generated form there are these fields........picT, photoT, captionT.....picT is for the name of the xml child, photoT is for the image name and captionT is for the caption <form id="form2" name="form2" method="POST" action="xmltest.php"> <input type="hidden" name="create_xml" value="true"/> <input type="hidden" name="random_digit" id="random_digit" value="<?php print $random_digit;?>"/> <?php $returnURL = "http://ve-creative.com/test/8/templateEdit.php?template=$templateID&ID=$random_digit&=Step+2"; $path = "upload/$random_digit/"; // path to the directory to read ( ./ reads the dir this file is in) if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(!is_dir($file)){ $item[] = $file; } } } closedir($handle); } $total_items = count($item); $max_items = ceil($total_items / 5); // items per <td> $start = 0; $end = $max_items //generate the table ?> <center> <table width="675px" border="0" cellspacing="5" cellpadding="5" align="center"> <tr> <?php $r = 0; $i=1; $pn=1; $pto=1; for($n=0; $n<$total_items; $n++) { if($n !=0 && fmod($n, 5) == 0) { echo "</tr><tr>"; } $imageL= $path.$item[$n]; if (substr($imageL,-5) != 'b.jpg') { $img_path="http://ve-creative.com/test/8/$imageL"; // display the item 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>"; } } if($r>0) { for($m=$r; $m<5; $m++) { echo "<td> </td>"; } } ?> 4. Users enter the caption they'd like for each image and click submit/save....each image sent has a picT, photoT and captionT attached to it.....the values are sent to another php that generates a xml file from the values. 5. once generated the xml file is saved and the user is redirected back to the original page. Right now my problem is i can only get picT to show on the xml as needed i.e. pic1, pic2, pic3.....photoT needs to show as PHOTO 1, PHOTO 2, PHOTO 3 etc i can get them all to show seperately using the foreach loop but cant get them to show in the same document......these are the results i get.... <?xml version="1.0" encoding="UTF-8"?> <gallery> <pic1 name="PHOTO 3" caption="test03"/> <pic2 name="PHOTO 3" caption="test03"/> <pic3 name="PHOTO 3" caption="test03"/> </gallery> If i add photoT and captionT as an array in the code it basically says a string is needed not an array. So somehow i need to include them in the foreach loop hence my question......can i use 3 different arrays on one loop? Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787775 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 and this is my code for the xml <?php error_reporting(E_ALL); $photoName = $_POST['photoT']; $picCaption = $_POST['captionT']; $random_digit = $_POST['random_digit']; $xmlFiles = array(); if(isset($_POST['create_xml'])){ // Create the object (with header) $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>"; $xmlobj = simplexml_load_string($xmltext); // Loop through the POST items foreach ( $_POST['picT'] as $key => $picStart) { $nameCapsA = $xmlobj->addChild($picStart); $nameCapsA->addAttribute("name", "$photoName"); $nameCapsA->addAttribute("caption", $picCaption); } print header("Content-type: text/plain") . $xmlobj->asXML(); $path_dir = "xmlf/"; $path_dir .= $random_digit .".xml"; /* Data in Variables ready to be written to an XML file */ $fp = fopen($path_dir,'w'); $write = fwrite($fp,$xmlobj->asXML()); /* Loading the created XML file to check contents */ $sites = simplexml_load_file("$path_dir"); echo "<br> Checking the loaded file <br>" .$path_dir. "<br>"; echo "<br><br>Whats inside loaded XML file?<br>"; print_r($sites); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787787 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 hi zanus....thanks for the code but it's no help to me as i still can't include the values from photoT and captionT into the results as they should appear Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787807 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 any ideas anyone? Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787877 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Author Share Posted March 18, 2009 ???????? Quote Link to comment https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-788034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.