Home>

The rss of the blog (livedoor blog) is output to html with ajax (+ php).
After reading with php, it outputs with ajax.
At present, I would like to make 5 places where 10 cases are displayed without restriction.

Error message

I controlled it with ajax or php, I didn't have any knowledge about controlling it, but I tried various things for about 3 days.
I could not do it.

Applicable source code
$(function () {
$.ajax ({
  url: 'common/js/rss.php', // contents to be included in rss.php
  xmlType: 'xml', // This is not dataType but xmlType
  cache: false,
  success: function (xml) {
      var row = 0;
      var data = [];
      var nodeName;
      $(xml) .find ('item'). each (function () {
        data [row] = {};
        $(this) .children (). each (function () {// get child elements
            nodeName = $(this) [0] .nodeName;// element name
            data [row] [nodeName] = {};// initialization
            attributes = $(this) [0] .attributes;
            for (var i in attributes) {
            data [row] [nodeName] [attributes [i] .name] = attributes [i] .value;// attribute name = value
            }
          data [row] [nodeName] ['text'] = $(this) .text ();

        });

          function dateFormat (date) {// Function used for date formatting (format in year, month, month, day)
            var y = date.getFullYear ();
            var m = date.getMonth () + 1;
            var d = date.getDate ();
            var w = date.getDay ();
            m = ('0' + m) .slice (-2);
            d = ('0' + d) .slice (-2);
            return y + '.' + m + '.' + d + '';
            }
          $(this) .find ('date, dc \\: date'). each (function () {
                var pubtime = $(this) .text ();
                pubtime = new Date (pubtime);
                data [row] .pubDate = dateFormat (pubtime);
            });
        row ++;
      });

      for (i in data) {
          // output date, title, link
          $('# feed'). append ('<dt>' + data [i] .pubDate + '</dt><dd><a href = "' + data [i] .link.text + '"target =" _ blank ">' + data [i] .title.text + '</a></dd>');
      }
  }
});
});
<? php
$xml = file_get_contents ($url);
header ("Content-type: application/xml;charset = UTF-8");
print $xml;
?>
Tried
  • Answer # 1

    if (parseint (i)>= 5) break;


    It seems thatiis a character string.
    If you compare it withparseint (), it will move.

  • Answer # 2

    for (i in data) {
      if (i === 5) break;
      $('# feed'). append ('<dt>' + data [i] .pubDate + '</dt><dd><a href = "' + data [i] .link.text + '"target =" _ blank ">' + data [i] .title.text + '</a></dd>');
    }