Milind
Milind

Reputation: 71

How to use loop in comment section

I have used foreach loop to extract multiple value of user_phone between tab but it will produce error.I dont what is exact formate.

 $result = $Admin->select($Admin->newsletter_subscribers,'',"");
 print_r($result['user_phone']);
 $data="<message-submit-request>
 <username>@@@@@@@</username>
 <password>#######</password>
 <sender-id>$$$$$$</sender-id>".
 foreach($result as $row)
 {
   "<to>".$row['user_phone']."</to>"
 }."<MType></MType>
<message-text>
<text>hi test message 1</text>
</message-text>
</message-submit-request>";

Upvotes: 0

Views: 70

Answers (2)

hakre
hakre

Reputation: 197682

The exact format for string concatenation is shown in the PHP manual under string operators. However I won't elaborate on these directly becasue cerating XML by concatenating strings is a bad idea. For example having characters like <, > and & in your input, you'll run into many problems.

For your use-case SimpleXMLElement is a handy object that allows you to do the same in a much more consisted manner:

// create XML document based on boilerplate
$xml = new SimpleXMLElement('
<message-submit-request>
    <username>@@@@@@@</username>
    <password>#######</password>
    <sender-id>$$$$$$</sender-id>
    <MType></MType>
    <message-text>
        <text>hi test message 1</text>
    </message-text>
</message-submit-request>
');

// add elements where appropriate
foreach($result as $row)
{
    $xml->addChild('to', $row['user_phone']);
}

This will ensure that all values are properly encoded. Additionally this can have a magnitude on benefits when you further process the data, e.g. outputting it.

However you haven't shown in your question what follows up with $data so you need to know how to obtain the XML from the SimpleXMLElement as string to make the example complete. Here it is:

$data = $xml->asXML();

See as well the SimpleXML Basic Usage guide in the manual if you'd like to learn more.

Upvotes: 0

DS9
DS9

Reputation: 3033

Try this:

$result = $Admin->select($Admin->newsletter_subscribers,'',"");
print_r($result['user_phone']);
$data = "<message-submit-request>
 <username>@@@@@@@</username>
 <password>#######</password>
 <sender-id>$$$$$$</sender-id>";

 foreach($result as $row)
 {
  $data .= "<to>".$row['user_phone']."</to>";
 }

 $data .= "<MType></MType>
<message-text>
<text>hi test message 1</text>
</message-text>
</message-submit-request>";

Upvotes: 1

Related Questions