<?php

include("inc.top.php");
$q=new Cdb;
$q2=new Cdb;
	$t->set_file("content", "statistics.html");
	$t->set_file("stats_row", "statistics_row.html");

	get_logged_info();
	$member_id=$q->f("id");

	$order = "";
	
	if ($_GET['name'] == "up"){
		$order = ' ORDER BY name DESC';
	}elseif ($_GET['name'] == "down"){
		$order = ' ORDER BY name ASC';
	}
	
	if ($_GET['email'] == "up"){
		$order = ' ORDER BY email DESC';
	}elseif ($_GET['email'] == "down"){
		$order = ' ORDER BY email ASC';
	}
	
	if ($_GET['mails_sent'] == "up"){
		$order = ' ORDER BY mails_sent DESC';
	}elseif ($_GET['mails_sent'] == "down"){
		$order = ' ORDER BY mails_sent ASC';
	}
	
	if ($_GET['camp_id'] == "up"){
		$order = ' ORDER BY camp_name DESC';
	}elseif ($_GET['camp_id'] == "down"){
		$order = ' ORDER BY camp_name ASC';
	}
	
	if ($_GET['date_click'] == "up"){
		$order = ' ORDER BY date_click DESC';
	}elseif ($_GET['date_click'] == "down"){
		$order = ' ORDER BY date_click ASC';
	}
	
	if ($_GET['not_mail'] == "up"){
		$order = ' ORDER BY not_mail DESC';
	}elseif ($_GET['not_mail'] == "down"){
		$order = ' ORDER BY not_mail ASC';
	}
	
	if ($_GET['aff'] == "up"){
		$order = ' ORDER BY c.name DESC';
	}elseif ($_GET['aff'] == "down"){
		$order = ' ORDER BY c.name ASC';
	}
	
	if ($_GET['nr_joined'] == "up"){
		$order = ' ORDER BY nr_joined DESC';
	}elseif ($_GET['nr_joined'] == "down"){
		$order = ' ORDER BY nr_joined ASC';
	}
	
//SELECT a.id,b.name AS camp_name, a.name, a.email, a.mails_sent, a.nr_joined, a.camp_id, a.date_click, a.ip, a.not_mail, a.aff FROM track a, campaigns b, track c WHERE a.camp_id=b.id AND b.member_id='180' AND a.aff=c.id AND a.id!=1  GROUP BY a.id ORDER BY c.name DESC	
	$q->query("SELECT c.name AS aff_name, b.name AS camp_name, a.name, a.email, a.mails_sent, a.nr_joined, a.camp_id, a.date_click, a.ip, a.not_mail, a.aff FROM track a, campaigns b, track c WHERE a.camp_id=b.id AND b.member_id='$member_id' AND a.aff=c.id AND a.id!=1 GROUP BY a.id $order");
	// mails_sent   	  nr_joined   	  camp_id   	  date_click   	  ip   	  not_mail   	  aff
//	echo "SELECT c.name AS aff_name, b.name AS camp_name, a.name, a.email, a.mails_sent, a.nr_joined, a.camp_id, a.date_click, a.ip, a.not_mail, a.aff FROM track a, campaigns b, track c WHERE a.camp_id=b.id AND b.member_id='$member_id' AND a.aff=c.id AND a.id!=1 GROUP BY a.id $order";
	while ($q->next_record()){
		$t->set_var("name",$q->f("name"));
		$t->set_var("email",$q->f("email"));
		$t->set_var("mails_sent",$q->f("mails_sent"));
		$t->set_var("nr_joined",$q->f("nr_joined"));
		$t->set_var("camp_id",$q->f("camp_name"));
		$t->set_var("date_click",($q->f("date_click") == "0000-00-00 00:00:00" ? "Not Clicked" : $q->f("date_click")));
		$t->set_var("ip",$q->f("ip"));
		$t->set_var("not_mail",($q->f("not_mail") ? "Yes" : "No"));
		
//		$i = 0;
//		if ($q->f("aff") != 0){
//			
//			$q2->query("SELECT name FROM track WHERE id='".$q->f("aff")."'");
//			$q2->next_record();
//			$i = 1;
//		}
		$t->set_var("aff",($q->f("aff_name") ? $q->f("aff_name") : "None"));
		$t->parse("statistics", "stats_row", true);
		
	}
	


include("inc.bottom.php");
?>