Jump to content

Recommended Posts

Hi I have following tables:

alltables01.jpg

 

And following is my PHP code (mysearch.php) which will search and list the relevant results in the same page.

For example lets say in the search criteria when someone enters particuar date like 05/05/2013, I want all the results of that month i.e May 2013 (anything from 01/05/2013 - 31/05/2013) to be listed; Can this be done.

Or I have to create another field in the database with MM-YY only and search that field.

Any help would be much appreciated, thank you.

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
?>
<?php
include 'db.inc.php';
?>
<?php
session_start();
$_SESSION['duration']='duration';
$_SESSION['datepicker']='datepicker';
$_SESSION['transport']='transport';
$_SESSION['activity']='activity';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Intranet Activities Search</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css" />
<script>
$(function() {
$("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
});
</script>
<style type="text/css" media="screen">
table {
	font-size: 14px;
}
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="mysearch.php">
  <table width="600" border="0" cellspacing="0" cellpadding="0">
        <tr>
      <td>Duration (days)</td>
      <td>
      <?php
      	$duration_options = '';
		for ($d=7; $d<=14; $d++) {
    	$sel = $d == $_SESSION['duration'] ? "selected='selected" : '';
    	$duration_options .= "<option $sel value='$d'> $d</option>";
		}
		?>
      <select name="duration" id="duration">
           <option value="">Any</option>
		   <?php echo $duration_options ?>
      </select></td>
    </tr>
      <tr>
        <td> </td>
        <td> </td>
      </tr>
      <tr>
      <td width="45%">Date (YYYY-MM-DD)</td>
      <td width="55%">
      <?php
	  	$datepicker_options['datepicker'] = 'datepicker';
		$_SESSION['datepicker'] = $datepicker_options['datepicker'];		
	  ?>
      <input name="datepicker" type="text" id="datepicker" />
      </td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td>Transportation</td>
      <td>
      <?php
     	$transport_options = '';
		$trans = array('included', 'excluded');
		foreach ($trans as $o) {
    	$sel = $o == $_SESSION['transport'] ? "selected='selected" : '';
    	$transport_options .= "<option $sel value='$o'> $o</option>";
		}
	 ?>
     <select name="transport" id="transport">
          <option selected="selected" value="0">Any</option>
		  <?php echo $transport_options ?>
      </select>  
      </td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td colspan="2">
<?php
    $_SESSION['activity'] = (isset($_POST['activity'])) ? $_POST['activity'] : array();
    $query="SELECT * FROM inclusion ORDER BY incid";
    $test= mysql_query($query) or die(mysql_error());
    while(list($incid, $incdesc) = mysql_fetch_row($test)) {
        $chk = in_array($incid, $_SESSION['activity']) ? "checked='checked'" : '';
        echo "<input type=\"checkbox\" name=\"activity[]\" value=\"$incid\" $chk /> $incdesc <br>";
    }
?>
</td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td><input type="submit" name="searchbtn" id="searchbtn" value="Search" /></td>
      <td> </td>
    </tr>
  </table>
</form>
<p></p>
<p></p>
<p>RESULTS</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td> </td>
  </tr>
</table>
<?php
if(isset($_REQUEST['searchbtn'])){
    $where = array();
    $whereclause = '';
    
    if (!empty($_POST['duration'])) {
        $val = intval($_POST['duration']);
        $where[] = "(acdur = $val)";
    }
    
    if (!empty($_POST['transport'])) {
        $val = mysql_real_escape_string($_POST['transport']);
        $where[] = "(actransp = '$val')";
    }
	
	    if (!empty($_POST['datepicker'])) {
        $val = mysql_real_escape_string($_POST['datepicker']);
        $where[] = "(acdate = '$val')";
    }
    
   // assuming you change you checkbox names
    if (isset($_POST['activity'])) {
        $val = join(",", array_map('intval', $_POST['activity']));
        $where[] = "(incl_activity.incid IN ($val))";
    }
	    
    if (count($where)) $whereclause = "WHERE " . join(' AND ', $where);
			
$query = "SELECT activities.*, teamleader.tlname, GROUP_CONCAT(incdesc SEPARATOR ', ') as Includes  
FROM activities
JOIN incl_activity on activities.acid = incl_activity.acid
JOIN inclusion on incl_activity.incid = inclusion.incid
JOIN teamleader on activities.tlid2 = teamleader.tlid
$whereclause
GROUP BY activities.acid
ORDER BY activities.accost ASC";
			
		$test= mysql_query($query) or die(mysql_error());
	
	    if(mysql_num_rows($test))
    {
        $i=0;    
        while($row = mysql_fetch_assoc($test)){
        
            $actitle=$row["actitle"];
            $tlname=$row["tlname"];
            $acdate=$row["acdate"];
            $acdur=$row["acdur"];
            $accost=$row["accost"];
            $incdesc=$row["Includes"];
            $actransp=$row["actransp"];
            
            echo "<div style='width:250px; padding:10px; float:left;'>
                $actitle <br> $tlname <br> $acdate <br> $acdur <br> $accost <br> $incdesc <br> $actransp
                </div>";
            $i++;            
            if($i % 3 == 0) echo "<div style='clear:both'></div>\n";
            
        }
        if ($i%3) echo "<div style='clear:both'></div>\n";
    }	
	else
{
     echo '<div align=center style="margin:20px; font-family:Arial, Helvetica, sans-serif; font-size: 20px; font-weight:bold; color: #ae1919;">Your search did not match any results.</div>';
	 header('Refresh: 5; URL=mysearch.php');
}
}
?>
</body>
</html>
<?php mysql_free_result($test); ?>

 

You can use the BETWEEN operator but you will still need to do some date manipulation.  Something like:

 

<?php
$input_date = '2013-05-10';  # just a random date to test with
$time = strtotime($input_date);
$first_day_of_month = date('Y-m-01', $time);
$last_day_of_month = date('Y-m-t', $time);

$query = "SELECT `actitle` FROM `activities` WHERE `acdate` BETWEEN '$first_day_of_month' AND '$last_day_of_month'";

?>

 

Hopefully that makes sense to you.

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.