Arkadaşlar merhaba, bir konuda ufak bir yardıma ihtiyacım var, aşağıdaki kodlar aracılığ ile bir satış sistemi mevcut, tablo olarak belirli alanlarda tüm verileri MYSQL'den alabiliyorum ancak konuya tam hakim olamadığım için yapamadığım birşey var. Son eklenen satış, ilk başa gelsin istiyorum ancak en sonra atıyor, ne yapıp ettiysem bir türlü yapamadım. Kodlar aşağıda, bu konuda yardımcı olabilirseniz çok menun olurum.

<?php 
displayMsgs();
?>
<style>
/*.day
{
	color:#000000;
	cursor:pointer;
}
.day:hover
{
	background:#0000FF;
	color:#ffffff;
}
.day.disabled
{
	color:#757575;
}
.day.disabled:hover
{
	background:#757575;
	color:#666666;
}*/
</style>
<link href="<?php echo BASE_URL; ?>assets/css/datepicker.css" rel="stylesheet">
<h3 class="title"><?php echo $page_title;?></h3>
<form id="formsearch" method="get" accept-charset="utf-8" class="form-horizontal">
<input type="hidden" name="module" value="reports"/>
<input type="hidden" name="view" value="sales"/>
<div class="control-group">
  <label class="control-label" for="name"><?php echo $lang['user'];?></label>
  <div class="controls"><select name="user[]" multiple="multiple" class="tip chzn-select span4" id="category" data-placeholder="<?php echo $lang['user'];?> <?php echo $lang['select'];?>" title="<em>pr_category_tip</em>" data-error="<?php echo $lang['user'];?> is required or need attention.">
		<?php if($view == 'add')
		{?>
		<option value="" selected="selected"></option>
		<?php }
		else
		{
		?>
		<option value=""></option>
		<?php
		}?>
		<?php 
		$query = 'SELECT * FROM '.TABLE_USERS;
		$rows = $db->fetchNum($query);
		$res = $db->get_qresult($query);
		if($rows)
		{
		while($cat_data = $db->fetchArray($res))
		{
			//if($_GET['user'] == $cat_data['id']) $temp = 'selected'; else $temp = '';
			if(in_array($cat_data['id'],(array)$_GET['user'])!=false) $temp = 'selected'; else $temp = '';
		?>
			<option value="<?php echo $cat_data['id'];?>" <?php echo $temp;?>><?php echo $cat_data['name'];?></option>
		<?php
		}
		}
		?>
	</select></div>
</div>
<div class="control-group">
  <label class="control-label" for="shop">Mağaza</label>
  <div class="controls"><select name="shop" class="tip chzn-select span4" id="shop" data-placeholder="Mağaza <?php echo $lang['select'];?>" title="<em>pr_category_tip</em>" data-error="Shop is required or need attention.">
		<?php if($view == 'add')
		{?>
		<option value="" selected="selected"></option>
		<?php }
		else
		{
		?>
		<option value=""></option>
		<?php
		}?>
		<?php 
		$query = 'SELECT distinct(shop) FROM '.TABLE_USERS;
		$rows = $db->fetchNum($query);
		$res = $db->get_qresult($query);
		if($rows)
		{
		while($cat_data = $db->fetchArray($res))
		{
			if($_GET['shop'] == $cat_data['shop']) $temp = 'selected'; else $temp = '';
		?>
			<option value="<?php echo $cat_data['shop'];?>" <?php echo $temp;?>><?php echo $cat_data['shop'];?></option>
		<?php
		}
		}
		?>
	</select></div>
</div>
<div class="control-group">
  <label class="control-label" for="title">Görev</label>
  <div class="controls"><select name="title" class="tip chzn-select span4" id="title" data-placeholder="Görev Seç" title="<em>pr_category_tip</em>" data-error="Görevi is required or need attention.">
		<?php if($view == 'add')
		{?>
		<option value="" selected="selected"></option>
		<?php }
		else
		{
		?>
		<option value=""></option>
		<?php
		}?>
		<?php 
		$query = 'SELECT distinct(title) FROM '.TABLE_USERS;
		$rows = $db->fetchNum($query);
		$res = $db->get_qresult($query);
		if($rows)
		{
		while($cat_data = $db->fetchArray($res))
		{
			if($_GET['title'] == $cat_data['title']) $temp = 'selected'; else $temp = '';
		?>
			<option value="<?php echo $cat_data['title'];?>" <?php echo $temp;?>><?php echo $cat_data['title'];?></option>
		<?php
		}
		}
		?>
	</select></div>
