Page 1 of 1
[SOLVED] Userdefined offset
Posted: Thu Jan 06, 2005 4:46 pm
by twb
feyd | Help us, help you. Please use 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
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Thu Jan 06, 2005 4:52 pm
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..
Posted: Thu Jan 06, 2005 5:12 pm
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
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]
Posted: Thu Jan 06, 2005 5:36 pm
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.
re:undefined offset
Posted: Thu Jan 06, 2005 6:32 pm
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
Posted: Thu Jan 06, 2005 6:37 pm
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?
Undefined offset
Posted: Thu Jan 06, 2005 8:29 pm
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
Posted: Thu Jan 06, 2005 8:31 pm
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)
Posted: Thu Jan 06, 2005 8:31 pm
by feyd
sounds like the function is getting called, even though you didn't submit anything to it yet.
undefined offset
Posted: Thu Jan 06, 2005 8:51 pm
by twb
markl999
That worked perfect.
Many Thanks
twb