<?php
session_start();	
	if (isset($_POST['k1'])) {
	require_once('../wa_baza/wa_polacz.php');
	date_default_timezone_set('Europe/Warsaw');
	$id = $_SESSION['id']; // id user
	$nazwa = $_SESSION['nazwa'];  //nazwa user
	$data = date("Y-m-d");
	
	$nu=$_POST['k1'];
	if ($nu=='Wszyscy') {
	$zap = "SELECT od, do, tekst, data, godz FROM chat WHERE
			wszyscy=1 AND datediff(NOW(), data)<3
			ORDER BY data, godz ASC";
	} else $zap = "SELECT od, do, tekst, data, godz FROM chat WHERE
			((od='$nazwa' And do='$nu') OR (do='$nazwa' And od='$nu'))
			AND (datediff(NOW(), data)<7)
			ORDER BY data, godz ASC";
	$pw = mysqli_query($db, $zap);
	$cw = array();
	while ($wiersz = mysqli_fetch_row($pw)) 
	{
		$cw[] = $wiersz;
	}	
	//-----------------------
	$lw = count($cw);
	$nd = 0;
	$nw = 0;
	$sp = '&nbsp;&nbsp;';
	$jeden_dzien = strtotime(date("Y-m-d", strtotime($data)) . " -1 day"); 
	$wczoraj = date('Y-m-d', $jeden_dzien); 
	$ad2 = '';
	for ($i=0; $i<$lw; $i++) {
		$ad = $cw[$i][3]; 
		if (($ad!=$ad2) && ($ad!=$wczoraj) && ($ad!=$data)) {
			echo '<br /><p style="color: #D48; text-align: center; font-weight: bold">--------------------[ '.$ad.' ]--------------------</p>';
		}
		if ($ad==$wczoraj && $nw==0) {
			echo '<br /><p style="color: #e55; text-align: center; font-weight: bold">--------------------'.$sp.' WCZORAJ '.$sp.'--------------------</p>';
			$nw++;
		}
		if ($ad==$data && $nd==0) {
			echo '<br /><p style="color: #e55; text-align: center; font-weight: bold">--------------------'.$sp.' DZISIAJ '.$sp.'--------------------</p>';
			$nd++;
		}		
		if ($cw[$i][0]==$nazwa) {
			$kol = '#d40';
		} else $kol = '#090';
		echo '<p id="chat_text1" style="color: '.$kol.'">'.$cw[$i][0].': </p>';
		echo '<p id="chat_text2">'.$cw[$i][2].'</p>';
		if (((strtotime($data) - strtotime($ad)) / (86400))>1) {
		echo '<p id="chat_text3">'.$ad.' / '.$cw[$i][4].'</p><br />';
		} else echo '<p id="chat_text3">'.$cw[$i][4].'</p><br />';
		$ad2 = $cw[$i][3];
	}
}

?>