4 Replies - 165 Views - Last Post: 15 March 2014 - 06:58 AM Rate Topic: -----

#1 chris98  Icon User is online

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 731
  • Joined: 06-July 13

write to XML file

Posted 15 March 2014 - 02:28 AM

OK, I've been looking into the w3schools tutorial on AJAX live search, and I must admit it looks pretty impressive. But, there is one issue. I am at the stage where I am constantly updating my site, and I would need to write the data to the xml file through PHP because I wouldn't want to manually update it all the time. I have created the following script below to write to the file, and have made the necessary changes to my search code. But, there is one problem. It will write the data, but when I go to the URL, it doesn't show it as XML code: http://www.stronghol...co.uk/links.xml

The idea is, whenever I want the content updating, I will just visit this file below:

<?php
require_once('/home/directories/include.php');

function create_file($filename,$filecontent){
	if (strlen($filename)>0){
		$file = @fopen($filename,"w");
		if ($file != false){
			fwrite($file,$filecontent);
			fclose($file);
			return 1;
		}
		return -2;
	}
	return -1;
}

echo "Processing... 
<br />";

if (file_exists('links.xml'))
{
	unlink('links.xml');
	echo "Old File Removed. Attempting to create new file...
	<br />";
}

echo "File does not yet exist. Attempting to create file...
<br />";
$filename = 'links.xml';


if (create_file($filename, $header) == 1)
{
	echo "Blank file was created...<br />";
} 
else 
{
	exit('Error creating file.');
}
        
$file = fopen('links.xml',"w");

$header = '<?xml version="1.0" encoding="utf-8"?>
<pages>';
fwrite($file, $header);

$sql = "SELECT title, id FROM `pages` WHERE sub_page != 'sub_sub_page' ORDER BY id DESC";
$ps = $shn_sites->prepare($sql);
$ps->execute();
foreach ($ps as $article) 
{
	$link = replace_characters($article['title']);
	$url = 'http://www.strongholdnation.co.uk/articles/'.$article['id'].'/'.$link.'';
	
	$fwrite = '
	<link>
<title>'.$article['title'].'</title>
<url>'.$url.'</url>
</link>
<link>
	';
	
	fwrite($file, $fwrite);
}

$footer = '
</pages>';	
	
fwrite($file, $footer);

echo "All info written to file succesfully. The connection will now close.
<br />";

fclose($file);

echo "Connection closed.";


This post has been edited by chris98: 15 March 2014 - 02:40 AM


Is This A Good Question/Topic? 0
  • +

Replies To: write to XML file

#2 chris98  Icon User is online

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 731
  • Joined: 06-July 13

Re: write to XML file

Posted 15 March 2014 - 02:49 AM

I have also placed the onkeyup on the form rather than the search input. I changed it, and now get this error due to the XML I think:

Quote

Warning: DOMDocument::load() [domdocument.load]: Premature end of data in tag link line 247 in /home/*******/links.xml, line: 357 in /home/****/livesearch.php on line 3

This post has been edited by chris98: 15 March 2014 - 02:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,023
  • Joined: 08-June 10

Re: write to XML file

Posted 15 March 2014 - 05:58 AM

Quote

now get this error due to the XML

that’s correct, you are having twice as much <link> as you have </link>, invalidating your XML.
Was This Post Helpful? 0
  • +
  • -

#4 chris98  Icon User is online

  • D.I.C Addict

Reputation: 15
  • View blog
  • Posts: 731
  • Joined: 06-July 13

Re: write to XML file

Posted 15 March 2014 - 06:20 AM

Thanks, that's all it was. It will show the suggestions below, but do you know how I could get it to display results similar to Google when it shows a hint you can select to search?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,023
  • Joined: 08-June 10

Re: write to XML file

Posted 15 March 2014 - 06:58 AM

for that I’d always rely on a library, e.g. Twitter’s Typeahead.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1