Jump to content

[SOLVED] If statement condition not evaluating to true


ephemeral

Recommended Posts

The following code is part of an AJAX-driven page that I am currently constructing:

 

<?php

if(@$_REQUEST['action'] == 'check_date' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
	// check if requested via ajax
	echo json_encode(check_date($_REQUEST['popUpDate']));
	exit; // only print json version of msg
} else if (@$_REQUEST['action'] == 'register') {
	check_date($_REQUEST['popUpDate']);

	if (!$resp['ok']) {
		$message = "There was a problem with your request: \n";
		$message .= $resp['msg'];
	}
}



function check_date($date)
{
	$resp = array();
	$events = get_events();

	if (!$date) 
	{
		$resp = array( 'ok' => false, 'msg' => "You must enter a date" );
	} 
	else if (!preg_match("%^((?:19|20)\d\d)/(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])$%", $date)) 
	{
		$resp = array( 'ok' => false, 'msg' => "Date must be in format YYYY/MM/DD" );
	}
	else 
	{
		// check if date is already taken
		$arraySize = count($events);
		$testVal = false;
		for ($myRow = 0; $myRow < $arraySize; $myRow++) 
		{
			$arb = $events[$myRow]['Date'];
			if (levenshtein($arb, $date) == 0) 
			{
				$resp = array( 'ok' => false, 'msg' => "There is already an event on this date" );
			}
			else
			{
				$testVal = true;
			}
		} // end for	

		//print $testVal;
		// check if date is actually valid
		$groups = array();
		preg_match('%^((?:19|20)\d\d)/(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])$%', $date, $groups);
		if ($groups[3] == 31 && ($groups[2] == 4 || $groups[2] == 6 || $groups[2] == 9 || $groups[2] == 11)) 
		{
			$resp = array( 'ok' => false, 'msg' => "Date is 31 in a month with only 30 days" );
		} 
		else if ($groups[2] == 2 and $groups[3] >= 30) 
		{
			$resp = array( 'ok' => false, 'msg' => "Date is 30 or 31 in February" );
		}
		else if ($groups[2] == 2 and $groups[3] == 29 and !($groups[1] % 4 == 0 and ($groups[1] % 100 != 0 or $groups[1] % 400 == 0))) 
		{
			$resp = array( 'ok' => false, 'msg' => "Date is 29 in February and it is not a leap year" );
		}
		else if (testVal == true) 
		{
			$resp = array( 'ok' => true, 'msg' => "This date is available" );
		} // end if

	} // end else

	return $resp;
} // end function


function get_events()
{
	//Database connection details
	$host = "mysql.utm.edu";
	$mysql_user = "library";
	$mysql_password = "pmlib";
	$mysql_db = "library";

	//make connection with mysql and select the database
	$mysql_connect = mysql_connect($host, $mysql_user, $mysql_password);
	$db_select = mysql_select_db($mysql_db);

	//getting some times for later use (in query and performance)
	$nowTime = time();
	$today = date("Y/n/j", time());

	$results = mysql_query("SELECT DATE_FORMAT(eventTime, '%Y/%m/%d') as eDate,DATE_FORMAT(eventTime, '%H:%i') as eTime,name FROM eventcal_test WHERE eventTime >= '$today' ORDER BY eDate ASC") or die(mysql_error());

	while($row = mysql_fetch_assoc($results))
	{
		$events[] = array('Date' => $row['eDate'], 'Time' => $row['eTime'], 'Performer' => $row['name']); 
	}	
	//print_r($events);
	return $events;

} // end function


