Why is my small PHP script not working?

IcorneBen

New Member
I am writing a script that takes information entered in this format:\[code\]John Smith123 Fake StRiverwood, 11234http://google.com\[/code\]and turns it into\[code\]<location id='1'> name='John Smith', address='123 Fake St', city='Riverwood', zip=' 11234', website='http://google.com'</location>\[/code\]Here is the code:\[code\]$locations = $_POST['locations'];$data = http://stackoverflow.com/questions/3847381/explode("\n", $locations);$dataBlocks = explode("\n\n", $locations);$num = 1;for ($i=0; $i<count($dataBlocks); $i++) { $name = $data[0]; $address = $data[1]; $data2 = explode(',', $data[2]); $city = $data2[0]; $zip = $data2[1]; $website = $data[3]; $xml = ("<location id='$num'> name='$name', address='$address', city='$city', zip='$zip', website='$website' </location>"); $num++;}\[/code\]Now, the above works for one block of info, but when I have 2 or more blocks like so:\[code\]John Smith123 Fake StRiverwood, 11234http://google.comPeter Gunderson345 Some StWoodsdale, 44566http://yahoo.com\[/code\]Then I get this output:\[code\]<location id='1'> name='John Smith', address='123 Fake St', city='Riverwood', zip=' 11234', website='http://google.com'</location><location id='2'> name='John Smith', address='123 Fake St', city='Riverwood', zip=' 11234', website='http://google.com'</location>\[/code\]I know why this is, it's because I am defining these variables:\[code\]$name = $data[0];$address = $data[1];$data2 = explode(',', $data[2]);$city = $data2[0];$zip = $data2[1];$website = $data[3];\[/code\]based on there places in the $data array. So, even though I'm using a loop the values of those variables are always the same. For example: whereas for John Smith the $name variable should have a value of $data[0], for Peter Gunderson the name variable should have a value of $data[5]. But now it's $data[0] for both. Hence the output is showing the same name, address, etc. for every info block entered.So the question is, how can this script be modified so that the output will show unique results for every info block and not just repeat the first one?EDIT@gazler your code gives me this output for the above example:\[code\]<location id='1'> name='John Smith', address='123 Fake St', city='Riverwood', zip=' 11234', website='http://google.com'</location><location id='2'> name='', address='Peter Smith', city='', zip='', website='Woodsdale, 44566'</location>\[/code\]
 
Back
Top