-4

The title is a bit confusing, but the error which I have is -

$xml="<Contacts>";
     for($i=0;$i<count($results['records']);$i++){
        $xml. = "<Contact>
                   <Name>".$results['records'][$i]['name']."</Name>";
         }
 $xml.="</Contacts>";

When I try to add something to string (concatenate) I get 500 Internal server error. I believe that problem is in " $results['records'][$i]['name']". I think the solution is to replace JSON value with variable and enclose it in {}...maybe I am wrong, i don't know.

UPD:

if I "echo ".$results['records'][$i]['name'].""; It works fine.

Anatolii Vorobiov
  • 129
  • 1
  • 1
  • 11
  • It is definitely a bad idea to generate XML manually. – Yeldar Kurmangaliyev Jun 29 '15 at 04:00
  • 2
    @YeldarKurmangaliyev ...why? – Sverri M. Olsen Jun 29 '15 at 04:01
  • How does `.=` work when you try that instead of `. =`? – Drakes Jun 29 '15 at 04:01
  • 3
    Also `` isn't closed, ever. – Drakes Jun 29 '15 at 04:02
  • 1
    where is the close tag of ` ` – Sathish Jun 29 '15 at 04:02
  • @YeldarKurmangaliyev it's not about xml, i could replace the name of variable with other name. =) – Anatolii Vorobiov Jun 29 '15 at 04:02
  • @SverriM.Olsen Because you are reinventing a wheel. It is hard to extend, modify and read such code but it is easy to make a mistake (OP already made a mistake). There are enough XML writing \ reading tools in PHP. Read this article: http://stackoverflow.com/questions/486757/how-to-generate-xml-file-dynamically-using-php – Yeldar Kurmangaliyev Jun 29 '15 at 04:03
  • @AnatoliiVorobiov It is definitely about XML. No matter how your variable is called, you are generating XML in your loop whether you understand it or not. – Yeldar Kurmangaliyev Jun 29 '15 at 04:05
  • 1
    Yeldar is right, you should use a DOMDocument class or a third-party library in order to build objects and arrays that become XML strings when needed. Anyway, please post directly the source code that leads to the 500, and if you can check out the Apache error log. It contains the error description, because 500 is server-side generated. – Snake Jun 29 '15 at 04:05
  • I am really shocked and confused about upvoting a comment "why?" concerning question "why is it bad to generate XML manually". It is a known bad practice. XML is not only about tags - it is about namespaces, formatting, schemas, escaping. Read this SO topic: http://stackoverflow.com/questions/3034611/whats-so-bad-about-building-xml-with-string-concatenation – Yeldar Kurmangaliyev Jun 29 '15 at 04:08
  • @YeldarKurmangaliyev Nonsense. XML is highly structured and only needs to be written once. Once written you just interpolate your values. It is dead easy (and has nothing to do with reinventing anything) and it is many times faster than programmatically constructing it every single time you need the structure. – Sverri M. Olsen Jun 29 '15 at 04:09
  • @SverriM.Olsen I do not understand you. I am talking about **dynamical** generating XML through **string concatenation** instead of XML serialization. That's what OP is actually trying to do. I don't say to generate **every** XML file programatically. – Yeldar Kurmangaliyev Jun 29 '15 at 04:13

1 Answers1

1

You have a string concatenation (.=) syntax error. Change

$xml. = "<Contact>

to

$xml .= "<Contact>

I'm strictly answering the question about the "string concatenation" PHP error. No downvotes for anything except this point, please. But yes, try not to generate XML manually. I closed </Contact>s for you below.

$xml="<Contacts>";
for ($i = 0; $i < count($results['records']); $i++) {
    $xml .= "<Contact><Name>".$results['records'][$i]['name']."</Name></Contact>";
}
$xml.="</Contacts>";
Drakes
  • 23,254
  • 3
  • 51
  • 94