Jump to content

Recommended Posts

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);
}
    }
}

Link to comment
https://forums.phpfreaks.com/topic/149975-foreach-loop/
Share on other sites

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>

 

Link to comment
https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787708
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787723
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787750
Share on other sites

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";
   }
}

Link to comment
https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787760
Share on other sites

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787775
Share on other sites

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);


}


?> 

Link to comment
https://forums.phpfreaks.com/topic/149975-foreach-loop/#findComment-787787
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.