SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
    <?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 Code:
    <?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!

  2. #2
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Just because I've got a bit of downtime at work today...

    Completely untested and firing off the top of my head.

    PHP Code:
    <?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>";    
    }
    ?>

  6. #6
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  7. #7
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hang on a minute... I think it's working!!

    I'll keep testing and advise...

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean it would delete the first xml file completely? Oh dear.

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    It wont delete anything, but you wont see records from the overwritten values.

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

  11. #11
    SitePoint Member
    Join Date
    Sep 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •