Foros del Web » Programando para Internet » PHP »

While

Estas en el tema de While en el foro de PHP en Foros del Web. Hola todos Hago una búsqueda en una tabla para listar estadísticas. Código PHP: $sql1 = "SELECT title, SUM(clicks) AS SUCLICKS, COUNT(id) AS NUMBER, permalink FROM most_viewed WHERE clicked_date>='$time_now1' AND clicked_date<='$time_now2' GROUP BY title ORDER BY SUCLICKS desc LIMIT 0, 5"; $result1 = mysql_query($sql1, $con) or die("error en consulta:".mysql_error()); if ($row= mysql_fetch_array($result1)) { DO { // Print out result $title=$row["title"]; $title=addslashes($title); $times=$row["SUCLICKS"]; $perma=$row['permalink']; //Hasta aquí todo bien pero necesito hacer otra búsqeda en otra tabla para //obtener el campo feed_id que le corresponde al row['$perma'], entonces sigo... ...
  #1 (permalink)  
Antiguo 11/12/2008, 17:26
(Desactivado)
 
Fecha de Ingreso: septiembre-2004
Mensajes: 360
Antigüedad: 19 años, 7 meses
Puntos: 1
While

Hola todos

Hago una búsqueda en una tabla para listar estadísticas.

Código PHP:
$sql1 = "SELECT title, SUM(clicks) AS SUCLICKS, COUNT(id) AS NUMBER, permalink FROM most_viewed WHERE clicked_date>='$time_now1' AND clicked_date<='$time_now2' GROUP BY title ORDER BY SUCLICKS desc LIMIT 0, 5";

$result1 = mysql_query($sql1, $con) or die("error en consulta:".mysql_error());

if ($row= mysql_fetch_array($result1))
{

DO {
// Print out result
$title=$row["title"];
$title=addslashes($title);
$times=$row["SUCLICKS"];
$perma=$row['permalink'];


//Hasta aquí todo bien pero necesito hacer otra búsqeda en otra tabla para
//obtener el campo feed_id que le corresponde al row['$perma'], entonces sigo...

$sql3 = "SELECT feed_id FROM articles WHERE permalink='$perma' ";

$result3 = mysql_query($sql3, $con) or die("error en consulta:".mysql_error());
if ($row= mysql_fetch_array($result3))
{

  $v=$row['feed_id'];
  echo $v; //----> no logro hacer el loop, siempre me sale el mismo :(
?>

<tr>
                  <td bgcolor="#008080" width="80%"><font color="#FFFFFF" face="Verdana" size="1"><a style="color: #FFFFFF; font-family: Verdana; font-size: 10px" href="page.php?v=<?=$v?>" target="_blank"><b><?=$title?></b><br></font></td>
                  <td bgcolor="#008080" width="5%"><font color="#FFFFFF" face="Verdana" size="1"><b><?=$times?></b><br></font></td></tr>
<?
}
}
while(
$row mysql_fetch_array($result1));
}

else
{
?>
<tr>
                  <td bgcolor="#008080" colspan="2" width="100%" align="center"><font color="#FFFFFF" face="Verdana" size="1"><b>No Stats</b><br></font></td>
                  </tr>
<?
}
Y ahí estoy trabada, no logro el loop ese.
Alguien se da cuenta qué hago mal ?
  #2 (permalink)  
Antiguo 11/12/2008, 17:50
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: While

Cambia:
if ($row= mysql_fetch_array($result1))

Por:
while ($row= mysql_fetch_array($result1))
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 11/12/2008, 18:21
(Desactivado)
 
Fecha de Ingreso: septiembre-2004
Mensajes: 360
Antigüedad: 19 años, 7 meses
Puntos: 1
Respuesta: While

Gracias !
pero me dice...

Parse error: syntax error, unexpected T_ELSE on line 79, es decir... ahí donde tengo

}
else
{
....

Editando:
lo corregí agregando
if (!empty($sql)) {

pero sigue sin hacer el loop :(
  #4 (permalink)  
Antiguo 11/12/2008, 18:38
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: While

Ok, la estructura deberia ser mas o menos asi:

Código:
$sql = consulta a base de datos
$resultado = ejecutar consulta
$nostats = true; // Crear variable para saber si se mostro algo
while($row = leer linea por linea segun resultado) {
    Mostrar contenido
    $sql3 = nueva consulta
    $resultado3 = ejecutar consulta
    while($row3 = leer linea por linea segun resultado3) {
        Mostrar contenido
    }
    $nostats = false;  // Si se mostro algo
}
if($nostats) {
    Poner mensaje de que no hay informacion para mostrar
}
__________________
- León, Guanajuato
- GV-Foto
  #5 (permalink)  
Antiguo 11/12/2008, 18:44
(Desactivado)
 
Fecha de Ingreso: septiembre-2004
Mensajes: 360
Antigüedad: 19 años, 7 meses
Puntos: 1
Respuesta: While

Muchísimas gracias !
  #6 (permalink)  
Antiguo 11/12/2008, 18:52
 
Fecha de Ingreso: abril-2006
Mensajes: 1.128
Antigüedad: 18 años
Puntos: 33
Respuesta: While

Suyta:
Prueba algo asi ->
while($row= mysql_fetch_array($result3))
{
$v=$row['feed_id'];
echo $v; //----> no logro hacer el loop, siempre me sale el mismo :(
}
Saludos
Franco
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:58.