mmm he utilizado el metodo mediante SQL y esto es lo que me a salido, me gustaria saber si e metido la pata o estoy por el buen camino.
<?php $dbh = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('sensei_blog', $dbh); $start = (empty($_REQUEST["start"]) ? 0 : ($_REQUEST["start"])); $section = (empty($_REQUEST["seccion"]) ? principal : ($_REQUEST["seccion"])); $not = (empty($_REQUEST["noticia"]) ? false : ($_REQUEST["noticia"])); $end = 5;if( $not == false){ $res = mysql_query("SELECT title, SUBSTRING(entrada,0,100) FROM blog WHERE seccion='".$section."' ORDER BY id DESC LIMIT ".$start.",".$end."", $dbh) or die('Error select_blog: '.mysql_error());}else{ $res = mysql_query("SELECT title, entrada FROM blog WHERE title ='".$title."' ORDER BY id DESC LIMIT ".$start.",".$end."", $dbh) or die('Error select_blog: '.mysql_error());} $n = mysql_num_rows($res); echo '<table>'; while($row = mysql_fetch_array($res)) { echo' <a href=/?noticia='.$row['title'].'> <table> <tr> <td> '.$row['title'].'</td> <td> '.$row['entrada'].'</td> </tr> </table> </a>'; } if($start > 0) { echo '<div style="float:left"> <a href="?start='.($start - 5).'">NOTICIAS RECIENTES</a> </div>'; } if(($start + 5) <= $n) { echo '<div style="align:right"> <a href="?start='.($start + 5).'">NOTICIAS ANTERIORES</a> </div>'; }?>
No te hace falta un
<?php $not = (empty($_REQUEST["noticia"]) ? false : ($_REQUEST["noticia"]));if( $not == false)
Puedes ponerlo directamente como:
<?php if(!empty($_REQUEST["noticia"]))
Supongo que por lo demás estaría bien, aunque no lo he probado.
mmm ahora me devuelve esto:
Notice: Use of undefined constant test - assumed 'test' in /home/sensei/public_html/noticias.php on line 18
Notice: Undefined index: entrada in /home/sensei/public_html/noticias.php on line 42
titulo7
Notice: Undefined index: entrada in /home/sensei/public_html/noticias.php on line 42
titulo6
Notice: Undefined index: entrada in /home/sensei/public_html/noticias.php on line 42
titulo5
Notice: Undefined index: entrada in /home/sensei/public_html/noticias.php on line 42
titulo4
Notice: Undefined index: entrada in /home/sensei/public_html/noticias.php on line 42
titulo3
La linea 18
$section = (empty($_REQUEST["seccion"]) ? test : ($_REQUEST["seccion"]));
La linea 42
<td> '.$row['entrada'].'</td>
a ver que es jeje gracias por la ayuda ;)
EDIT:
edito puesto que solucione los errores, el codigo ya mas o menos cuidado es:
<?phperror_reporting(E_ALL); $dbh = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('sensei_blog', $dbh); $start = (empty($_REQUEST["start"]) ? 0 : ($_REQUEST["start"])); $section = (empty($_REQUEST["seccion"]) ? $section=test : ($_REQUEST["seccion"])); $not = (empty($_REQUEST["noticia"]) ? false : ($_REQUEST["noticia"])); $end = 5;if( $not == false){ $res = mysql_query("SELECT title, SUBSTRING(entrada,0,100) AS entrada FROM blog WHERE seccion='".$section."' ORDER BY id DESC LIMIT ".$start.",".$end."") or die('Error select_blog: '.mysql_error());}else{ $res = mysql_query("SELECT title, entrada FROM blog WHERE title ='".$title."' ORDER BY id DESC LIMIT ".$start.",".$end."") or die('Error select_blog: '.mysql_error());} $n = mysql_num_rows($res); while($row = mysql_fetch_array($res)) { echo' <a href=/?noticia='.$row['title'].'> <table> <tr> <td> '.$row['title'].'</td> <td>'.$row['entrada'].'</td> </tr> </table> </a>'; } if($start > 0) { echo '<div style="float:left"> <a href="?start='.($start - 5).'">NOTICIAS RECIENTES</a> </div>'; } if(($start + 5) <= $n) { echo '<div style="align:right"> <a href="?start='.($start + 5).'">NOTICIAS ANTERIORES</a> </div>'; }?>
El AS me dijeron que lo pusiera y navegue, e llegado a la conclusión quizá errónea que lo que hace es exportar ahí la nueva variable, el resultado eliminado. En cualquier caso sigo utilizando mal el SUBSTRING y por mas que e buscado no encuentro la solución, en todos los manuales viene lo mismo, y creo que es lo que hago no entiendo que hago mal. A ver si alguien ve el maldito error.
Prefiero hacerlo con SQL aun que sea quizá algo mas difícil o me este costando mas trabajo ya que mediante php es menos eficiente ya que recoge la cadena entera del sql para luego solo utilizar parte.
Bueno, sude sangre pero hice el programa entero de nuevo, FUNCIONA!!! pero... jeje siempre hay un pero nunca sale todo bien en este caso es la parte del else, en ella mi intención es que aparezca únicamente la noticia en la que se a pinchado para verla entera pero no funciona.
<?php $dbh = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('sensei_blog', $dbh); $start = (empty($_REQUEST["start"]) ? 0 : ($_REQUEST["start"])); $section = (empty($_REQUEST["seccion"]) ? test : ($_REQUEST["seccion"])); $post = (empty($_REQUEST["entrada"]) ? false : ($_REQUEST["entrada"])); $end = 5; if($post == false){ $res = mysql_query("SELECT entrada, title FROM blog WHERE seccion='".$section."' ORDER BY id DESC LIMIT ".$start.",".$end."", $dbh) or die('Error select_blog: '.mysql_error()); $n = mysql_num_rows($res); while($row = mysql_fetch_array($res)) { echo'<div style="color:white;"> <a href="?entrada='.$row['title'].'"> <table> <tr> <td>'.$row['title'].' </td> <td>'.nl2br(substr($row['entrada'],0,100)).'</td> </tr> </table> </a> </div>'; } } else { $res = mysql_query("SELECT entrada, title, autor FROM blog WHERE entrada='".$post."' ORDER BY id DESC LIMIT ".$start.",".$end."", $dbh) or die('Error select_blog: '.mysql_error()); $n = mysql_num_rows($res); while($row = mysql_fetch_array($res)) { echo' <div align="center" style="color:white;"> <span style="font-size:20pt;font-weight:bold;">'.$row['title'].'</span> <br/> <br/> <div>'.$row['entrada'].'</div> </div> <span align="right" style="color:white;>'.$row['autor'].'</span>'; } } if($start > 0) { echo '<div align="left"> <a href="?start='.($start - 5).'">ENTRADAS RECIENTES</a> </div>'; } if(($start + 5) <= $n) { echo '<div align="rigth"> <a href="?start='.($start + 5).'">ENTRADAS ANTERIORES</a> </div>'; }?>
Me decidí a hacerlo mediante php en vez SQL para evitarme el error. En cualquier caso que opináis del método, ¿lo hubieseis hecho de forma distinta? Y sobre el error en la segunda pagina, ¿a que se debe? ¿Alguna idea?
P.D: como noto la experiencia a la hora de escribir el código, no tengo mucha aun pero la fluidez que he ganado en este tiempo me resulta increíble jeje
Zant gracias, pero me pierdo en ese código y no saco nada en claro xD yo estoy empezando también y en cuanto se complica un poco el tema me resulta imposible seguirlo.
Ya solucione el problema, al parecer me faltaba un ; y tenia mal alguna cosita mas. Con ayuda de un amigo mediante Gtalk conseguí arreglarlo.
<?php $dbh = mysql_connect('localhost', 'user', 'pass'); mysql_select_db('sensei_blog', $dbh); $start = (empty($_REQUEST["start"]) ? 0 : ($_REQUEST["start"])); $section = (empty($_REQUEST["seccion"]) ? $section="test" : ($_REQUEST["seccion"])); $post = (empty($_REQUEST["entrada"]) ? false : ($_REQUEST["entrada"])); $end = 5; if($post == false){ $res = mysql_query("SELECT entrada, title FROM blog WHERE seccion='".$section."' ORDER BY id DESC LIMIT ".$start.",".$end."", $dbh) or die('Error select_blog: '.mysql_error()); $n = mysql_num_rows($res); while($row = mysql_fetch_array($res)) { echo'<div> <a href="?entrada='.$row['title'].'"> <table style="color:white;"> <tr> <td style="font:20pt bold;" >'.$row['title'].' </td> <td style="font:12pt"><br/>'.nl2br(substr($row['entrada'],0,100)).'</td> </tr> </table> </a> </div>'; } } else { $res = mysql_query("SELECT entrada, title, autor FROM blog WHERE title='".$post."'") or die('Error select_blog: '.mysql_error()); if (mysql_num_rows($res) == 1){ $row = mysql_fetch_array($res); echo'<div align="center" style="color:white;"> <span style="font:20pt bold;">'.$row['title'].'</span> <br/> <br/> <div>'.nl2br($row['entrada']).'</div> </div> <span align="right" style="color:white;">'.$row['autor'].'</span>'; }else{ echo "No se encuentra esta una noticia con este titulo"; } } if($start > 0) { echo '<div align="left"> <a href="?start='.($start - 5).'">ENTRADAS RECIENTES</a> </div>'; } if(($start + 5) <= $n) { echo '<div align="right"> <a href="?start='.($start + 5).'">ENTRADAS ANTERIORES</a> </div>'; }?>
También e añadido un comprobante para en caso de que no exista la noticia salta un mensaje de error.
Quizá en estos días lo modifique y muestre las entradas mediante el id, me a comentado mi amigo que seria mejor para ahorrarme posibles problemas con los caracteres especiales.
Por fin alcance la solución jeje por fin!