{"id":115,"date":"2016-06-09T08:54:43","date_gmt":"2016-06-09T08:54:43","guid":{"rendered":"https:\/\/blog.jkanework.com\/post.php?idpost=115"},"modified":"2016-06-09T08:54:43","modified_gmt":"2016-06-09T08:54:43","slug":"web-php-mostrar-ficheros-html-en-orden-tipo-noticias-feed-sin-base-de-datos","status":"publish","type":"post","link":"https:\/\/blog.jkanetwork.com\/?p=115","title":{"rendered":"[Web PHP] Mostrar ficheros html en orden (tipo noticias feed) sin base de datos"},"content":{"rendered":"<p>Buenas lectores de JKANetwork, hoy contin&uacute;o con la serie de tutoriales cortos, esta vez con PHP<\/p>\n<p>Estoy seguro de que alguna vez hab&eacute;is querido mostrar noticias, u otras cosas, en orden, como si fuese un historial, pero sin tener que editar la web en si, y sin tener que usar una base de datos. Puede que se os haya ocurrido, como yo, hacer una carpeta e ir creando archivos con su contenido, pero luego no hab&eacute;is sabido mostrarlos en orden o bien (Si si que sab&eacute;is, entonces este tutorial no os sirve xd)<\/p>\n<p>Pues aqu&iacute; os voy a pasar un peque&ntilde;o codigo con explicaciones, para que pod&aacute;is hacerlo en vuestro servidor. Los requisitos son tener una carpeta aparte con las noticias\/feed\/lo que sea, y que esten numeradas (siendo 00001.html la mas vieja, y hacia arriba. Pod&eacute;is elegir otra cosa, pero este script las ordena en orden descendente numericamente, y alfabeticamente).<\/p>\n<p>El c&oacute;digo es muy muy sencillo, mirad:<\/p>\n<p><span style=\"color: #800000;\">$arraynews = scandir(&#8216;news\/&#8217;,1);<\/span><br \/><span style=\"color: #800000;\">unset($arraynews[count($arraynews) &#8211; 1]);<\/span><br \/><span style=\"color: #800000;\">unset($arraynews[count($arraynews) &#8211; 1]);<\/span><br \/><span style=\"color: #800000;\">$totalnews = count($arraynews);<\/span><br \/><span style=\"color: #800000;\">for ( $x = 0 ; $x &lt; $totalnews ; $x++){<\/span><br \/><span style=\"color: #800000;\">&nbsp; $texto = file_get_contents(\u00abnews\/\u00bb . $arraynews[$x]);<\/span><br \/><span style=\"color: #800000;\">&nbsp; &nbsp; echo $texto;<\/span><br \/><span style=\"color: #800000;\">&nbsp; &nbsp; echo \u00ab<br \/>\u00ab;<\/span><br \/><span style=\"color: #800000;\">}<\/span><\/p>\n<p><span style=\"color: #000000;\">El c&oacute;digo hace lo siguiente.. La funcion scandir escanea los ficheros del directorio, y el \u00ab1\u00bb hace que los ordene descendentemente. Despu&eacute;s quitamos las dos ultimas posiciones del array (Que son, en Linux, las correspondientes a \u00ab.\u00bb y \u00ab..\u00bb) y despu&eacute;s, hay un for que recorre todos los ficheros (Que estan en html aqui) y los escribe tal cual en la web. &iquest;Ve&iacute;s? Era m&aacute;s sencillo de lo que parec&iacute;a<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Buenas lectores de JKANetwork, hoy contin&uacute;o con la serie de tutoriales cortos, esta vez con PHP Estoy seguro de que alguna vez hab&eacute;is querido mostrar noticias, u otras cosas, en orden, como si fuese un historial, pero sin tener que editar la web en si, y sin tener que usar una base de datos. Puede [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-tutoriales"],"_links":{"self":[{"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=115"}],"version-history":[{"count":0,"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=\/wp\/v2\/posts\/115\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jkanetwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}