Solo mostrar el mas reciente en while php

  • Autor Autor satanson
  • Fecha de inicio Fecha de inicio
satanson

satanson

Gamma
Verificado
Verificación en dos pasos activada
Verificado por Whatsapp
¡Usuario con pocos negocios! ¡Utiliza siempre saldo de Forobeta!
Hola betas, tengo una duda. Como pueden ver en la imagen -> https://prnt.sc/jBoojWWojttA si 2 personas comentan aparecen en la lista, como puedo hacer para que aparezca solo 1 en vez de 2 o más. Mas abajo les dejo el codigo de la consulta:

PHP:
$query3 = $db->query("
            SELECT dd_threads.tid,dd_threads.uid,dd_threads.views,dd_threads.replies,dd_users.uid,dd_users.avatar,dd_posts.username,dd_posts.pid,dd_posts.dateline, dd_posts.replyto,dd_posts.fid,dd_forums.fid,dd_forums.name,dd_posts.message,dd_posts.subject,dd_posts.tid FROM dd_users,dd_posts,dd_forums,dd_threads
            where
            dd_posts.fid = dd_forums.fid
            and
            dd_users.uid = dd_posts.uid
            and
            dd_threads.tid = dd_posts.tid
            and          
            dd_posts.replyto >= 0
            and
            dd_posts.visible > 0
            and
            dd_forums.lastposttid != ''
            ORDER BY dd_posts.pid DESC LIMIT 10
           
        ");


        while($fila = $db->fetch_array($query3))
        {
            $id = $fila['tid'];          
            $msg = $fila['message'];
            if($fila['avatar'] != ''){
            $ava = $fila['avatar'];
            }else{
            $ava = 'images/default_avatar.jpg';
            }
            $replies = $fila['replies'];
            $views = $fila['views'];
            $uid = $fila['uid'];
            $title = $fila['subject'];
            $title = str_replace('RE: ', '', $title);
            $fname = $fila['name'];
            $fid = $fila['fid'];
            $output = preg_match_all('/([-a-z0-9_\/:.]+\.(jpg|jpeg|png|webp))/i', $msg, $matches);
            //$date$date = my_date($mybb->settings['dateformat'], $fila['dateline'], '', false);
            $date = my_date("relative", $fila['dateline']);
            $user = $fila['username'];
            $i++;
            if($fila['replyto']>0){$laspost='&action=lastpost';}else{
                $laspost='';
            }
            $img .= "<tr><td class='trow1' style='width: 5px;overflow:visible;padding:5px;text-align:center;'>".$i."</td><td class='trow1' style='width: 25px;padding:5px;'>
            <div style='float:left;position:relative;overflow:show'><center><a href='member.php?action=profile&uid=".$uid."' title='Perfil de ".$user."'><img src=".$ava." style='max-height: 25px;max-width:25px;'></a></center></div>
            </td>
            <td class='trow1'>
            <div class='title'><a href='showthread.php?tid=".$id."".$laspost."' title='".$title."'>".$title."</a></div>
            </td>
            <td class='trow1' style='padding:5px;text-align:center;'>
            <div class='foro'><a href='forumdisplay.php?fid=".$fid."' title='".$fname."'>".$fname."</a></div>
            </td>
            <td class='trow1' style='padding:5px;text-align:center;'>
            ".$replies."
            </td>
            <td class='trow1' style='padding:5px;text-align:center;'>
            ".$views."
            </td>          
            <td class='trow1' style='padding:5px;text-align:center;'>
            <div class='fecha'>".$date."</div>
            </td>
            <td class='trow1' style='padding:5px;text-align:center;'>
            <div class='usuario'><a href='member.php?action=profile&uid=".$uid."' title='Perfil de ".$user."'>".$user."</a></div>
            </td>          
            </tr>
                    ";  
        }
 
Que tal!
Creo que lo unico que debiste compartir fue la consulta SQL ya que eso debe ser modificado.

Tu consulta:
Insertar CODE, HTML o PHP:
$query3 = $db->query("
            SELECT dd_threads.tid,dd_threads.uid,dd_threads.views,dd_threads.replies,dd_users.uid,dd_users.avatar,dd_posts.username,dd_posts.pid,dd_posts.dateline, dd_posts.replyto,dd_posts.fid,dd_forums.fid,dd_forums.name,dd_posts.message,dd_posts.subject,dd_posts.tid FROM dd_users,dd_posts,dd_forums,dd_threads
            where
            dd_posts.fid = dd_forums.fid
            and
            dd_users.uid = dd_posts.uid
            and
            dd_threads.tid = dd_posts.tid
            and         
            dd_posts.replyto >= 0
            and
            dd_posts.visible > 0
            and
            dd_forums.lastposttid != ''
            ORDER BY dd_posts.pid DESC LIMIT 10
          
        ");

No he compilado esta consulta pero podrias probar (he agregado unicamente un group by):

Insertar CODE, HTML o PHP:
$query3 = $db->query("
            SELECT  dd_threads.tid,dd_threads.uid,dd_threads.views,dd_threads.replies,dd_users.uid,dd_users.avatar,dd_posts.username,dd_posts.pid,dd_posts.dateline, dd_posts.replyto,dd_posts.fid,dd_forums.fid,dd_forums.name,dd_posts.message,dd_posts.subject,dd_posts.tid FROM dd_users,dd_posts,dd_forums,dd_threads
            where
            dd_posts.fid = dd_forums.fid
            and
            dd_users.uid = dd_posts.uid
            and
            dd_threads.tid = dd_posts.tid
            and         
            dd_posts.replyto >= 0
            and
            dd_posts.visible > 0
            and
            dd_forums.lastposttid != ''
            GROUP BY dd_users.uid
            ORDER BY dd_posts.pid DESC LIMIT 10
          
        ");

Si no te funciona podrias intentar utilizar "DISTINCT" aunque para esto es posible que haga doble consulta SQL lo cual no es recomendable a menos que alguien que tenga experiencia con base de datos te brinde una mejor consulta.
 
Que tal!
Creo que lo unico que debiste compartir fue la consulta SQL ya que eso debe ser modificado.

Tu consulta:
Insertar CODE, HTML o PHP:
$query3 = $db->query("
            SELECT dd_threads.tid,dd_threads.uid,dd_threads.views,dd_threads.replies,dd_users.uid,dd_users.avatar,dd_posts.username,dd_posts.pid,dd_posts.dateline, dd_posts.replyto,dd_posts.fid,dd_forums.fid,dd_forums.name,dd_posts.message,dd_posts.subject,dd_posts.tid FROM dd_users,dd_posts,dd_forums,dd_threads
            where
            dd_posts.fid = dd_forums.fid
            and
            dd_users.uid = dd_posts.uid
            and
            dd_threads.tid = dd_posts.tid
            and        
            dd_posts.replyto >= 0
            and
            dd_posts.visible > 0
            and
            dd_forums.lastposttid != ''
            ORDER BY dd_posts.pid DESC LIMIT 10
         
        ");

No he compilado esta consulta pero podrias probar (he agregado unicamente un group by):

Insertar CODE, HTML o PHP:
$query3 = $db->query("
            SELECT  dd_threads.tid,dd_threads.uid,dd_threads.views,dd_threads.replies,dd_users.uid,dd_users.avatar,dd_posts.username,dd_posts.pid,dd_posts.dateline, dd_posts.replyto,dd_posts.fid,dd_forums.fid,dd_forums.name,dd_posts.message,dd_posts.subject,dd_posts.tid FROM dd_users,dd_posts,dd_forums,dd_threads
            where
            dd_posts.fid = dd_forums.fid
            and
            dd_users.uid = dd_posts.uid
            and
            dd_threads.tid = dd_posts.tid
            and        
            dd_posts.replyto >= 0
            and
            dd_posts.visible > 0
            and
            dd_forums.lastposttid != ''
            GROUP BY dd_users.uid
            ORDER BY dd_posts.pid DESC LIMIT 10
         
        ");

Si no te funciona podrias intentar utilizar "DISTINCT" aunque para esto es posible que haga doble consulta SQL lo cual no es recomendable a menos que alguien que tenga experiencia con base de datos te brinde una mejor consulta.
Si coloco "GROUP BY dd_users.uid" se cae la web -> https://prnt.sc/cgOUQo9u1XhO y con distinct no funciona. alguna otra idea?
 
Guárdalo en una matriz y no recuerdo cual pero hay una función para eliminar los duplicados en la matriz
 
Por qué no hiciste JOIN en vez de esa pila de ANDs
 
Puedes probar esto:
SQL:
            SELECT DISTINCT dd_threads.tid,dd_threads.uid,dd_threads.views,dd_threads.replies,dd_users.uid,dd_users.avatar,dd_posts.username,dd_posts.pid,dd_posts.dateline, dd_posts.replyto,dd_posts.fid,dd_forums.fid,dd_forums.name,dd_posts.message,dd_posts.subject,dd_posts.tid FROM dd_users,dd_posts,dd_forums,dd_threads
            where
            dd_posts.fid = dd_forums.fid
            and
            dd_users.uid = dd_posts.uid
            and
            dd_threads.tid = dd_posts.tid
            and         
            dd_posts.replyto >= 0
            and
            dd_posts.visible > 0
            and
            dd_forums.lastposttid != ''
            GROUP BY dd_threads.tid
            ORDER BY dd_posts.pid DESC LIMIT 10
 
Última edición por un moderador:
Puedes probar esto:
SQL:
            SELECT DISTINCT dd_threads.tid,dd_threads.uid,dd_threads.views,dd_threads.replies,dd_users.uid,dd_users.avatar,dd_posts.username,dd_posts.pid,dd_posts.dateline, dd_posts.replyto,dd_posts.fid,dd_forums.fid,dd_forums.name,dd_posts.message,dd_posts.subject,dd_posts.tid FROM dd_users,dd_posts,dd_forums,dd_threads
            where
            dd_posts.fid = dd_forums.fid
            and
            dd_users.uid = dd_posts.uid
            and
            dd_threads.tid = dd_posts.tid
            and        
            dd_posts.replyto >= 0
            and
            dd_posts.visible > 0
            and
            dd_forums.lastposttid != ''
            GROUP BY dd_threads.tid
            ORDER BY dd_posts.pid DESC LIMIT 10
No bro, no funciona sigue repitiendo los mismos post
 
alguna otra idea? yo tengo una idea pero no se como hacerlo, mi idea es: no se puede hacer que la lista almacene el ID del post y que si ya esta en la lista, solo muestre el la version mas reciente. Yo creo que funcionaria.
 
debes ocupar DISTINCT e INNER JOIN ya que solo te mostrará aquellos archivos que se encuentren en la tabla secundaría. compárteme la estructura de las tablas y datos para hacer pruebas
 
debes ocupar DISTINCT e INNER JOIN ya que solo te mostrará aquellos archivos que se encuentren en la tabla secundaría. compárteme la estructura de las tablas y datos para hacer pruebas
Es la base datos de mybb, solo saque los datos que quiero. Solo tendrias que instalar mybb en local y ya tendrias todo. Gracias por tu ayuda, se agradece.
 
Mientras mantengas los datos del usuario y avatar en tu select, te va a seguir mostrando, doble, lo que puedes hacer es recorrer tu consulta como ya lo haces e ir guardando en un array, a fin de antes de "agregar" al array, busca primeramente si ya no existe un post con el mismo id, si existe haz "continue" y no le hará caso al resto, seguira iterando hasta que termine, pero no te guardará el duplicado
 
Atrás
Arriba