[SOLVED] Userdefined offset

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

[SOLVED] Userdefined offset

Post by twb »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


hi, Im new to php
I'm getting a warrning undefined Offset that occurs twice. The full error is "Undefined offset: 2 The textbox input is dd/mm/yyyy. And returns the yyyy/mm/dd. It queries the db fine and returns the proper results. The warning only comes up when you load the initial page.

Code As Follows:

Code: Select all

list($day,$month,$year) = explode("/",$date); 
$timestamp = mktime(0, 0, 0, $month,$day,$year); 
$datevariable = date("Y-m-d",$timestamp); 
$query = "Select * From calander WHERE task_date = '$datevariable' ";

Thanks in advance
twb


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

since you didn't post any more code, I can only assume it's a problem with the $date value not exploding into 3 values for the list call to work. You may want to verify that they are getting broken apart correctly..
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

Post by twb »

the full code for this function is

Code: Select all

function ValidateSearch($date){
	global $msg;
	if(!ereg('^([0-9]{2})/([0-9]{2})/([0-9]{4})$',$date,$parts)){
		$msg .= (!empty($msg)) ? '<BR>': '';		
		$msg .= "Invalid birthdate format entry enter dd/mm/yyyy";
		}
		
	if(!checkdate ($parts[2],$parts[1],$parts[3])){
		$msg .= (!empty($msg)) ? '<BR>': '';		
		$msg .= "The date of birth is invalid. Check that month is between 1-12
				and day is valid for that month.";}
				
	
	list($day,$month,$year) = explode("/",$date); 
	$timestamp = mktime(0, 0, 0, $month,$day,$year); 
	$datevariable = date("Y-m-d",$timestamp); 
	$query = "Select * From calander WHERE task_date = '$datevariable' "; 
	
	echo "  ";
	echo "querry is  ",$query;
	$result = mysql_query($query); 
	if(!$result){
	echo "Error in search for results:".mysql_error();
	//exit();
	}
I gather it explodes O.K since it queries the db and returns the proper results

Thanks
twb


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what line does this warning happen on?

What's the reason for exploding the date, when you've already extracted them to $parts? You don't need the mktime() and date() calls either by that point. You can just place the $parts pieces into the proper order.
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

re:undefined offset

Post by twb »

This happens on the

Code: Select all

list($day,$month,$year) = explode("/",$date);
line. The reason I exploded it is because the $parts have to be changed fron dd/mm/yyyy(which is the input from a text box) to yyyy/mm/dd. When I dont explode the parts it wont give me the yyyy/mm/dd. It will see it as mm/dd/yyyy

twb
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The error suggests that $date is not in dd/mm/yyyy format, but more like dd/mmyyyy format.
Right beofre the explode line can you do an echo $date; and post the output?
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

Undefined offset

Post by twb »

if I type in 22/12/2004 it will echo 22/12/2004. The problem occurs before I type anything in(when the page loads). After I type in the date and submit, it works and there is no warning. The whole problem is when I enter that particular page on loading.

Thanks again
twb
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Ah, sounds like you are validating the date before it's even submitted, so maybe ValidateSearch() is being called too previous.
You'll only want to call ValidateSearch() on a form post, so something like,

Code: Select all

if(!empty($_POST)){
    ValidateSearch($_POST['date']);
}
(amend to suite)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like the function is getting called, even though you didn't submit anything to it yet.
twb
Forum Commoner
Posts: 27
Joined: Thu Jan 06, 2005 4:39 pm

undefined offset

Post by twb »

markl999
That worked perfect.
Many Thanks
twb
Post Reply