// tells which form to post - event form
if ($_POST["formChoice"] == "event") {
	//if (!isset($_POST['popUpDate']) && !isset($_POST['startTime']) && !isset($_POST['endTime'])) {
	// if no values set, post form
?>     
        	<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST" id="eventform">
			<div class="tablecontainer">
				<div class="cap">
					<h2>Add a New Event</h2>
				</div>
				<div id="dateRow" class="tr">
					<div id="dateLabel" class="td">Date: </div>
					<div id="dateInput" class="td"><input id="popUpDate" name="popUpDate" class="popUpDate" autocomplete="off" size="30" value="<?=@$_REQUEST['popUpDate']?>" /></div>
                        <div id="dateError" class="td hidden"></div>
				</div>
				<div id="startTimeRow" class="tr">
					<div id="startTimeLabel" class="td">Start Time: </div>
					<div id="startTimeInput" class="td"><input id="startTime" name="startTime" class="pickTime" autocomplete="off"  size="30" /></div>
                        <div id="startTimeError" class="td hidden"></div>
				</div>
				<div id="endTimeRow" class="tr">
					<div id="endTimeLabel" class="td">End Time: </div>
					<div id="endTimeInput" class="td"><input id="endTime" name="endTime" class="pickTime" autocomplete="off" size="30" /></div>
                        <div id="endTimeError" class="td hidden"></div>
				</div>
			</div>
            	<input type="submit" value="Add Event" name="add" />
		</form>
<?php
	//} elseif (isset($_POST['popUpDate'] && isset($_POST['startTime']) && isset($_POST['endTime'])) {

	//}


// tells which form to post - performance form
} else if ($_POST["formChoice"] == "performance") {
?>
	<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
        	<div class="tablecontainer">
            	<div class="cap">
                	<h2>Add a New Performance to an Existing Event</h2>
                </div>
                <div class="tr">
                	<div class="td">Date: </div>
                    <div class="td">
                    	<select size="1" name="date" id="date">
                    		<option value="" selected="selected" />
                        </select>
                    </div>
                </div>
                <div class="tr">
                	<div class="td">Time: </div>
                    <div class="td">
                    	<select size="1" name="time" id="time">
                        	<option value="" selected="seleted" />
                        </select>
                    </div>
                </div>
                <div class="tr">
                	<div class="td">Name of Performer: </div>
                    <div class="td">
                    	<input type="text" id="name" name="name" size="30" />
                    </div>
                </div>
            </div>
            <input type="submit" value="Add Performance" name="add" />
        </form>
<?php
}
?>

 

 

I am having a problem that seems strange. In this section of code:

 

// check if date is already taken
		$arraySize = count($events);
		$testVal = false;
		for ($myRow = 0; $myRow < $arraySize; $myRow++) 
		{
			$arb = $events[$myRow]['Date'];
			if (levenshtein($arb, $date) == 0) 
			{
				$resp = array( 'ok' => false, 'msg' => "There is already an event on this date" );
			}
			else
			{
				$testVal = true;
			}
		} // end for	

 

The if statement is supposed to check if the two parameters in the levenshtein() function are the same. (Note: What I want to do is check if the strings are the same. I've tried checking if they are equal with ==, using strcmp(), and levenshtein(). Directly before the if statement, inside the for loop, I tried printing levenshtein($arb, $date), and the result I got was zero -- they were equal -- but when placed inside the if like I have above, the statement does not evaluate to true, thus skipping to the else)

 

I have used is_string and both $arb and $date are strings. $arraySize is 1, which is correct for the $events array. When printing $arb and $date, they are both exactly '2009/12/29'. I can't quite figure out why that condition doesn't evaluate to true and enter the if.

 

Thanks ahead of time for any replies, and sorry if I have done anything wrong...this is my first post here.

Yes, I have tried printing something from inside and it is never printed.

 

Also, there is a part of my code a little later than only evaluates if $testVal is set to true, which is done in the "else" of that particular if statement, and that part depending on $testVal is running.

Then your input most likely isn't what you are expecting. I would create some error tracing, something like this:

            $arb = $events[$myRow]['Date'];
            echo "Does $arb == $date?<br>";
            if (levenshtein($arb, $date) == 0)
            {
               echo "Yes";
               $resp = array( 'ok' => false, 'msg' => "There is already an event on this date" );
            }
            else
            {
               echo "No";
               $testVal = true;
            }

I may be being dense here, but surely the only time when levenshtein() will return 0 is if the two arguments are identical... and if that's the case, why not simply test for string equality

if ($arb === $date)

I have tried printing both values directly before the if statement, and they come out to be exactly the same...if that is what you mean by my "input isn't what I am expecting," lemmin. Also, there is a part of that program that is functioning a little later that wouldn't unless $testVal is set to be true...and the only time $testVal is true is when the else is executed.

 

Mark, I tried testing for string equality and that didn't work, so I went through and tried strcmp() and levenshtein(). That was explained in my first post. ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.