Page 1 of 1
warning 'error' with implode function
Posted: Fri Nov 17, 2006 2:08 pm
by garry27
does anyone know why i might be getting the following error
Warning: implode() [function.implode]: Bad arguments. in /home/unn_p921847/public_html/test3/map_data2.php on line 14
on this line of my script:
Code: Select all
$sql_str = implode(" ", $chkbox_arr );
and if there's a way around it.
cheers
Posted: Fri Nov 17, 2006 2:16 pm
by John Cartwright
Posted: Fri Nov 17, 2006 2:21 pm
by garry27
it comes back NULL
Posted: Fri Nov 17, 2006 2:24 pm
by feyd
Null would say that the variable isn't set. i.e. it's not an array.
Posted: Fri Nov 17, 2006 2:33 pm
by garry27
but the code runs as it should. what to you mean by set? do you mean i have to declare $chbox_arr as an array before i add values to it?
here's the code i have up util line 14:
Code: Select all
<?php
require_once('common.php');
db_connect();
error_reporting(E_ALL ^ E_NOTICE);
foreach($_GET as $key=>$value) {
if ($value !== ''){
$chkbox_arr[]=' AND Amenities.'.$key."='y'";
}
}
//string together all elements in a single string
$sql_str = implode(" ", $chkbox_arr );
Posted: Fri Nov 17, 2006 2:37 pm
by feyd
Not set, as in doesn't exist, not there, gone, vapor.
Remove the E_NOTICE toggle from your call to
error_reporting(), you should see something interesting.
Posted: Fri Nov 17, 2006 2:41 pm
by John Cartwright
This is coming from a form correct? Is the method set to GET? Typically we send data using POST, in which we reference it using $_POST.
You also will want to initialize your array before adding values to do,
Code: Select all
error_reporting(E_ALL);
$chkbox_arr = array();
foreach($_GET as $key=>$value) {
$chkbox_arr[]=' AND Amenities.'.$key."='y'";
}
//string together all elements in a single string
$sql_str = implode(" ", $chkbox_arr );
There is no need to check whether there are values contained within the check box, as it won't be passed in your post if it was not checked.
Run this and please tell me the output.
Posted: Fri Nov 17, 2006 2:57 pm
by garry27
ye, i had to declare it to remove the error. not sure why. i always read that you didn't have to declare variables or arrays in php before they're used.
thanks
Posted: Fri Nov 17, 2006 3:05 pm
by feyd
All variables must be declared before being read.
In your originally posted code, if $_GET was empty, $chkbox_arr would never exist by the time your call to
implode() was run. With arrays, it is especially important to have them declared before reading them. While some of this importance is lesser now with register_globals off, it's still a valid and good practice to initialize your variables.