warning 'error' with implode function

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
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

warning 'error' with implode function

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

what does

Code: Select all

var_dump($chkbox_arr)
return?
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post by garry27 »

it comes back NULL
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Null would say that the variable isn't set. i.e. it's not an array.
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post 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 );
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply