473,714 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

loop not working

anfetienne
424 Contributor
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['create_xml'])){
  3.  
  4.         $picT = $_POST['picT'];
  5.  
  6.         $photoT = $_POST['photoT'];
  7.  
  8.         $captionT = $_POST['captionT'];
  9.  
  10.  
  11. $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
  12. $xmlobj = simplexml_load_string($xmltext);
  13.  
  14.  
  15. $nameCapsA = $xmlobj->addChild("$picT");
  16. $nameCapsA->addAttribute("name", "$photoT");
  17. $nameCapsA->addAttribute("caption", "$captionT");
  18.  
  19. // set output filename
  20. $filename= "test1.xml";
  21.  
  22. print header("Content-type: text/plain") . $xmlobj->asXML();
  23. }
  24.  
  25. ?>
  26.  
Mar 13 '09 #1
16 1724
Markus
6,050 Recognized Expert Expert
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).
Mar 13 '09 #2
anfetienne
424 Contributor
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $r = 0;
  3. $i=1;
  4. $pn=1;
  5. $pto=1;
  6.  
  7. for($n=0; $n<$total_items; $n++) {
  8. if($n !=0 && fmod($n, 5) == 0) {
  9. echo "</tr><tr>";
  10. }
  11.       $imageL= $path.$item[$n];
  12.      if (substr($imageL,-5) != 'b.jpg')
  13.      {
  14.       $img_path="http://ve-creative.com/test/8/$imageL";
  15.             // display the item
  16.             echo '<td><center><p><a href="'.$img_path .'"><img src= "'.$path.$item[$n] .'" height="100" width="100"></a></p></center>';
  17.             echo '<center><p><input type="hidden" name="randT" value="'.$random_digit.'"></p></center>';
  18.             echo '<center><p><input type="hidden" name="idT" value="'.$i++.'"></p></center>';
  19.             echo '<center><p><input type="hidden" name="picT" value="pic'.$pn++.'"></p></center>';
  20.             echo '<center><p><input type="hidden" name="photoT" value="PHOTO '.$pto++.'"></p></center>';
  21.             echo '<center><p><input type="text" name="captionT" value=""></p></center>';
  22.             echo "<center><p><a href=imgEdit.php?img=$img_path> > Edit Image < </a></p></center><br></td>";  
  23.     }
  24. }
  25.  
  26. if($r>0) {
  27. for($m=$r; $m<5; $m++) {
  28. echo "<td>&nbsp;</td>";
  29. }
  30. }
  31.  
  32. ?>
  33.  

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. if(isset($_POST['create_xml'])){
  5.  
  6.         $picT = $_POST['picT'];
  7.  
  8.         $photoT = $_POST['photoT'];
  9.  
  10.         $captionT = $_POST['captionT'];
  11.  
  12.         $randT = $_POST['randT'];
  13.  
  14.  
  15. $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
  16. $xmlobj = simplexml_load_string($xmltext);
  17.  
  18.  
  19. $nameCapsA = $xmlobj->addChild("$picT");
  20. $nameCapsA->addAttribute("name", "$photoT");
  21. $nameCapsA->addAttribute("caption", "$captionT");
  22.  
  23. // set output filename
  24. $filename= "test1.xml";
  25.  
  26. print header("Content-type: text/plain") . $xmlobj->asXML();
  27.     }
  28.  
  29. ?>
  30.  
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
Mar 13 '09 #3
anfetienne
424 Contributor
when i test it without a loop it works perfectly fine but only generates 1 xml line
Mar 13 '09 #4
Markus
6,050 Recognized Expert Expert
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.

Expand|Select|Wrap|Line Numbers
  1. <input name="file_description[]" type="text" />
  2. <input name="file_description[]" type="text" />
  3.  
That array is then accessible through POST like so:

Expand|Select|Wrap|Line Numbers
  1. // $_POST['file_description'] is an array!
  2. foreach ( $_POST['file_description'] as $key => $file_description )
  3. {
  4.    echo $file_description, "<br />";
  5. }
  6.  
Now we have a loop, going through each of the submitted rows. This is where we can incorporate the XML object.

Expand|Select|Wrap|Line Numbers
  1. // Create the object (with header)
  2. $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
  3. $xmlobj = simplexml_load_string($xmltext);
  4.  
  5. // Loop through the POST items
  6. foreach ( $_POST['file_description'] as $key => $file_description )
  7. {
  8.     $child = $xmlobj->addChild( $key );
  9.     $child->addAttribute( 'description', $file_description );
  10. }
  11.  