</div>
<div class="control-group">
  <label class="control-label" for="category"><?php echo $lang['category'];?></label>
  <div class="controls">
	<select name="category[]" multiple="multiple" class="tip chzn-select span4" id="category" data-placeholder="Kategori <?php echo $lang['select'];?>" title="<em>pr_category_tip</em>" >
		<?php if($view == 'add')
		{?>
		<option value="" selected="selected"></option>
		<?php }
		else
		{
		?>
		<option value=""></option>
		<?php
		}?>
		<?php 
		$query = 'SELECT * FROM '.TABLE_CATEGORIES;
		$rows = $db->fetchNum($query);
		$res = $db->get_qresult($query);
		if($rows)
		{
		while($cat_data = $db->fetchArray($res))
		{
			//if($_GET['category'] == $cat_data['id']) $temp = 'selected'; else $temp = '';
			if(in_array($cat_data['id'],(array)$_GET['category'])!=false) $temp = 'selected'; else $temp = '';
		?>
			<option value="<?php echo $cat_data['id'];?>" <?php echo $temp;?>><?php echo $cat_data['name'];?></option>
		<?php
		}
		}
		?>
	</select> 
  </div>
</div>
<div class="control-group">
  <label class="control-label" for="product"><?php echo $lang['product'];?></label>
  <div class="controls">
	<select name="product[]" multiple="multiple" class="tip chzn-select span4" id="product" data-placeholder="<?php echo $lang['product'];?> <?php echo $lang['select'];?>" title="<em>pr_product_tip</em>">
		<?php if($view == 'add')
		{?>
		<option value="" selected="selected"></option>
		<?php }
		else
		{
		?>
		<option value=""></option>
		<?php
		}?>
		<?php 
		$query = 'SELECT * FROM '.TABLE_PRODUCTS;
		$rows = $db->fetchNum($query);
		$res = $db->get_qresult($query);
		if($rows)
		{
		while($cat_data = $db->fetchArray($res))
		{
			//if($_GET['product'] == $cat_data['id']) $temp = 'selected'; else $temp = '';
			if(in_array($cat_data['id'],(array)$_GET['product'])!=false) $temp = 'selected'; else $temp = '';
		?>
			<option value="<?php echo $cat_data['id'];?>" <?php echo $temp;?>><?php echo $cat_data['name'];?></option>
		<?php
		}
		}
		?>
	</select> 
  </div>
</div>
<div class="control-group">
  <label class="control-label" for="type"><?php echo $lang['invoice_type'];?></label>
  <div class="controls">
	<select name="type" class="tip chzn-select span4" id="type" data-placeholder="<?php echo $lang['invoice_type'];?>" title="<em>pr_type_tip</em>" data-error="Type is required or need attention.">
		<option value=""></option>
		<?php if(!isset($_GET['type']))
		{?>
		<option value="1">Nakit</option>
		<option value="2">Kontrat</option>
		<?php }
		else
		{
		?>
		<option value="1" <?php if($_GET['type']==1) echo 'selected';?>>Nakit</option>
		<option value="2" <?php if($_GET['type']==2) echo 'selected';?>>Kontrat</option>
		<?php
		}?>
	</select>
  </div>
</div>
<div class="control-group">
  <label class="control-label" for="frmdate"><?php echo $lang['date'];?></label>
  <div class="controls">
  <input type="text" name="frmdate" id="frmdate" class="span4 tip small" id="name" title="<?php echo $lang['Bu Tarihten'];?>" value="<?php echo $_GET['frmdate'];?>"/>
  <span><input type="text" name="todate" id="todate" class="span4 tip small" id="name" title="<?php echo $lang['Bu Tarihe'];?>"  value="<?php echo $_GET['todate'];?>"/></span>
   </div>
