mysql - Adding number to fetched array with php -


i have problem adding numbering fetched array on parsed xml output. got code 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 host mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']); //select database @mysql_select_db($config['db_name']) or die( "unable select database");  $xml          = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; $root_element = $config['table_name']."s"; //fruits $xml         .= "<$root_element>";  //select items in table $sql = "select * mail oid='".$oid."' order id ";  //select * ".$config['table_name'];  $result = mysql_query($sql); if (!$result) {     die('invalid query: ' . mysql_error()); }  if(mysql_num_rows($result)>0) {    while($result_array = mysql_fetch_assoc($result))    {       $xml .= "<".$config['table_name'].">";        //loop through each key,value pair in row       foreach($result_array $key => $value)       {          //$key holds table column name          $xml .= "<$key>";           //embed sql data in cdata element avoid xml entity issues          $xml .= "$value";           //and close element          $xml .= "</$key>";       }        $xml.="</".$config['table_name'].">";    } }  //close root element $xml .= "</$root_element>";  //send xml header browser header ("content-type:text/xml");  //output xml data echo $xml;  ?> 

it work fine after few edit , output goes this.

<mails>     <mail>         <id>1011</id>         <oid>1</oid>         <from>test user</from>         <content>this test mail.</content>     </mail>         <mail>         <id>101222</id>         <oid>1</oid>         <from>test user</from>         <content>this test mail.</content>     </mail> </mails> 

my problem display fetched array number , automaticaly increase number next mail tag similar like

<mails>     <mail  id="1"> <-fetched array number? dont know how add this.         <id>101221</id>         <oid>1</oid>         <from>test user</from>         <content>this test mail.</content>     </mail>         <mail  id="2">         <id>101222</id>         <oid>1</oid>         <from>test user</from>         <content>this test mail.</content>     </mail> </mails> 

please guide me.

$mail = 1;   // write before loop 

then instead of $xml .= "<$key>";

$xml .= ($key == 'mail' ? "<$key id=\"".$mail++."\">" : "<$key>"); 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -