PHP: Filter recordset with array/dates

Posted on 2009-02-23
Last Modified: 2012-06-21

I have a table in my database.


id     name      date1      date2      date3
1      john      1/1/2010      1/2/2010      1/3/2005
2      sue      1/1/2006      1/2/2010      1/3/2006
3      mary      1/1/2010      1/2/2010      1/3/2010
4      adam      1/1/2008      1/2/2008      1/3/2010
5      lisa      1/1/2010      1/2/2009      1/3/2010


$expArr = array
      "type01"=>array("date1","Floating Date"),


SELECT * FROM Employee WHERE $expArr[''][0] < CURdate();

I need a loop to filter the database against the values from the 3 array[0] for dates that are older than today


*Floating Date*

sue      1/1/2006
adam      1/1/2008


adam      1/1/2008
lisa      1/2/2009


john      1/3/2005
sue      1/3/2006

Your help is greatly appreciated.
Question by:kobeballa
    LVL 3

    Accepted Solution

    try this loop code
    $bd_host = "localhost";
    $bd_usuario = "root";
    $bd_password = "";
    $bd_base = "";
    $con = mysql_connect($bd_host, $bd_usuario, $bd_password) or die(mysql_error());
    mysql_select_db($bd_base, $con) or die(mysql_error());
    foreach($expArr as $exprA)
    	$sql="SELECT * FROM Employee WHERE ";
    	$sql.=" < CURdate();";
    	$rs = mysql_query($sql) or die("Error: ".$sql);
    	echo $exprA[1]."<br><br>";
    		echo $row['name'].' '.$row[$exprA[0]]."<br>";
    	echo "<br><br>";

    Open in new window


    Author Comment

    Thank you so much for your help. I will reward you with the points. If it's not too much trouble, I was wondering if you can look at my code and see if you can optimize it.

    I changed the desired output to:


    Birthday - 1/3/2005


    Floating Date - 1/1/2006
    Birthday - 1/3/2006


    Floating Date - 1/1/2008
    Anniversary - 1/2/2008


    Anniversay - 1/2/2009
    My Code:
    $query_rs = "SELECT * FROM viewActivePhysicians";
    $rs = mysql_query($query_rs, $pt1) or die(mysql_error());
    $row_rs = mysql_fetch_assoc($rs);
    		echo "<h3>".$row_rs['lastName'].',&nbsp;'.$row_rs['firstName']."</h3>";
    		foreach($expArr as $exprA)
    			$sql="SELECT * FROM viewActivePhysicians WHERE userID = ";
    			$sql.=$row_rs['userID']." AND ";
    			$sql.=" < CURdate();";
    			$rsDate = mysql_query($sql) or die("Error: ".$sql);
    			$rowDateData = mysql_fetch_assoc($rsDate);
    			$totalrowDateData = mysql_num_rows($rsDate);			
    			if ($totalrowDateData > 0) { // Show if recordset not empty 	
    				echo $exprA[0]." - ".$rowDateData[$exprA[0]]."<br>";	
    	} while ($row_rs = mysql_fetch_assoc($rs));

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now