<?php
$log = $_POST["login"];
$contient = $_POST["contient"];
$type = $_POST["type"];

echo "<center><h4>Recherche de messages</h4></center><br /><form action=\"#\" method=\"post\" name=\"form1\" target=\"_parent\">
  <table width=\"618\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"2\" style=\"border=0px;font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
    <tr style =\"background-color:#222222;\">
      <td colspan=\"2\" align=\"center\">Type de msg : <select name=\"type\">
				<option value=\"bataille\"";
				if($type == "bataille") {echo" selected"; }
				echo">Bataille</option>
				<option value=\"autre\"";
				if($type == "autre") {echo" selected"; }
				echo">Autre</option>
				<option value=\"mp\"";
				if($type == "mp") {echo" selected"; }
				echo">Message Personnel</option>
				</select>
			</td>
    </tr>
    <tr>
      <td>Msg du joueur : 
      	<input name=\"login\" type=\"text\" id=\"login\" value=\"$log\"></td>
      <td>Et contient (falcultatif) :
      	<input name=\"contient\" type=\"text\" id=\"contient\" value=\"$contient\"></td>
    </tr>
    <tr style =\"background-color:#222222;\">
      <td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"Submit\" value=\"Rechercher\"></td>
    </tr>
  </table>
</form>
<br />";

if(empty($login)) {
	exit;
}

