1

So I'm having trouble setting up a for loop in PHP to output the contents of an HTML form to an xml file. It works without the for loop, but not with. Any ideas? Tearing my hair out!

Thanks in advance.

//PHP Form

<?php
if (isset($_POST['lsr-submit']))
{
}
$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$xml = simplexml_load_string($str);
$i=1;
for ($i=1; $i <50; $i++) 
{ 
   $name = $_POST['Name_'.$i];
   $time = $_POST['Time_'.$i];
   $duration = $_POST['Duration_'.$i];
   $name = htmlentities($name, ENT_COMPAT, 'UTF-8', false);
   $time = htmlentities($time, ENT_COMPAT, 'UTF-8', false);
   $duration = htmlentities($duration, ENT_COMPAT, 'UTF-8', false);
   $xml->Slot = "";
   $xml->Slot->addChild('Name', $name);
   $xml->Slot->addChild('Time', $time);
   $xml->Slot->addChild('Duration', $duration);
}
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('../scSHARE/editor.xml');
print "<meta http-equiv=\"refresh\"     content=\"0;URL=return.php\">";
?>

//HTML Form

<?php
$xml=simplexml_load_file("../scSHARE/editor.xml");
$xml_array = (array) $xml;
$chunks=array_chunk($xml_array["Slot"], 7);
$i=1;
$l=1;
foreach($chunks as $bank) {
  ?> 
  <form name="form1" method="post" action="form.php">
     <div class="menu-toggle bank_<?php echo $i; ?>"  style="display:none;">
       <?php
       foreach($bank as $slot) { ?>
         <div class="row">
           <div class="small-12 column">
             <div class="small-6 column">
                Slot <?php echo $l?>
               <input type="text" name="Name_<?php echo $l;?>" value="<?php echo $slot->Name;?>">
             </div>
             <div class="small-6 column">
               <div class="row">
                 <div class="small-6 column">
                  Time
                   <input type="text" name="Time_<?php echo $l;?>" value="<?php echo $slot->Time;?>">
                 </div>
                 <div class="small-6 column">
                   Duration
                   <input type="text" name="Duration_<?php echo $l;?>" value="<?php echo $slot->Duration;?>">
                 </div>
               </div>
             </div>
           </div>
         </div>
       <?php $l++;} ?>
      <p style="text-align: center;">
        <input type="submit" name="submit" id="submit" class="hollow success button" value="Update">
      </p>
     </div>
  </form>

<?php $i++;
}
?>
1
  • 1
    First of all, your if (isset($_POST['lsr-submit'])) has no effects. You need to apply it to all your code, not to nothing. Commented Apr 28, 2016 at 22:02

1 Answer 1

1

You use this syntax:

for ($i=1; $i <50; $i++) 
{ 
    (...)
    $xml->Slot = "";
    $xml->Slot->addChild('Name', $name);
}

By this way, at each iteration you override previous <Slot> node. You have to add child(s) to root node instead:

for ($i=1; $i <3; $i++) 
{ 
    (...)
    $slot = $xml->addChild( "Slot" );
    $slot->addChild( 'Name', $name );
    $slot->addChild( 'Time', $time );
    $slot->addChild( 'Duration', $duration );
}

Side note:

Consider wrapping entire code by isset($_POST['lsr-submit'])

if (isset($_POST['lsr-submit']))
{
    $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
    (...)
    print "<meta http-equiv=\"refresh\"     content=\"0;URL=return.php\">";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that makes sense, its working perfectly now. Thanks for the quick response. Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.