loop not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    loop not working

    i've created my code to go from a form to xml, it creates the xml and saves it on my server perfectly fine....the problem im having now is the loop is only picking up the last form entry instead of all form entries.

    This is my code

    Code:
    <?php
    if(isset($_POST['create_xml'])){
    
            $picT = $_POST['picT'];
    
            $photoT = $_POST['photoT'];
    
            $captionT = $_POST['captionT'];
    		
    
    $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
    $xmlobj = simplexml_load_string($xmltext);
    
    
    $nameCapsA = $xmlobj->addChild("$picT");
    $nameCapsA->addAttribute("name", "$photoT");
    $nameCapsA->addAttribute("caption", "$captionT");
    
    // set output filename
    $filename= "test1.xml";
    
    print header("Content-type: text/plain") . $xmlobj->asXML();
    }
    
    ?>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I don't see a loop :)

    Also, you're only grabbing one value from the POST array.

    Can I see how you're passing the data (i.e. the form).

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #3
      I eventually had to take the loop out because it just kept going like a duracell bunny lol here is the code i've used to generate the form and updated xml that also collects the random digit from the previous page

      Code:
      <?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="randT" value="'.$random_digit.'"></p></center>';
      			echo '<center><p><input type="hidden" name="idT" value="'.$i++.'"></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>&nbsp;</td>";
      }
      }
      
      ?>

      Code:
      <?php
      
      
      if(isset($_POST['create_xml'])){
      
              $picT = $_POST['picT'];
      
              $photoT = $_POST['photoT'];
      
              $captionT = $_POST['captionT'];
      		
              $randT = $_POST['randT'];
      		
      
      $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
      $xmlobj = simplexml_load_string($xmltext);
      
      
      $nameCapsA = $xmlobj->addChild("$picT");
      $nameCapsA->addAttribute("name", "$photoT");
      $nameCapsA->addAttribute("caption", "$captionT");
      
      // set output filename
      $filename= "test1.xml";
      
      print header("Content-type: text/plain") . $xmlobj->asXML();
      	}
      
      ?>
      It submits perfectly fine to the xml coding, when i add my part to save the file that works as well but its the looping that im having a hard time with as well

      Comment

      • anfetienne
        Contributor
        • Feb 2009
        • 424

        #4
        when i test it without a loop it works perfectly fine but only generates 1 xml line

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          You're naming every input the same. Doing this will only pass the last value, overwriting the previous ones.

          You can pass an array through your inputs, by adding brackets to the end of the name attribute.

          Code:
          <input name="file_description[]" type="text" />
          <input name="file_description[]" type="text" />
          That array is then accessible through POST like so:

          Code:
          // $_POST['file_description'] is an array!
          foreach ( $_POST['file_description'] as $key => $file_description )
          {
             echo $file_description, "<br />";
          }
          Now we have a loop, going through each of the submitted rows. This is where we can incorporate the XML object.

          Code:
          // 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['file_description'] as $key => $file_description )
          {
              $child = $xmlobj->addChild( $key );
              $child->addAttribute( 'description', $file_description );
          }
          Something like that.

          - Mark.

          Comment

          • anfetienne
            Contributor
            • Feb 2009
            • 424

            #6
            hey markus thanks for that.....i went through so many turoials last night......can you tell me what you think of this that i made

            Code:
            <?php
            
            $random_digit = $_POST['random_digit'];
            
            error_reporting(E_ALL);
            
            $xmlFiles = array();
            
            function createXML($caption)
            {
                global $xmlObjects;
            
                $picT = $_POST['picT'];
                $photoT = $_POST['photoT'];
            
                $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
                $xmlobj = simplexml_load_string($xmltext);
            
                $nameCapsA = $xmlobj->addChild($picT);
                $nameCapsA->addAttribute("name", $photoT);
                $nameCapsA->addAttribute("caption", $caption);
            
                $xmlFiles[] = $xmlobj->asXML();
            }
            
            if(isset($_POST['create_xml']))
            {
            
                if (is_array($_POST['captionT']))
                {
                    foreach ($_POST['captionT'] as $caption)
                    {
                        createXML($caption);
                    }
                }
                else
                {
                    createXML($_POST['captionT']);
                }
            }
            
            echo header("Content-type: text/plain");
            
            //Echo data from each file.
            foreach ($xmlFiles as $xmlFile)
            {
                echo $xmlFile . "<br /><br />\n";
            
                    $path_dir = "xmlf/";
            
                    $path_dir .=   $random_digit .".xml";
            		
            $fp = fopen($path_dir,'w');
            
                        $write = fwrite($fp,$xmlFile);
            
            		
            }
            
            ?>

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              -.-

              I don't follow what's happening.

              But, if it's working for you, go with it.

              - Markus.

              Comment

              • anfetienne
                Contributor
                • Feb 2009
                • 424

                #8
                it comes up blank.....no errors nothing loool....ive just tried your code and i got this

                test1
                test2
                test3

                Warning: Invalid argument supplied for foreach() in /home/vecre0/public_html/test/8/xmltest3.php on line 23

                do you think it would be best to merge mine and yours somehow so it includes

                $nameCapsA = $xmlobj->addChild($picT );
                $nameCapsA->addAttribute(" name", $photoT);

                Comment

                • anfetienne
                  Contributor
                  • Feb 2009
                  • 424

                  #9
                  markus....how can i get this so that i have 3 attribues in my array for the additional addAttributes? I have figured out everything else.....foreac h is the best loop to use but i cant get the extra arrays....shoul d i use standard array or associative?

                  # // Loop through the POST items
                  # foreach ( $_POST['file_descripti on'] as $key => $file_descripti on )
                  # {
                  # $child = $xmlobj->addChild( $key );
                  # $child->addAttribute ( 'description', $file_descripti on );
                  # }

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    I'll be honest with you here.

                    I'm having a hard time understanding what is currently going on. I've given you numerous examples to work with, but each time you seem to do something different. I'd love to help you get this all sorted out, but, seriously, it's eating a lot of my time. Lol. The big issue is: I don't want to do this for you. I want to give you the building blocks and have you learn from this instead of taking what I've wrote and stick it on your website - that is what the website is fundamentally about: helping, but in a way that will educate.

                    Now, what you're trying to do is not that hard for someone who has some experience with PHP, and I suspect you don't. It's only a small project, so you might consider hiring someone (we have a jobs forum).

                    But like I said, I do want to help.

                    So, let's start from the top: tell me everything about your project. I need to know the steps you are taking from beginning to end.

                    Here's what I know (or I think I know):
                    • You're uploading images for a flash gallery
                    • The flash gallery relies on an XML file to locate the images, assign captions, etc.


                    What else?

                    I'm thinking if we start with a plan, we can knock this out quickly.

                    - Markus.

                    Comment

                    • anfetienne
                      Contributor
                      • Feb 2009
                      • 424

                      #11
                      Hi,

                      sorry for troubling you so much loool but it is very strange.....i have got rid of all what i had before and basically tried your example you gave me....i done a small test and used echo to see if the variables functioned and they do but once i include the SimpleXML i just get a blank white screen on my tests with no errors and i have error reporting on.

                      based on what you told me this is my code so far

                      Code:
                      <?php error_reporting(E_ALL);
                      
                      
                      $random_digit = $_POST['random_digit'];
                      $xmlFiles = array();
                      
                      
                      if(isset($_GET['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['photoT'] as $key => $photoName)
                      {
                      	$picT = $_POST['picT'];
                      	$captionT = $_POST['captionT'];
                      
                      	$nameCapsA = $xmlobj->addChild($picT);
                          $nameCapsA->addAttribute("name", $photoName);
                          $nameCapsA->addAttribute("caption", $captionT);
                      	
                      
                      print header("Content-type: text/plain") . $xmlobj->asXML();
                      	}
                      //Echo data from each file.
                      foreach ($xmlFiles as $xmlFile)
                      {
                        
                      
                              $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);
                      	}
                      
                      }
                      
                      
                      ?>

                      Comment

                      • anfetienne
                        Contributor
                        • Feb 2009
                        • 424

                        #12
                        everything for the flash gallery is done i can get it to save the xml and the flash gallery picks it up and recognises but when i've got the lop you showed me in my code i just get a blank white with no erros.....i have no quarms with anything else....you pointed me in the right direction for those

                        Comment

                        • anfetienne
                          Contributor
                          • Feb 2009
                          • 424

                          #13
                          i noticed i made 1 mistake in "if(isset($ _GET['create_xml'])){"

                          i have changed that....now i get this error

                          Warning: Invalid argument supplied for foreach() in /home/vecre0/public_html/test/8/xmltest.php on line 15

                          Im going to keep working at it....im pretty close

                          Comment

                          • anfetienne
                            Contributor
                            • Feb 2009
                            • 424

                            #14
                            Hi,

                            I have been reading over my coding and I have figured out why the loop i wrote wasn't working and why the loop example markus stated never worked.......ba sically the part of my code working off an array produces the values needed but because it's the only item on array it selects the last value of the other var's and posts them

                            To make this work i need to have all values in an array which i have done. My question is is it possible to have more than 1 array on a foreach loop?

                            here is an example of my coding....

                            Code:
                            // Loop through the POST items
                            foreach ( $_POST['photoT'] as $key => $photoName)
                            {
                            	$picT = $_POST['picT'];
                            	$captionT = $_POST['captionT'];
                            
                            	$nameCapsA = $xmlobj->addChild($picT);
                                $nameCapsA->addAttribute("name", $photoName);
                                $nameCapsA->addAttribute("caption", $captionT);
                            	
                            
                            }

                            Comment

                            • anfetienne
                              Contributor
                              • Feb 2009
                              • 424

                              #15
                              Thanks for helping me understand how to include the extra arrays to a foreach loop markus.....all what you told me and taught me works greta......than ks again

                              Comment

                              Working...