Title: Adding number to fetched array with php
Tags: php;mysql;xml
Question: I have some problem with adding numbering to the fetched array on the parsed xml output. I got a code from a website located here http://www.mightywebdeveloper.com/coding/mysql-to-xml-php/
<?php
header('Access-Control-Allow-Origin: *');
$oid= $_GET['oid'];
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "thisisuser";
$config['mysql_pass'] = "thisispass";
$config['db_name']    = "mydb";
$config['table_name'] = "mail";
//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
$xml          = "<?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;";
$root_element = $config['table_name']."s"; //fruits
$xml         .= "<$root_element&gt;";
//select all items in table
$sql = "SELECT * FROM mail WHERE oid='".$oid."' ORDER BY id ";
//SELECT * FROM ".$config['table_name'];
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
if(mysql_num_rows($result)&gt;0)
while($result_array = mysql_fetch_assoc($result))
$xml .= "<".$config['table_name']."&gt;";
//loop through each key,value pair in row
foreach($result_array as $key =&gt; $value)
{
//$key holds the table column name
$xml .= "<$key&gt;";
//embed the SQL data in a CDATA element to avoid XML entity issues
$xml .= "$value";
//and close the element
$xml .= "</$key&gt;";
}
$xml.="</".$config['table_name']."&gt;";
//close the root element
$xml .= "</$root_element&gt;";
//send the xml header to the browser
header ("Content-Type:text/xml");
//output the XML data
echo $xml;
It work just fine after a few edit and the output goes like this.
<mails&gt;
<mail&gt;
<id&gt;1011</id&gt;
<oid&gt;1</oid&gt;
<from&gt;Test User</from&gt;
<content&gt;This is a test mail.</content&gt;
</mail&gt;
<mail&gt;
<id&gt;101222</id&gt;
<oid&gt;1</oid&gt;
<from&gt;Test User</from&gt;
<content&gt;This is a test mail.</content&gt;
</mail&gt;
</mails&gt;
My problem is I would like to display the fetched array number and automaticaly increase the number next to the mail tag similar to something like
<mails&gt;
<mail  id="1"&gt; <-fetched array number? I dont know how to add this.
<id&gt;101221</id&gt;
<oid&gt;1</oid&gt;
<from&gt;Test User</from&gt;
<content&gt;This is a test mail.</content&gt;
</mail&gt;
<mail  id="2"&gt;
<id&gt;101222</id&gt;
<oid&gt;1</oid&gt;
<from&gt;Test User</from&gt;
<content&gt;This is a test mail.</content&gt;
</mail&gt;
</mails&gt;
Please guide me.


Here is another answer: $mail = 1;   // write this before loop
then instead of $xml .= "<$key&gt;";
$xml .= ($key == 'mail' ? "<$key id=\"".$mail++."\"&gt;" : "<$key&gt;");
Comment for this answer: where do I write the $mail =1;
Comment for this answer: Still the same result no changes
Comment for this answer: the result still like this`
1111112
Test User
This is a test mail.
Comment for this answer: Yea I changed to this `$xml .= ($key == 'mail' ? "" : "");`
Comment for this answer: for example, before this `if(mysql_num_rows($result)>0)`
Comment for this answer: such tag    `` ?
Comment for this answer: did you chang the second line?
Comment for this answer: By the way there is typo there - must be `"<$key id="`

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/10/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><mysql><xml>', 'Title': 'Adding number to fetched array with php', 'ViewCount': 49, 'id': 30759225, 'user_AboutMe': None, 'user_AccountId': 6447148.0, 'user_DisplayName': 'TeachMe', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 4995337.0}}