</div> 
<div class="control-group">
  <div class="controls">
  	<input type="submit" name="submit" value="Rapor <?php echo $lang['report'];?>" class="btn btn-primary" />
	<a href="index.php?module=reports&view=sales" class="btn btn-primary">Sıfırla</a>
    <a href="export_excel.php<?php echo $current_queryString; ?>" class="btn btn-primary">Excel Olarak Raporla</a>
  </div>
</div>
</form> 
<br/>
<h3>Rapor</h3>

<table id="prData" class="table table-bordered table-hover table-striped table-condensed" style="margin-bottom: 5px;">
	<thead>
	<tr>
		<th>#</th>
		<th>Mağaza</th>
		<th><?php echo $lang['username'];?></th>
        <th>Görevi</th>
		<th><?php echo $lang['category'];?></th>
		<th><?php echo $lang['product'];?></th>
		<th><?php echo $lang['invoice_type'];?></th>
        <th>Açıklama</th>
		<th><?php echo $lang['date'];?></th>
		<th><?php echo $lang['quantity'];?></th>
		<th>Tutar</th>
        <?php if($_SESSION['isadmin'] == 1) {?>
        <th style="min-width:115px; text-align:center;">Action</th> 
        <?php }?>
	</tr>
	</thead>
	<tbody>
	<?php
	$query = "SELECT ".TABLE_SALES.".*, ".TABLE_SALES.".type as saletype, ".TABLE_USERS.".name as username, ".TABLE_USERS.".shop as shopname, ".TABLE_USERS.".title as titlename, ".TABLE_PRODUCTS.".name as productname, ".TABLE_CATEGORIES.".name as categoryname FROM ".TABLE_SALES." LEFT JOIN ".TABLE_USERS." ON ".TABLE_SALES.".user = ".TABLE_USERS.".id LEFT JOIN ".TABLE_PRODUCTS." ON ".TABLE_SALES.".product = ".TABLE_PRODUCTS.".id LEFT JOIN ".TABLE_CATEGORIES." ON ".TABLE_PRODUCTS.".category = ".TABLE_CATEGORIES.".id";
	if($_GET)
	{
		$andFlag = false;
		/*if($_GET['user']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_SALES.".user = ".$_GET['user'];
			$andFlag = true;
		}*/
		if($_GET['user'])
		{
			$_GET['user'] = array_filter($_GET['user']);
			if($_GET['user']) {
				if($andFlag) $query .= " and "; else $query .= " WHERE ";
				$query .= TABLE_SALES.".user in (".implode(",",$_GET['user']).")";
				$andFlag = true;
			}
		}
		if($_GET['type']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_SALES.".type = ".$_GET['type'];
			$andFlag = true;
		}
		/*if($_GET['category']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_PRODUCTS.".category = ".$_GET['category'];
			$andFlag = true;
		}*/
		if($_GET['category'])
		{
			$_GET['category'] = array_filter($_GET['category']);
			if($_GET['category']) {
				if($andFlag) $query .= " and "; else $query .= " WHERE ";
				$query .= TABLE_PRODUCTS.".category in (".implode(",",$_GET['category']).")";
				$andFlag = true;
			}
		}
		/*if($_GET['product']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_SALES.".product = ".$_GET['product'];
			$andFlag = true;
		}*/
		if($_GET['product'])
		{
			$_GET['product'] = array_filter($_GET['product']);
			if($_GET['product']) {
				if($andFlag) $query .= " and "; else $query .= " WHERE ";
				$query .= TABLE_SALES.".product in (".implode(",",$_GET['product']).")";
				$andFlag = true;
			}
		}
		if($_GET['shop']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_USERS.".shop = '".$_GET['shop']."'";
			$andFlag = true;
		}
		if($_GET['title']!='')
			{
				if($andFlag) $query .= " and "; else $query .= " WHERE ";
				$query .= TABLE_USERS.".title = '".$_GET['title']."'";
				$andFlag = true;
			}
		if($_GET['frmdate']!='' && $_GET['todate']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_SALES.".date BETWEEN '".$_GET['frmdate']."' AND '".$_GET['todate']."'";
			$andFlag = true;
		}
		else
		if($_GET['frmdate']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_SALES.".date >= '".$_GET['frmdate']."'";
			$andFlag = true;
		}
		else
		if($_GET['todate']!='')
		{
			if($andFlag) $query .= " and "; else $query .= " WHERE ";
			$query .= TABLE_SALES.".date <= '".$_GET['todate']."'";
			$andFlag = true;
		}
	}
	$rows = $db->fetchNum($query);
	if($rows>0)
	{
	$res = $db->get_qresult($query);
	$sno = 1;
	$sum = 0;
	$sum1 = 0;
	while($data = $db->fetchArray($res))
	{
	$sum = $sum + $data['price'];
	$sum1 = $sum1 + $data['quantity'];
	?>
		<tr>
			<td><?php echo $sno++;?></td>
			<td><?php echo $data['shopname'];?></td>
            <td><?php echo $data['username'];?></td>
            <td><?php echo $data['titlename'];?></td>
			<td><?php echo $data['categoryname'];?></td>
			<td><?php echo $data['productname'];?></td>
			<td><?php echo ($data['saletype']=='1')?'Nakit':'Kontrat';?></td>            
            <td><?php echo $data['sexplain'];?></td>
			<td><?php echo date('d-m-Y',strtotime($data['date']));?></td>
			<td><?php echo $data['quantity'];?></td>
			<td align="right"><?php echo number_format($data['price'],2, ',', '.');?> TL</td>
            <?php if($_SESSION['isadmin'] == 1) {?>
            <td align="center">
				<div style="text-align:center">
				<a class="tip" href="<?php echo BASE_URL;?>index.php?module=sales&view=edit&id=<?php echo $data['id'];?>" title="" data-original-title="Edit <?php echo $entity_single;?>"><i class="icon-edit"></i></a>
				<a class="tip delete-ref" data-ref="<?php echo $data['id'];?>" href="<?php echo BASE_URL;?>index.php?module=reports&view=sales&rid=<?php echo $data['id'];?>" title="" data-original-title="Delete <?php echo $entity_single;?>"><i class="icon-trash"></i></a>
			    </div>
			</td>
            <?php }?>
		</tr>
	<?php
	}
	?>
	<tr><td colspan="9"><strong>Toplam</strong></td><td align="right" <?php if($_SESSION['isadmin'] == 1) echo 'colspan="1"';?>><?php echo number_format($sum1,0);?></td><td align="right" <?php if($_SESSION['isadmin'] == 1) echo 'colspan="2"';?>><?php echo number_format($sum,2, ',', '.');?> TL</td></tr>
	
	<?php
	}
	else
	{
		?>
		<tr>
			<td colspan="<?php echo ($_SESSION['isadmin'] == 1?12:11);?>"><?php echo $lang['comming_soon'];?></td>
		</tr>
		<?php
	}
	?>
	
	</tbody>
