XML CMS - displaying the results based on a number

Hello

Like many others I’ve been creating a simple PHP/XML CMS with a wonderful tutorial on this site. Essentially it’s a ‘latest news’ page. The stories display on the web page with the most recent item created at the top, but I want to be able to ‘re-shuffle’ the stories based on a number I add to an XML node.
I’m sure there must be a way with PHP to order the results based on a number in one of the nodes? For example:


<?xml version="1.0"?>
<article id="March">
  <headline>The Headline</headline>
  <rank>1</rank>
  <email></email>
  <abstract>A brief description here.</abstract>
  <keywords></keywords>
  <status>live</status>
  <para-intro>Some text here.
</para-intro>
</article>

In the above example, because it has been given the rank ‘1’, I would like it to display first on the web page no matter when it was created. Other stories with higher numbers would appear below it. Does that make sense?!

Here is the php code that extracts the info and displays it on the web page:

<?php
$dh = opendir('./xml/');

$fileCount = 0;
while ($file = readdir($dh) and $fileCount < 40){
	if (eregi("^..?$", $file)) {
		continue;
	}
	$open = "./xml/".$file;
	$xml = domxml_open_file($open);

	//we need to pull out all the things from this file that we will need to 
	//build our links
	$root = $xml->root();
	$stat_array = $root->get_elements_by_tagname("status");
	$status = extractText($stat_array);
	
	$ab_array = $root->get_elements_by_tagname("abstract");
	$abstract = extractText($ab_array);

	$h_array = $root->get_elements_by_tagname("headline");
	$headline = extractText($h_array);

	if ($status != "live"){
		continue;
	}
	echo "<tr><td>";
	echo stripslashes ("<a href=\\"showArticle.php?file=".$file . "\\">".$headline . "</a><br>");
	echo stripslashes ("$abstract");
	echo "<p>&nbsp;</p></td></tr>";
	
	$fileCount++;
}
?>

Any help or advice would be much appreciated!

I think I can live with that. There won’t be hundreds of xml files… probably no more than about 20 at a time.

I reckon this is all working brilliantly now and I really can’t thank you enough for your help.

StarLion you are a damned decent human being!!!

Hang on a minute… I think it’s working!!

I’ll keep testing and advise…

That’s incredibly generous StarLion - thank you!!!

I’ve read through it all and I think I see what the code is getting at. However, for some reason it’s only returning one search result (there are currently seven xml pages).

Do you have any idea why that should be?

Many thanks again!

It wont delete anything, but you wont see records from the overwritten values.


$a[0] = 3;
$a[1] = 2;
$a[0] = 6;
echo $a[0]; //6

Do you mean it would delete the first xml file completely? Oh dear.

If the files have rank numbers that collide, they will overwrite each other. If that’s the case, a bit more in-depth work will need to occur.

Just because I’ve got a bit of downtime at work today…

Completely untested and firing off the top of my head.


<?php
$dh = opendir('./xml/');

$fileCount = 0;
while ($file = readdir($dh) and $fileCount < 40){
    if (eregi("^..?$", $file)) {
        continue;
    }
    $open = "./xml/".$file;
    $xml = domxml_open_file($open);

    //we need to pull out all the things from this file that we will need to 
    //build our links
    
    $root = $xml->root();
    $stat_array = $root->get_elements_by_tagname("status");
    $status = extractText($stat_array);
    
    $ab_array = $root->get_elements_by_tagname("abstract");
    $abstract = extractText($ab_array);

    $h_array = $root->get_elements_by_tagname("headline");
    $headline = extractText($h_array);

    $h_array = $root->get_elements_by_tagname("rank");
    $rank = extractText($h_array);

    if ($status != "live"){
        continue;
    }
    $outfile[$rank] = array('fname' => $file, 'headline' => $headline, 'abstract' => $abstract);
    $fileCount++;
}
ksort($outfile);
foreach($outfile AS $file) {
    echo "<tr><td>".stripslashes("<a href=\\"showArticle.php?file=".$file['fname']."\\">".$file['headline'] ."</a><br>".$file['abstract'])."<p>&nbsp;</p></td></tr>";    
}
?>

Thanks for the reply StarLion.

I’m a real beginner at this…

Do I need to write an array function just before the echo code? Then place the ‘sort’ command after that but before the ‘echos’?

Sorry if I sound stupid!

Dont echo the items immediately - load them into an array, then sort it, then output the array.

What your script is doing now is:
for Each Item:
Extract Values
Output HTML
NextItem

What you want it to do is:
foreach Item:
Extract Values.
Store Values in array.
NextItem
Sort array
Foreach arrayitem:
output item
NextArrayItem

OK, well I’ve been doing some research and it looks like I need to use the php ‘sort’ function but I can’t get it to work.

The stories are being displayed I think according to Unix Epoch time based on when the story was created. I can’t seem to overide it at all.

Here’s the code that displays the news stories on the web page, can anybody tell me how it’s deciding to display by the timestamp?

<?php
$dh = opendir('./xml/');

$fileCount = 0;
while ($file = readdir($dh) and $fileCount < 40){
	if (eregi("^..?$", $file)) {
		continue;
	}
	$open = "./xml/".$file;
	$xml = domxml_open_file($open);

	//we need to pull out all the things from this file that we will need to 
	//build our links
	
	$root = $xml->root();
	$stat_array = $root->get_elements_by_tagname("status");
	$status = extractText($stat_array);
	
	$ab_array = $root->get_elements_by_tagname("abstract");
	$abstract = extractText($ab_array);

	$h_array = $root->get_elements_by_tagname("headline");
	$headline = extractText($h_array);

	if ($status != "live"){
		continue;
	}
	sort($ab_array);
	echo "<tr><td>";
	echo stripslashes ("<a href=\\"showArticle.php?file=".$file . "\\">".$headline . "</a><br>");
	echo stripslashes ("$abstract");
	echo "<p>&nbsp;</p></td></tr>";
	
	$fileCount++;
}
?>

I’m hoping that if I can work out where the script is saying ‘Display stories by datestamp’ I’ll be able to stop it!!

Thanks in advance