Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Code Hidden Error

Status
Not open for further replies.

dagger2002

Programmer
Nov 23, 2004
172
US
Ok All i have a hidden error. Can Any One Find it?

Here is A sample DB Row From DB:
Code:
20	Coord Meet Min: 07-03	File Path	3	2007	03	01	20070301	21

Here is the code
Code:
<?php
################################################################################
##
##	Sets Variables

	$year 		= 1;
	$compDate 	= $year . "0632";
	$compDate2 	= ($year - 1) . "0632";

##
################################################################################
?>

<?php
################################################################################
##
##	Queries DB

	$query 	= "SELECT file_id, file_name, file_path, file_type, left(file_date, 4) as file_year, right(left(file_date, 6), 2) as file_month, right(file_date, 2) as file_day, file_date as fullDate FROM `files` WHERE `file_type` = '3'
	ORDER BY fullDate DESC;";

	$result 	= mysql_query($query) or die("Couldn't Select: files");

	$numRows 	= mysql_num_rows($result);

##
################################################################################
?>


<?php
################################################################################
##
##	Start Page Setup

##	If records
	if($numRows > 0){

##		Set Year to null
//		$compareYear = 0;

##	Main While Loop
		while ($row 	= mysql_fetch_array($result)) {


			if($compDate < $row[fullDate] AND $compDate2 > $row[fullDate]){
				echo "<blockquote><p>" . $row[file_name] . "</p></blockquote>";
			}
			else{
				$year 		= $row[file_year];
				$compDate 	= $year . "0632";
				$compDate2 	= ($year - 1) . "0632";

				echo "<h2>" . ($year - 1) . " / " . $year . "</h2>";
				echo "<blockquote><p>" . $row[file_name] . "</p></blockquote>";
			}
			echo "<h3>" . $compDate . " | " . $compDate2 . " : " . $row[fullDate] . " ; " . $year . " , " . $row[file_id] . "</h3>";
		}
	}
	else{
		die("No files to view");
	}
##
################################################################################
?>

The problem is is that the data is always Coming out not true. So it is always hitting the else.
The data is held in a mysql db.

The data should resemble this when done
Code:
EXAMPLE

2006 / 2007

	File 1	20070601
	File 2	20070505
	File 3	20070201
	File 4	20060710

2005 / 2006

	File 5	20060604
	File 6	20050710
...

Thanks All
 
does not the whole thing break down when you use $year = 1 as your start point?
for example 10632 is pretty much always going to be less than 20060730 (disregarding that 32 is not a valid day in any month...)

if you set the $year var at the top to 2007 is suspect it might work more as you want.
 
some thoughts:

1. convert the $comp_ variables to integers to ensure that you are performing a numeric comparison
2. generally - don't fudge column types. if you want a colum to be a date use a date column and format your dates correctly (yyyy-mm-dd): makes life so much easier.
3. your code will always fail the conditional the first time around as $year is 1 thereafter $compDate and $compDate2 are refreshed and reset with a new value each loop iteration. but take a typical iteration

$compDate = 20070601;
$compDate2 = 20060601;

...
$row[fullDate] = 20070501;

your test always ask whether the HIGHER figure ($compDate) is LESS than the date and the LOWER figure must be greater than the date. so i think it unlikelt that 20070601 will ever be less than a number that 20060601 will be greater than.

is that the root cause of the issue? Do you have your comparisons reversed?
 
All of the compDate, and year variables are int. Also All colums out of the db are int minus name and path.

Using your example

The if statement would look like this right?

if(20070601 < 20070501 AND 20060601 > 20070501)

This is a true statement. It should follow the if route. Right?
 
that's correct. but the conditional can never be true. simplifying things

$compdate = 100
$compdate2 = 90

$compdate 2 can never be greater than a number that $compdate is less than.

ie for the part of the conditional to be true the comparison number needs to be greater than 100. which means $compdate2 (which definitionally is always less than $compDate) can never be greater than the number.

I think that you are trying to say that the fullDate must be between $compDate and $compdate2. In which case the correct maths is:

Code:
if ( ($compdate [red]<[/red] $fullDate) && ($compdate2 [red]>[/red] $fullDate) ){
 
ty padie
the math was the problem. i reversed the <> signs.
 
that's what i was trying to say in my last two posts ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top