</table>
<div id="picModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="picModalLabel" aria-hidden="true">
 <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel"></h3>
</div>
<div class="modal-body" style="text-align:center; height:200px;">
<img id="product_image" src="" style="height:100%;" />
</div>
<div class="modal-footer">
<button class="btn btn-danger" data-dismiss="modal" aria-hidden="true"><?php echo $lang['close'];?></button>
</div>
</div></div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="clearfix"></div>
<script>
$(function() {
$( "#frmdate" ).datepicker({
  setDate: new Date(),
  format: 'yyyy-mm-dd',
  autoclose: true
});
$( "#todate" ).datepicker({
  setDate: new Date(),
  format: 'yyyy-mm-dd',
  autoclose: true
});
$('#frmdate').change(function(){
	$( "#todate" ).datepicker('remove');
	if($('#todate').val())
	{
	$( "#todate" ).datepicker( {
		startDate: new Date(),
		format: 'yyyy-mm-dd',
		autoclose: true,
		startDate: $('#frmdate').val()
	});
	}
	else
	{
	var todt = new Date($('#frmdate').val());
	
	$( "#todate" ).datepicker( {
		setDate: todt.getDate() + 7,
		format: 'yyyy-mm-dd',
		autoclose: true,
		startDate: $('#frmdate').val()
	});
	}
});
$('#todate').change(function(){
	$( "#frmdate" ).datepicker('remove');
	$( "#frmdate" ).datepicker({
		format: 'yyyy-mm-dd',
		autoclose: true,
		endDate: $('#todate').val() 
	});
});
});
</script>
<div style="display:none;">
<pre><?php print_r($_GET);?></pre>
</div>