Jump to content

How to change from shuffle random to sort on array?


legends

Recommended Posts

I have a script and am having difficulty changing the script from a shuffle to a definitive result.  Right now it get the info but always shuffles the information around.  Maybe it too many hours looking at the code.

 

here is the section of the code that i need some help with i need the title, summary and clickurl to load into the $rss_feeds[0], $rss_feeds[1], $rss_feeds[2] in that order instead of always shuffling this around between the three.

 

Any help would be greatly appreciated.

 

  // -------------------------------

  // Display Result

  // -------------------------------

  function display_result()

  {

      global $rss_feeds;

      shuffle($rss_feeds);

      // Number Of Items @page

      $num_result = count($rss_feeds[0]);

      $num_item = ITEM_PER_PAGE;

      if ($num_result < $num_item) {

          $num_item = $num_result;

      }

      // Display

      echo '<hr width="100%" align="left"><div id="yahoo_result">';

      for ($x = 0; $x < $num_item; $x++) {

          echo '<p>' . $rss_feeds[0][$x] . '. ' . $rss_feeds[1][$x] . '</p><a href="'. $rss_feeds[2][$x] .'" target="_blank">Read Full Article</a><br><br><hr width="100%" align="left"><br>';

      }

      echo '</div>';

      return;

  }

  // -------------------------------

  // Get Result

  // -------------------------------

  function get_result($xml_file)

  {

      // Load XML

      $xml = simplexml_load_file($xml_file);

      foreach ($xml->Result as $item) {

          // Title

          $rss_title = cleanup_result($item->Title);

          $rss_title = ucwords($rss_title);

          // Summary

          $rss_summary = cleanup_result($item->Summary);

          $rss_summary = ucwords($rss_summary);

          // ClickUrl

          $rss_clickurl = cleanup_result($item->ClickUrl);

          $rss_clickurl = ucwords($rss_clickurl);

          // RSS Feeds

          $rss_feeds[title][] = $rss_title;

          $rss_feeds[summary][] = $rss_summary;

          $rss_feeds[clickurl][] = $rss_clickurl;

      }

      return $rss_feeds;

  }

looking at your get_results function. Your display function depends on each feed gathered from your get_results to have three parts. What would happen if one of the rss feeds only had two parts or was not in the order you expect?

You must control that each part goes where wanted.

 

 

HTH

TEamatomic

It looks to me like you have a mismatch of assumptions about your array.  When the array is constructed this is what it looks like:

 

$rss_feeds[title] = array();

$rss_feeds[summary] = array();

$rss_feeds[clickurl] = array();

 

Then you start trying to go through the array as if the first element was numeric, which it isn't, it has the 3 keys in:  'title', 'summary', 'clickurl'.

 

Probably what you want in get_results is to remove the 3 lines where you assign the title, summary and url to this:

 

$rss_feeds[] = array('title' => $rss_title,
                                 'summary' => $rss_summary,
                                  'clickurl' => $rss_clickurl,);

 

Shuffle is still going to make the order random, so you need to lose that.


// -------------------------------
  // Display Result
  // -------------------------------
  function display_result()
  {
      global $rss_feeds;
       // Number Of Items @page
      $num_result = count($rss_feeds);
      $num_item = ITEM_PER_PAGE;
      if ($num_result           $num_item = $num_result;
      }
      // Display
      echo '';
      for ($x = 0; $x           echo '
' . $rss_feeds[$x]['title'] . '. ' . $rss_feeds[$x]['summary'] . 'Read Full Article


';
      }
      echo '';
      return;
  }

ok got that figured out had to move [$x] over and all worked well as far as sorting, now it will only show 3 results.  i have it config for 20 results.  any ideas on what happened there?

 

  // -------------------------------

  // Display Result

  // -------------------------------

  function display_result()

  {

      global $rss_feeds;

      // Number Of Items @page

      $num_result = count($rss_feeds);

      $num_item = ITEM_PER_PAGE;

      if ($num_result < $num_item) {

          $num_item = $num_result;

      }

      // Display

      echo '<hr width="100%" align="left"><div id="yahoo_result">';

      for ($x = 0; $x < $num_item; $x++) {

          echo '<p><b>' . $rss_feeds['title'][$x] . '</b><br>' . $rss_feeds['summary'][$x] . '</p><a href="'. $rss_feeds['clickurl'][$x] .'" target="_blank">Read Full Article</a><hr width="100%" align="left">';

      }

      echo '</div>';

      return;

  }

  // -------------------------------

  // Get Result

  // -------------------------------

  function get_result($xml_file)

  {

      // Load XML

      $xml = simplexml_load_file($xml_file);

      foreach ($xml->Result as $item) {

          // Title

          $rss_title = cleanup_result($item->Title);

          $rss_title = ucwords($rss_title);

          // Summary

          $rss_summary = cleanup_result($item->Summary);

          $rss_summary = ucwords($rss_summary);

          // ClickUrl

          $rss_clickurl = cleanup_result($item->ClickUrl);

          $rss_clickurl = ucwords($rss_clickurl);

          // RSS Feeds

          $rss_feeds[title][] = $rss_title;

          $rss_feeds[summary][] = $rss_summary;

          $rss_feeds[clickurl][] = $rss_clickurl;

      }

      return $rss_feeds;

  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.