Something like that.

- Mark.
Mar 14 '09 #5
anfetienne
424 Contributor
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $random_digit = $_POST['random_digit'];
  4.  
  5. error_reporting(E_ALL);
  6.  
  7. $xmlFiles = array();
  8.  
  9. function createXML($caption)
  10. {
  11.     global $xmlObjects;
  12.  
  13.     $picT = $_POST['picT'];
  14.     $photoT = $_POST['photoT'];
  15.  
  16.     $xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gallery></gallery>";
  17.     $xmlobj = simplexml_load_string($xmltext);
  18.  
  19.     $nameCapsA = $xmlobj->addChild($picT);
  20.     $nameCapsA->addAttribute("name", $photoT);
  21.     $nameCapsA->addAttribute("caption", $caption);
  22.  
  23.     $xmlFiles[] = $xmlobj->asXML();
  24. }
  25.  
  26. if(isset($_POST['create_xml']))
  27. {
  28.  
  29.     if (is_array($_POST['captionT']))
  30.     {
  31.         foreach ($_POST['captionT'] as $caption)
  32.         {
  33.             createXML($caption);
  34.         }
  35.     }
  36.     else
  37.     {
  38.         createXML($_POST['captionT']);
  39.     }
  40. }
  41.  
  42. echo header("Content-type: text/plain");
  43.  
  44. //Echo data from each file.
  45. foreach ($xmlFiles as $xmlFile)
  46. {
  47.     echo $xmlFile . "<br /><br />\n";
  48.  
  49.         $path_dir = "xmlf/";
  50.  
  51.         $path_dir .=   $random_digit .".xml";
  52.  
  53. $fp = fopen($path_dir,'w');
  54.  
  55.             $write = fwrite($fp,$xmlFile);
  56.  
  57.  
  58. }
  59.  
  60. ?> 
  61.  
Mar 14 '09 #6
Markus
6,050 Recognized Expert Expert
-.-

I don't follow what's happening.

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

- Markus.
Mar 14 '09 #7
anfetienne
424 Contributor
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);
Mar 14 '09 #8
anfetienne
424 Contributor
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 );
# }
Mar 14 '09 #9
Markus
6,050 Recognized Expert Expert
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.
Mar 14 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1989
by: Ken Fine | last post by:
I'm trying to include a list of people that's the result of looping through a recordset in a CDONTS mail. I'm trying to Dim the output of a loop, and it ain't working -- I'm getting a syntax error. Any ideas why the following snippet isn't working? <% Dim ConfirmedSpeakerList2 ConfirmedSpeakerList2=(While ((Repeat1__numRows <> 0) AND (NOT rsConfirmedSpeakers.EOF)) (rsConfirmedSpeakers.Fields.Item("Per_GivenName").Value)
8
1667
by: Drew | last post by:
I am building an application for keeping track of user permissions here at work. I have built the interfaces, and am now working on the processing page for inserting to the database. I am having a problem with a conditional in my loop not working correctly, I am sure that I have been overlooking something, but cannot pin it down. I have pared the code down quite a bit just so that I could resolve my issues with it. I have a page...
8
2853
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++) For(i=0; i<=K; i++)
5
9737
by: L. Oborne | last post by:
I have this code working fine in Classic ASP but I get compile errors when I try to run it as ASP.NET. Do While NOT RS.EOF If ... Then... Else... End If RS.MoveNext Loop
34
2675
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include <limits.h> #include <stdlib.h> int main(void) {
52
6330
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible variations(combination of fields), - then act on each group in some way ...eg ProcessRs (oRs as RecordSet)... the following query will get me the distinct groups
2
1863
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be duplicate so many time, also i'm using a removeloop if the client want to remove the frame, in the html of the page i'm creating a table and i'm calling this loop, the problem now is that when i'm calling the removeloop it removes the frame but not the...
2
2071
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with mysql. Same problem in Fx 2.0 and IE 7. I have the query sort of spread out across three pages to make it easier for me to generate it; I have a pagination script which checks what is being accessed from the query string (is it the whole blog or an...
6
500
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void output(unsigned char ret_buffer, int curr_buffer_size, bool& endoffile) { int index2=0; int addr=0; unsigned char outBuff;
4
6828
by: joaotsetsemoita | last post by:
hello everyone. Im trying to time out a loot after a certain time. Probably 5 to 10 minutes. I have the following function Private Sub processFileCreation(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) Dim strFilename As String = "c:\web\example.mdb"
0
8815
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9187
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9034
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6639
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3164
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.