Jump to content

how to sort by date


scarezekiel

Recommended Posts

ok so my problem is i need my data to be sorted by date which comes first.. how do i do it..can someone help me..if possible elaborate some codes? thxx..

 

so this is my program

 


<?
$b=$_GET['b'];
$m=$_GET['m'];
$y=$_GET['y'];

if  ($b <> '' && $m <> '' && $y <> '') {

$branch=$b;
$leavemonth=$m;
$leaveyear=$y;
}
else
{
$branch=$_POST['branch'];
$leavemonth=$_POST['leavemonth'];
$leaveyear=$_POST['leaveyear'];
}



$connection=mysql_connect("$server", "$username", "$password")
or die("Could not establish connection");
mysql_select_db($database_name, $connection)
or die ("Could not select database");


   
    $query="SELECT * from tblworkgroup";
    $result=mysql_query($query);?>


       	  Branch 
        <select name="branch" >
		<option value="all">ALL Branches</option>        
        <? while($row=mysql_fetch_array($result)) { ?>        	
   		 	<option value= "<?=$row['WorkGroupID']?>" <? if ($branch==$row['WorkGroupID']){ echo 'selected'; } ?> ><?=$row['WorkGroupName']?></option>
     <? } ?>
        </select>
           </td>
          </tr>
       <br><br><br>
		  Month   
	<select name="leavemonth" >
	<option value = "1" <? if ($leavemonth==1){ echo 'selected'; } ?>>January</option>
	<option value = "2" <? if ($leavemonth==2){ echo 'selected'; } ?>>February</option>
	<option value = "3" <? if ($leavemonth==3){ echo 'selected'; } ?>>March</option>
	<option value = "4" <? if ($leavemonth==4){ echo 'selected'; } ?>>April</option>
	<option value = "5" <? if ($leavemonth==5){ echo 'selected'; } ?>>May</option>
	<option value = "6" <? if ($leavemonth==6){ echo 'selected'; } ?>>June</option>
	<option value = "7" <? if ($leavemonth==7){ echo 'selected'; } ?>>July</option>
	<option value = "8" <? if ($leavemonth=={ echo 'selected'; } ?>>August</option>
	<option value = "9" <? if ($leavemonth==9){ echo 'selected'; } ?>>September</option>
	<option value = "10" <? if ($leavemonth==10){ echo 'selected'; } ?>>October</option>
	<option value = "11" <? if ($leavemonth==11){ echo 'selected'; } ?>>November</option>
	<option value = "12" <? if ($leavemonth==12){ echo 'selected'; } ?>>December</option> 
	</select>

	   
	Year   
	<select name="leaveyear" >
	<option value = "2010" 	<? if ($leaveyear==2010) { echo 'selected';} ?>>2010</option>
	<option value = "2011"	<? if ($leaveyear==2011) { echo 'selected';} ?>>2011</option>
	<option value = "2012"	<? if ($leaveyear==2012) { echo 'selected';} ?>>2012</option>
	<option value = "2013"	<? if ($leaveyear==2013) { echo 'selected';} ?>>2013</option>
	<option value = "2014"	<? if ($leaveyear==2014) { echo 'selected';} ?>>2014</option>
	<option value = "2015"	<? if ($leaveyear==2015) { echo 'selected';} ?>>2015</option>
	<option value = "2016"	<? if ($leaveyear==2016) { echo 'selected';} ?>>2016</option>
	<option value = "2017"	<? if ($leaveyear==2017) { echo 'selected';} ?>>2017</option>

	</select>


		  

<input type='submit' name='submit' value='Preview' Class="button" onclick='return validate()'>
   
   </div>

</form>			


<br /><br />

<div id="box" valign="top">
                	<h3> <strong>Leave Application Details</strong> &nbsp</h3> 
				<br><br>

                	<table width="80%" align="center" >
					<thead> 
<tr>
<th width="700px" align="left">Name</a></th>
                            	
<th width="800px" align="center"></a>Application Date</th>
                           
<th width="500x" align="right"></a>Reason</th>
</tr>
</thead>
<tbody>
<?
/*echo "branch:".$branch.'<br />';
echo "leavemonth:".$leavemonth.'<br />';
echo "leaveyear:".$leaveyear.'<br />';*/
//--------------------------------------------------
if ($leavemonth =='' ||  $leaveyear =='' || $branch == '') {
	// one value missing, so don't display		
}
else
{
	// start display data
	$branchtracker = '';

	if ($branch=='all'){
			$branch = '%';
	}

	$rs = mysql_query( " call vwleavereport('$leavemonth', '$leaveyear', $branch, 0);");
	$query="SELECT *, 
					MONTH(tblleaveapplication.DateFrom) as DFMonth,
					MONTH(tblleaveapplication.DateTo) as DTMonth,
					YEAR(tblleaveapplication.DateFrom) as DFYear
					FROM `tblleaveapplication`
					LEFT JOIN `tblemployee`
					ON tblleaveapplication.employeeid = tblemployee.id
					WHERE WorkGroupID LIKE '".$branch."'				
					AND (
					(MONTH(tblleaveapplication.DateFrom)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.")
					OR
					(MONTH(tblleaveapplication.DateTo)=".$leavemonth." AND YEAR(tblleaveapplication.DateFrom)=".$leaveyear.")
					)
					ORDER BY WorkGroupID
					";

	$result = mysql_query($query);
	if ((mysql_num_rows($result))>0){
?>		
	<FORM name="printbutton" method="post" align="left" action="report_print.php" target="_blank">
		<input type="hidden" name="leavemonth" value="<?php echo $leavemonth; ?>">
		<input type="hidden" name="leaveyear" value="<?php echo $leaveyear; ?>">
		<input type="hidden" name="branch" value="<?php echo $branch;?>">
		<Input type = "Submit" Name = "Submit1" Class="button" VALUE = "Print Leave Details">
	</form>				
<?	
	while($row=mysql_fetch_array($result))
	{
		if ($branchtracker<>$row['WorkGroupID']){
			echo '<tr><td colspan=4 style="background-color: #e8e8e8; font-weight: bold;">'.$row['WorkGroupID'].'</td></tr>';
			$branchtracker = $row['WorkGroupID'];
		} 
		echo '<tr>';
			echo '<td>'.$row['EmployeeName'].'</td>';
			echo '<td>'.$row['DateFrom'].'  -  '.$row['DateTo'].'</td>';
			echo '<td>'.$row['Purpose'].'</td>';				
		echo '</tr>';			
	}
 }
}
?>

post-131669-1348240330524_thumb.jpg

Link to comment
Share on other sites

Assuming you still need the records to be sorted initially on "WorkGroupID", just add a secondary SORT BY

 

And, why are you creating all the aliased fields ('DFMonth', 'DTMonth', 'DFYear') if you are not even using them. Plus, you don't want ot use * in your SELECT queries if you don't need all the data - it is a waste. I've also adjusted the query WHERE conditions to be simpler

 

$query = "SELECT WorkGroupID, EmployeeName, DateFrom, DateTo, Purpose
          FROM `tblleaveapplication`
          LEFT JOIN `tblemployee`
            ON tblleaveapplication.employeeid = tblemployee.id
          WHERE WorkGroupID LIKE '".$branch."'				
            AND YEAR (tblleaveapplication.DateFrom)=".$leaveyear."
            AND (
                 MONTH(tblleaveapplication.DateFrom)=".$leavemonth."
                 OR
                 MONTH(tblleaveapplication.DateTo)  =".$leavemonth."
                )
          ORDER BY WorkGroupID, DateFrom";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.