Código PHP:
SELECT us.*, ps.*,count(ps.idpost) as cantpost, rp.*, count(rp.idreply) as cantreply,
DAYOFMONTH(us.registerdate) AS dia, DAYOFWEEK(us.registerdate) AS semana,
MONTH(us.registerdate) AS mes, YEAR(us.registerdate) AS year,
DATE_FORMAT(us.registerdate,'%h:%i %p') AS hora FROM users us, post ps, reply rp
WHERE ps.idpost=1 AND ps.iduser=us.iduser AND rp.idpost=ps.idpost group by ps.idpost
Cita:
ahora mi tabla usuario consta de dos usuarios con iduser=1 y iduser=2. La tabla post tambien contiene el iduser=1 y la tabla reply tambien contiene el iduser=1. Si todo esto esta así me funciona bien pero ahora si la tabla reply está vacia no me saca los resultados de la tabla POST y usuario.CREATE TABLE `reply` (
`idreply` int(6) unsigned NOT NULL auto_increment,
`idpost` int(6) default NULL,
`idcat` int(6) default NULL,
`idforo` int(6) default NULL,
`iduser` int(6) default NULL,
`subject` varchar(50) NOT NULL default '',
`text` text NOT NULL,
`date` datetime default NULL,
`ip` varchar(16) NOT NULL default '000.000.000.000',
PRIMARY KEY (`idreply`)
) TYPE=MyISAM COMMENT='Replies for Post';
CREATE TABLE `post` (
`idpost` int(6) unsigned NOT NULL auto_increment,
`idcat` int(6) default NULL,
`idforo` int(6) default NULL,
`iduser` int(6) default NULL,
`type` tinyint(1) NOT NULL default '0',
`hits` bigint(8) NOT NULL default '0',
`subject` varchar(200) NOT NULL default '',
`text` text NOT NULL,
`date` datetime default NULL,
`ip` varchar(16) NOT NULL default '000.000.000.000',
PRIMARY KEY (`idpost`)
) TYPE=MyISAM COMMENT='Post for Topics';
CREATE TABLE `users` (
`iduser` int(6) unsigned NOT NULL auto_increment,
`active` tinyint(1) unsigned NOT NULL default '0',
`lastvisit` datetime default NULL,
`registerdate` datetime default NULL,
`level` tinyint(4) unsigned NOT NULL default '4',
`rank` tinyint(4) unsigned NOT NULL default '0',
`avatar` varchar(50) NOT NULL default 'noavatar.gif',
`avatar_type` varchar(50) default NULL,
`name` varchar(50) NOT NULL default '',
`mail` varchar(250) NOT NULL default '',
`show_mail_public` tinyint(1) NOT NULL default '0',
`country` varchar(250) default NULL,
`country_flag` varchar(50) NOT NULL default 'noflag.gif',
`birthday` date default NULL,
`login` varchar(150) NOT NULL default '',
`password` varchar(250) NOT NULL default '',
`icq` varchar(15) default NULL,
`aim` varchar(250) default NULL,
`yim` varchar(250) default NULL,
`msn` varchar(250) default NULL,
`website` varchar(250) default NULL,
`ocupation` varchar(250) default NULL,
`interests` tinytext,
`activation_key` varchar(250) NOT NULL default '',
`signature` tinytext,
PRIMARY KEY (`iduser`)
) TYPE=MyISAM COMMENT='Users Data';
`idreply` int(6) unsigned NOT NULL auto_increment,
`idpost` int(6) default NULL,
`idcat` int(6) default NULL,
`idforo` int(6) default NULL,
`iduser` int(6) default NULL,
`subject` varchar(50) NOT NULL default '',
`text` text NOT NULL,
`date` datetime default NULL,
`ip` varchar(16) NOT NULL default '000.000.000.000',
PRIMARY KEY (`idreply`)
) TYPE=MyISAM COMMENT='Replies for Post';
CREATE TABLE `post` (
`idpost` int(6) unsigned NOT NULL auto_increment,
`idcat` int(6) default NULL,
`idforo` int(6) default NULL,
`iduser` int(6) default NULL,
`type` tinyint(1) NOT NULL default '0',
`hits` bigint(8) NOT NULL default '0',
`subject` varchar(200) NOT NULL default '',
`text` text NOT NULL,
`date` datetime default NULL,
`ip` varchar(16) NOT NULL default '000.000.000.000',
PRIMARY KEY (`idpost`)
) TYPE=MyISAM COMMENT='Post for Topics';
CREATE TABLE `users` (
`iduser` int(6) unsigned NOT NULL auto_increment,
`active` tinyint(1) unsigned NOT NULL default '0',
`lastvisit` datetime default NULL,
`registerdate` datetime default NULL,
`level` tinyint(4) unsigned NOT NULL default '4',
`rank` tinyint(4) unsigned NOT NULL default '0',
`avatar` varchar(50) NOT NULL default 'noavatar.gif',
`avatar_type` varchar(50) default NULL,
`name` varchar(50) NOT NULL default '',
`mail` varchar(250) NOT NULL default '',
`show_mail_public` tinyint(1) NOT NULL default '0',
`country` varchar(250) default NULL,
`country_flag` varchar(50) NOT NULL default 'noflag.gif',
`birthday` date default NULL,
`login` varchar(150) NOT NULL default '',
`password` varchar(250) NOT NULL default '',
`icq` varchar(15) default NULL,
`aim` varchar(250) default NULL,
`yim` varchar(250) default NULL,
`msn` varchar(250) default NULL,
`website` varchar(250) default NULL,
`ocupation` varchar(250) default NULL,
`interests` tinytext,
`activation_key` varchar(250) NOT NULL default '',
`signature` tinytext,
PRIMARY KEY (`iduser`)
) TYPE=MyISAM COMMENT='Users Data';
El código donde saco esto es este:
Código PHP:
$query = "SELECT ps.*,ps.date,DAYOFMONTH(ps.date) AS dia, DAYOFWEEK(ps.date) AS semana, MONTH(ps.date) AS mes, YEAR(ps.date) AS year, DATE_FORMAT(ps.date,'%h:%i %p') AS hora FROM post ps WHERE ps.idpost=".$idpost." AND ps.idcat=".$_GET['catforo']." AND ps.idforo=".$_GET['foro']."";
$result = mysql_query($query) or die("No se ha podido ejecutar la consulta ".$query.". Error: ".mysql_error());
$fetch = mysql_fetch_assoc($result);
# Consulta para saber el tema actual
$query_00 = "SELECT * FROM foros WHERE idcat=".$_GET['catforo']." AND idforo=".$_GET['foro']."";
$result_00 = mysql_query($query_00) or die("No se ha podido ejecutar la consulta ".$query_00.". Error: ".mysql_error());
$fetch_00 = mysql_fetch_assoc($result_00);
# Consulta para obtener los datos del usuario
$query_01 = "SELECT us.*, ps.*,count(ps.idpost) as cantpost, rp.*, count(rp.idreply) as cantreply, DAYOFMONTH(us.registerdate) AS dia, DAYOFWEEK(us.registerdate) AS semana, MONTH(us.registerdate) AS mes, YEAR(us.registerdate) AS year, DATE_FORMAT(us.registerdate,'%h:%i %p') AS hora FROM users us, post ps, reply rp WHERE ps.idpost=".$fetch['idpost']." AND ps.iduser=us.iduser GROUP BY ps.iduser";
$result_01 = mysql_query($query_01) or die("No se ha podido ejecutar la consulta ".$query_01.". Error: ".mysql_error());
$fetch_01 = mysql_fetch_assoc($result_01);
$messages = $fetch_01['cantpost'] + $fetch_01['cantreply'];
# Consulta para mostrar las respuestas
$query_02 = "SELECT rp.*,us.* FROM reply rp, users us WHERE rp.idpost=".$fetch['idpost']." AND rp.iduser=us.iduser ORDER BY rp.idreply";
$result_02 = mysql_query($query_02) or die("No se ha podido ejecutar la consulta ".$query_02.". Error: ".mysql_error());
$fetch_02 = mysql_fetch_assoc($result_02);
echo "<table width='100%' border='0' cellpadding='0' cellspacing='1'>
<tr><td rowspan='2' class='center forobottom' style='padding-left:5px; padding-right:3px; padding-top:2px; padding-bottom:2px;'><table width='100%' border='0' cellspacing='0' cellpadding='0'>
<tr><td class='userlogin'><span class='info' title='".$fetch_01['name']."'><a href='foros.php?action=show&what=userinfo&iduser=".$fetch_01['iduser']."&".SID."'>".$fetch_01['login']."</a></span></td></tr>
<tr>
<td class='center'><img src='images/avatar/".$fetch_01['avatar']."' alt='Avatar de ".$fetch_01['name']."' class='hand'></td>
</tr><tr><td class='center'>";
# Llamada a la función para representar los ranking de usuarios
Rank($fetch_01['rank']);
echo "</td>
</tr><tr><td class='txt'>";
MakeDate($fetch_01['dia'], $fetch_01['semana'], $fetch_01['mes'], $fetch_01['year'], $fetch_01['hora']);
echo "</td></tr><tr><td class='txt'>Mensajes: ".$messages."</td></tr><tr>
<td class='txt'>País: ".$fetch_01['country']."</td></tr></table></td>
<td width='39%' valign='top' class='txt td' style='padding-left:5px; padding-right:3px; padding-top:2px; padding-bottom:2px;'>Publicado: ";
MakeDate($fetch['dia'], $fetch['semana'], $fetch['mes'], $fetch['year'], $fetch['hora']);
$cadena = $fetch['text'];
$format = BBCode($cadena);
$formattext = nl2br($format);
echo "</td>
<td width='57%' valign='top' class='txt td' style='padding-left:5px; padding-right:3px; padding-top:2px; padding-bottom:2px;'>Asunto: ".$fetch['subject']."
<img src='images/forum/btn/btn02.gif' alt='Citar ...' align='absmiddle' class='hand' border='0'> <img src='images/forum/btn/btn05.gif' alt='Editar mensaje ...' align='absmiddle' class='hand' border='0'> <img src='images/forum/btn/btn010.gif' align='absmiddle' alt='IP Grabada' class='hand' border='0'> <img src='images/forum/btn/btn011.gif' align='absmiddle' alt='Eliminar mensaje ...' class='hand' border='0'>
</td></tr><tr>
<td colspan='2' valign='top' class='txt td style0'>".$formattext."</td>
</tr><tr>
<td colspan='3' class='center forotema style1 right'> </td>";
Salu2