[RESOLU] Boucle while

Mammouth du PHP | 1966 Messages

29 mai 2013, 16:23

je crois que tu mélange le nom de tes tables et les colonnes de ces tables.

Poste nous la structure des tables que tu utilise
Spols
pour les fan de rubik's cube ou pour les curieux ==> le portail francophone du rubik's cube

Eléphanteau du PHP | 11 Messages

29 mai 2013, 17:27

Voici :
CREATE TABLE `forum_categorie` (
   `cat_id` int(11) not null auto_increment,
   `cat_nom` varchar(30) CHARSET latin1 not null,
   `cat_ordre` int(11) not null,
   PRIMARY KEY (`cat_id`),
   UNIQUE KEY (`cat_ordre`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5;

CREATE TABLE `forum_forum` (
   `forum_id` int(11) not null auto_increment,
   `forum_cat_id` mediumint(8) not null,
   `forum_name` varchar(30) CHARSET latin1 not null,
   `forum_desc` text CHARSET latin1 not null,
   `forum_ordre` mediumint(8) not null,
   `forum_last_post_id` int(11) not null,
   `forum_topic` mediumint(8) not null,
   `forum_post` mediumint(8) not null,
   `forum_icon_lu` varchar(255),
   `forum_icon_non_lu` varchar(255),
   `auth_view` tinyint(4) not null,
   `auth_post` tinyint(4) not null,
   `auth_topic` tinyint(4) not null,
   `auth_annonce` tinyint(4) not null,
   `auth_modo` tinyint(4) not null,
   PRIMARY KEY (`forum_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=17;


CREATE TABLE `forum_membres` (
   `membre_id` int(11) not null auto_increment,
   `membre_pseudo` varchar(30) CHARSET latin1 not null,
   `membre_mdp` varchar(32) CHARSET latin1 not null,
   `membre_email` varchar(250) CHARSET latin1 not null,
   `membre_msn` varchar(250) CHARSET latin1 not null,
   `membre_siteweb` varchar(100) CHARSET latin1 not null,
   `membre_avatar` varchar(100) CHARSET latin1 not null,
   `membre_signature` varchar(200) CHARSET latin1 not null,
   `membre_localisation` varchar(100) CHARSET latin1 not null,
   `membre_inscrit` int(11) not null,
   `membre_derniere_visite` int(11) not null,
   `membre_rang` tinyint(4) default '2',
   `membre_post` int(11) not null,
   PRIMARY KEY (`membre_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6;


CREATE TABLE `forum_post` (
   `post_id` int(11) not null auto_increment,
   `post_createur` int(11) not null,
   `post_texte` text CHARSET latin1 not null,
   `post_time` int(11) not null,
   `topic_id` int(11) not null,
   `post_forum_id` int(11) not null,
   PRIMARY KEY (`post_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


CREATE TABLE `forum_topic` (
   `topic_id` int(11) not null auto_increment,
   `forum_id` int(11) not null,
   `topic_titre` char(60) CHARSET latin1 not null,
   `topic_createur` int(11) not null,
   `topic_vu` mediumint(8) not null,
   `topic_time` int(11) not null,
   `topic_genre` varchar(30) CHARSET latin1 not null,
   `topic_last_post` int(11) not null,
   `topic_first_post` int(11) not null,
   `topic_post` mediumint(8) not null,
   PRIMARY KEY (`topic_id`),
   UNIQUE KEY (`topic_last_post`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


CREATE TABLE `forum_topic_view` (
   `tv_id` int(11) not null,
   `tv_topic_id` int(11) not null,
   `tv_forum_id` int(11) not null,
   `tv_post_id` int(11) not null,
   `tv_poste` enum('0','1') not null,
   PRIMARY KEY (`tv_id`,`tv_topic_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Merci d'avance.
Hunab Ku.

Eléphanteau du PHP | 11 Messages

29 mai 2013, 19:04

Résolu en modifiant comme cela :
- Ajout des deux champs dans le SELECT,
- Mise en commentaire de la boucle while pour ne garder que le code intérieur.

Code final :
//Cette requête permet d'obtenir tout sur le forum
/*$query=$db1->prepare('SELECT cat_id, cat_nom, 
forum_forum.forum_id, forum_name, forum_desc, forum_post, forum_topic, auth_view, forum_forum.forum_icon_lu, forum_forum.forum_icon_non_lu, forum_topic.topic_id, forum_topic.topic_post, post_id, post_time, post_createur, membre_pseudo, 
membre_id 
FROM forum_categorie
LEFT JOIN forum_forum ON forum_categorie.cat_id = forum_forum.forum_cat_id
LEFT JOIN forum_post ON forum_post.post_id = forum_forum.forum_last_post_id
LEFT JOIN forum_topic ON forum_topic.topic_id = forum_post.topic_id
LEFT JOIN forum_membres ON forum_membres.membre_id = forum_post.post_createur
LEFT JOIN forum_icon_lu ON forum_forum.forum_id = forum_forum.forum_icon_lu
LEFT JOIN forum_icon_non_lu ON forum_forum.forum_id = forum_forum.forum_icon_non_lu
WHERE auth_view <= :lvl 
ORDER BY cat_ordre, forum_ordre DESC');*/
$query=$db1->prepare('SELECT cat_id, cat_nom, 
forum_forum.forum_id, forum_name, forum_desc, forum_post, forum_topic, auth_view, forum_forum.forum_icon_lu, forum_forum.forum_icon_non_lu, forum_topic.topic_id, forum_topic.topic_post, post_id, post_time, post_createur, membre_pseudo, 
membre_id 
FROM forum_categorie
LEFT JOIN forum_forum ON forum_categorie.cat_id = forum_forum.forum_cat_id
LEFT JOIN forum_post ON forum_post.post_id = forum_forum.forum_last_post_id
LEFT JOIN forum_topic ON forum_topic.topic_id = forum_post.topic_id
LEFT JOIN forum_membres ON forum_membres.membre_id = forum_post.post_createur
WHERE auth_view <= :lvl 
ORDER BY cat_ordre, forum_ordre DESC');
$query->bindValue(':lvl',$lvl,PDO::PARAM_INT);
$query->execute();

/*$query2=$db1->prepare('SELECT forum_icon_lu, forum_icon_non_lu
FROM forum_forum');
$query2->execute();*/

/*while ($data2 = $query2 -> fetch())
{*/
	//Gestion de l'image à afficher
	if (!empty($id)) // Si le membre est connecté
	{
		if ($data['tv_id'] == $id) //S'il a lu le topic
		{
			if ($data['tv_poste'] == '0') // S'il n'a pas posté
			{
				if ($data['tv_post_id'] == $data['topic_last_post']) //S'il n'y a pas de nouveau message
				{
					$ico_mess = $data['forum_icon_lu'];
				}
				else
				{
					$ico_mess = $data['forum_icon_non_lu']; //S'il y a un nouveau message
				}
			}
			else // S'il a  posté
			{
				if ($data['tv_post_id'] == $data['topic_last_post']) //S'il n'y a pas de nouveau message
				{
					$ico_mess = $data['forum_icon_lu'];
				}
				else //S'il y a un nouveau message
				{
					$ico_mess = $data['forum_icon_non_lu'];
				}
			}
		}
		else //S'il n'a pas lu le topic
		{
			$ico_mess = $data['forum_icon_non_lu'];
		}
	} //S'il n'est pas connecté
	else
	{
		/*$ico_mess = $data2['forum_icon_non_lu'];*/
	}
	?>
	<center><table>
	<?php
	//Début de la boucle
	while($data = $query->fetch())
	{
		//On affiche chaque catégorie
		if( $categorie != $data['cat_id'] )
		{
			//Si c'est une nouvelle catégorie on l'affiche
			
			$categorie = $data['cat_id'];
			?>
			<tr>
			<th></th>
			<th class="titre"><strong>
			<?php
			echo stripslashes(htmlspecialchars($data['cat_nom']));
			?>
			</strong></th>             
			<th class="nombremessages"><strong>Sujets</strong></th>       
			<th class="nombresujets"><strong>Messages</strong></th>       
			<th class="derniermessage"><strong>Dernier message</strong></th>   
			</tr>
			<?php
		}
	 
		//Ici, on met le contenu de chaque catégorie

		// Ce super echo de la mort affiche tous
		// les forums en détail : description, nombre de réponses etc...
	 
		//echo'<tr><td><img src="./images/message.gif" alt="message" /></td>
		echo'<tr><td class="lunonlu"><center><img src="./images/forum/';
		
		//Gestion de l'image à afficher
		if (!empty($id)) // Si le membre est connecté
		{
			if ($data['tv_id'] == $id) //S'il a lu le topic
			{
				if ($data['tv_poste'] == '0') // S'il n'a pas posté
				{
					if ($data['tv_post_id'] == $data['topic_last_post']) //S'il n'y a pas de nouveau message
					{
						$ico_mess = $data['forum_icon_lu'];
					}
					else
					{
						$ico_mess = $data['forum_icon_non_lu']; //S'il y a un nouveau message
					}
				}
				else // S'il a  posté
				{
					if ($data['tv_post_id'] == $data['topic_last_post']) //S'il n'y a pas de nouveau message
					{
						$ico_mess = $data['forum_icon_lu'];
					}
					else //S'il y a un nouveau message
					{
						$ico_mess = $data['forum_icon_non_lu'];
					}
				}
			}
			else //S'il n'a pas lu le topic
			{
				$ico_mess = $data['forum_icon_non_lu'];
			}
		} //S'il n'est pas connecté
		else
		{
			$ico_mess = $data['forum_icon_non_lu'];
		}
		echo $ico_mess;
		echo '" alt="message" /></center></td>
		<td class="titre"><strong>
		<a class="lien" href="./voirforum.php?f='.$data['forum_id'].'">
		'.stripslashes(htmlspecialchars($data['forum_name'])).'</a></strong>
		<br />'.nl2br(stripslashes(htmlspecialchars($data['forum_desc']))).'</td>
		<td class="nombresujets">'.$data['forum_topic'].'</td>
		<td class="nombremessages">'.$data['forum_post'].'</td>';
	 
		// Deux cas possibles :
		// Soit il y a un nouveau message, soit le forum est vide
		if (!empty($data['forum_post']))
		{
			 //Selection dernier message
		 $nombreDeMessagesParPage = 15;
			 $nbr_post = $data['topic_post'] +1;
		 $page = ceil($nbr_post / $nombreDeMessagesParPage);
			  
			 echo'<td class="derniermessage">
			 '.date('H\hi \l\e d/M/Y',$data['post_time']).'<br />
			 <a class="lien" href="./voirprofil.php?m='.stripslashes(htmlspecialchars($data['membre_id'])).'&action=consulter">'.$data['membre_pseudo'].'  </a>
			 <a class="lien" href="./voirtopic.php?t='.$data['topic_id'].'&page='.$page.'#p_'.$data['post_id'].'">
			 <img src="./images/forum/go.gif" alt="go" /></a></td></tr>';
			 /*<img src="./images/go.gif" alt="go" /></a></td></tr>';*/
	 
		 }
		 else
		 {
			 echo'<td class="nombremessages">Pas de message</td></tr>';
		 }
	 
		 //Cette variable stock le nombre de messages, on la met à jour
		 $totaldesmessages += $data['forum_post'];
	 
		 //On ferme notre boucle et nos balises
	} //fin de la boucle
/*}*/ // fin du while ($data2 = $query2 -> fetch())
$query->CloseCursor();
//$query2->CloseCursor();
/*echo '</table></div>';*/
echo '</table></center><br /><br />';
Merci énormément à tous. Vous êtes très efficace =D>