if($type == "autre") {
	if (!empty($contient)) {
		$req = "SELECT * FROM matgraph_msg WHERE receveur IN ( SELECT id FROM matgraph_users WHERE login = '".addslashes($log)."') AND msg LIKE '%".addslashes($contient)."' ORDER BY date DESC";
	} else {
		$req = "SELECT * FROM matgraph_msg WHERE receveur IN ( SELECT id FROM matgraph_users WHERE login = '".addslashes($log)."' ) ORDER BY date DESC";
	}
	$res = mysql_query($req) or die('Erreur SQL! <br/>'.$req.'<br/>'.mysql_error());
	$num = mysql_num_rows($res);
	echo"<center>Vous avez obtenu $num résultat(s) à votre recherche :</center><br/><br/>";
	echo"<table width=\"618\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"2\" style=\"border=0px;font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
	  <tr style =\"background-color:#222222;\">
	  	<td align=\"center\"><b>Date : </b></td>
	  	<td align=\"center\"><b>Titre : </b></td>
	  	<td align=\"center\"><b>Message : </b></td>
	  </tr>";
	if ( $num > 0 ) {
	$i=1;
	  While ( $row = mysql_fetch_array($res) ) {
			echo"<tr";
			// gère le quadrillage du tableau
			if (($i/2) == ceil($i/2))
			{
				echo' style ="background-color:#111111;"' ;
			}
			echo">
				<td align=\"center\">$row[date]</td>
				<td align=\"center\">$row[sujet]</td>
				<td align=\"center\">$row[msg]</td>
		  </tr>";
			$i++;  
		}
	}
	echo"</table>";
	
// msg del
	if (!empty($contient)) {
		$req = "SELECT *
		FROM matgraph_msg_log
		WHERE receveur IN (
		SELECT id
		FROM matgraph_users
		WHERE login = '".addslashes($log)."') 
		AND msg LIKE '%".addslashes($contient)."' 
		ORDER BY date DESC";
	} else {
		$req = "SELECT *
		FROM matgraph_msg_log
		WHERE receveur IN (
		SELECT id
		FROM matgraph_users
		WHERE login = '".addslashes($log)."' ) 
		ORDER BY date DESC";
	}
	$res = mysql_query($req) or die('Erreur SQL! <br/>'.$req.'<br/>'.mysql_error());
	$num = mysql_num_rows($res);
	echo"<center>$num Message(s) supprimés : </center><br/><br/>";
	echo"<table width=\"618\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"2\" style=\"border=0px;font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
	  <tr style =\"background-color:#222222;\">
	  	<td align=\"center\"><b>Date : </b></td>
	  	<td align=\"center\"><b>Titre : </b></td>
	  	<td align=\"center\"><b>Message : </b></td>
	  </tr>";
	if ( $num > 0 ) {
	$i=1;
	  While ( $row = mysql_fetch_array($res) ) {
			echo"<tr";
			// gère le quadrillage du tableau
			if (($i/2) == ceil($i/2))
			{
				echo' style ="background-color:#111111;"' ;
			}
			echo">
				<td align=\"center\">$row[date]</td>
				<td align=\"center\">$row[sujet]</td>
				<td align=\"center\">$row[msg]</td>
		  </tr>";
			$i++;  
		}
	}
	echo"</table><br/>";
}elseif($type == "bataille") {
	if (!empty($contient)) {
		$req = "SELECT *
		FROM matgraph_bataille
		WHERE receveur IN (
		SELECT id
		FROM matgraph_users
		WHERE login = '".addslashes($login)."') 
		AND message LIKE '%".addslashes($contient)."%' 
		ORDER BY date DESC";
	} else {
		$req = "SELECT *
		FROM matgraph_bataille
		WHERE receveur IN (
		SELECT id
		FROM matgraph_users
		WHERE login = '".addslashes($login)."' ) 
		ORDER BY date DESC";
	}
	$res = mysql_query($req) or die('Erreur SQL! <br/>'.$req.'<br/>'.mysql_error());
	$num = mysql_num_rows($res);
	echo"<center>Vous avez obtenu $num résultat(s) à votre recherche :</center><br/><br/>";
	echo"<table width=\"618\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"2\" style=\"border=0px;font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
	  <tr style =\"background-color:#222222;\">
	  	<td align=\"center\"><b>Date : </b></td>
	  	<td align=\"center\"><b>Titre : </b></td>
	  	<td align=\"center\"><b>Message : </b></td>
	  </tr>";
	if ( $num > 0 ) {
	$i=1;
	  While ( $row = mysql_fetch_array($res) ) {
			echo"<tr";
			// gère le quadrillage du tableau
			if (($i/2) == ceil($i/2))
			{
				echo' style ="background-color:#111111;"' ;
			}
			echo">
				<td align=\"center\">$row[date]</td>
				<td align=\"center\">$row[sujet]</td>
				<td align=\"center\">$row[message]</td>
		  </tr>";
			$i++;  
		}
	}
	echo"</table><br/>";
} elseif($type == "mp") {
	if (!empty($contient)) {
		$req = "SELECT *
		FROM matgraph_mp
		WHERE receveur IN (
		SELECT id
		FROM matgraph_users
		WHERE login = '".addslashes($log)."') 
		AND message LIKE '%".addslashes($contient)."%' 
		ORDER BY date DESC";
	} else {
		$req = "SELECT *
		FROM matgraph_mp
		WHERE receveur IN (
		SELECT id
		FROM matgraph_users
		WHERE login = '".addslashes($log)."' ) 
		ORDER BY date DESC";
	}
	$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	echo"<center>Vous avez obtenu $num résultat(s) à votre recherche :</center><br /><br />";
	echo"<table width=\"618\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"2\" style=\"border=0px;font-family:trebuchet ms,sans-serif; font-size: 9pt ;\">
	  <tr style =\"background-color:#222222;\">
	  	<td align=\"center\"><b>Date : </b></td>
	  	<td align=\"center\"><b>Titre : </b></td>
	  	<td align=\"center\"><b>Message : </b></td>
	  </tr>";
	if ( $num > 0 ) {
	$i=1;
	  While ( $row = mysql_fetch_array($res) ) {
			echo"<tr";
			// gère le quadrillage du tableau
			if (($i/2) == ceil($i/2))
			{
				echo' style ="background-color:#111111;"' ;
			}
			echo">
				<td align=\"center\">$row[date]</td>
				<td align=\"center\">$row[sujet]</td>
				<td align=\"center\">$row[message]</td>
		  </tr>";
			$i++;  
		}
	}
	echo"</table><br/>";
}
?>