tags:

views:

114

answers:

6

I am getting the result in browser, but i need this output to be in xml format too.

print "<h2>Search results for '".$_POST['keyword']."':</h2>\n";
   for( $i = 1; $row = mysql_fetch_array($result); $i++ )
   {
      print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n";
      print "(occurrences: ".$row['occurrences'].")<br><br>\n";
   }

   /* Present how long it took the execute the query: */
   print "query executed in ".(substr($end_time-$start_time,0,5))." seconds.";
}
else
{
   /* If no keyword is defined, present the search page instead: */
   print "<form method='post'> Keyword: 
          <input type='text' size='20' name='keyword'>\n";
   print "Results: <select name='results'><option value='5'>5</option>\n";
   print "<option value='10'>10</option><option value='15'>15</option>\n";
   print "<option value='20'>20</option></select>\n";

   print "<input type='submit' value='Search'></form>\n";
}

print "</body></html>\n";
A: 

You could just output an XML string as well as a HTML one.

Toby Allen
A: 

Toby, i was asking on how to output as i seem to get some error on that.

A: 

Do you mean that you are trying to write the data to a file? The easy way is to wrap your print code like this:

ob_start()
/* print statements */
$data = ob_get_clean();

Then, you can print it out:

print $data;

As well as write it to a file:

file_put_contents('/path/to/file.xml', $data);
Jeff Ober
A: 

I need it to write to xml file, i got the point... jeff but i am not able to.... i tried the way... let me post the entire code which needs to be converted.

<?

print "<html><head><title>My Search Engine</title></head><body>\n";

if( $_POST['keyword'] )
{
   /* Connect to the database: */
   mysql_pconnect("localhost","monty","indian")
       or die("ERROR: Could not connect to database!");
   mysql_select_db("sachin");

   /* Get timestamp before executing the query: */
   $start_time = getmicrotime();

   /* Set $keyword and $results, and use addslashes() to
    *  minimize the risk of executing unwanted SQL commands: */
   $keyword = addslashes( $_POST['keyword'] );
   $results = addslashes( $_POST['results'] );

   /* Execute the query that performs the actual search in the DB: */
   $result = mysql_query(" SELECT p.page_url AS url,
                           COUNT(*) AS occurrences 
                           FROM page p, word w, occurrence o
                           WHERE p.page_id = o.page_id AND
                           w.word_id = o.word_id AND
                           w.word_word = \"$keyword\"
                           GROUP BY p.page_id
                           ORDER BY occurrences DESC
                           LIMIT $results" );

   /* Get timestamp when the query is finished: */
   $end_time = getmicrotime();

   /* Present the search-results: */
   print "<h2>Search results for '".$_POST['keyword']."':</h2>\n";
   for( $i = 1; $row = mysql_fetch_array($result); $i++ )
   {
      print "$i. <a href='".$row['url']."'>".$row['url']."</a>\n";
      print "(occurrences: ".$row['occurrences'].")<br><br>\n";
   }

   /* Present how long it took the execute the query: */
   print "query executed in ".(substr($end_time-$start_time,0,5))." seconds.";
}
else
{
   /* If no keyword is defined, present the search page instead: */
   print "<form method='post'> Keyword: 
          <input type='text' size='20' name='keyword'>\n";
   print "Results: <select name='results'><option value='5'>5</option>\n";
   print "<option value='10'>10</option><option value='15'>15</option>\n";
   print "<option value='20'>20</option></select>\n";

   print "<input type='submit' value='Search'></form>\n";
}

print "</body></html>\n";

/* Simple function for retrieving the current timestamp in microseconds: */
function getmicrotime()
{
   list($usec, $sec) = explode(" ",microtime());
   return ((float)$usec + (float)$sec);
}

?>

I dont know why this is not working at all, i am calling this in flex application so i need it in xml format.

print "<xmlFor>";
while( $Row = mysql_fetch_object( $result ) )
{
print "<person><id>".$Row->keyword."</id><name>".$Row->url."</name></person>\n";
}
print "</xmlFor>";
A: 

instead of printing the html, print the xml... no problem? else post your error message here

Antony Carthy
A: 
print "<xmlFor>";
while( $Row = mysql_fetch_object( $result ) )
{
print "<person><id>".$Row->keyword."</id><name>".$Row->url."</name></person>\n";
}
print "</xmlFor>";

This is the xml i trying to print, but it does not get