The mysterious errors of PHP

Thread Solved

Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

The mysterious errors of PHP

 
0
  #1
Oct 30th, 2006
Hi
Can you see somthing I am missing?
My form is passing arrays to the process and the process is producing
some errors. I cannot see any problems. In fact I have two other simular
foreach loops that are not creating errors.
Thanks
[php]
<?php
$fee2_choice = strip_tags(trim($_POST['fee2_choice']));
$fee2_choice = array_filter($fee2_choice);//line 150
$fee2_choice = ereg_replace("[)(.-]","",$fee2_choice);

$fee2_code = strip_tags(trim($_POST['fee2_code']));
$fee2_code = array_filter($fee2_code); //line 155
$fee2_code = ereg_replace("[)(.-]","",$fee2_code);

$fee2_unit = strip_tags(trim($_POST['fee2_unit']));
$fee2_unit = array_filter($fee2_unit); //line 160
$fee2_unit = ereg_replace("[)(.-]","",$fee2_unit);

$fee2_describe = strip_tags(trim($_POST['fee2_describe']));
$fee2_describe = array_filter($fee2_describe);//line 165
$fee2_describe = ereg_replace("[)(.-]","",$fee2_describe);

$fee2_money = strip_tags(trim($_POST['fee2_money']));
$fee2_money = array_filter($fee2_money); //line 170
$fee2_money = ereg_replace("[)(.-]","",$fee2_money);

//loop through arrays that will be inserted into database
$indices = array_keys($fee2_code); //line 391
foreach($indices as $index) //line 392
{
$fee2_choice[$index];
$fee2_code[$index];//fee_code
$fee2_unit[$index];//number of units
$fee2_describe[$index];//description
$fee2_money[$index];//service fee

//evaluation and insert occur here
}

?>
[/php]

error messages received when form is submitted:
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 150
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 155
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 160
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 165
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 170
Warning: array_keys() [function.array-keys]: The first argument should be an array in D:\search_process.php on line 391
Warning: Invalid argument supplied for foreach() in D:\pulse_web_server\webroot\pulse\billing\billing_ohip_fee_search_process.php on line 392
:eek:
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Solved Threads: 35
Moderator
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: The mysterious errors of PHP

 
0
  #2
Oct 30th, 2006
array_filter() takes two arguments you only gave it one.
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: The mysterious errors of PHP

 
0
  #3
Oct 30th, 2006
Originally Posted by stymiee View Post
array_filter() takes two arguments you only gave it one.

Hi

Thanks for responding.

It is difficult to understand what the callback is can you explain better than the PHP manual?

Note: I was looking at Example 2. array_filter() without callback.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Solved Threads: 35
Moderator
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: The mysterious errors of PHP

 
0
  #4
Oct 31st, 2006
In that case you need to declare the variable you pass to it an array before passing it to that function.

  1. $fee2_choice = new array();
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: The mysterious errors of PHP

 
0
  #5
Oct 31st, 2006
Originally Posted by stymiee View Post
In that case you need to declare the variable you pass to it an array before passing it to that function.

  1. $fee2_choice = new array();
Thanks for the response.

If I am already working with arrays do this?

[php]
$fee2_choice = new array($fee2_choice);
[/php]

The information coming from the form are arrays, see simple example.
[php]
//FORM FUNCTION
<?php
function name()
{
<tr>
<td name"\fee2_choice[]\"></td>
<td name"\fee2_code[]\"></td>
</tr>

$all_array = array(fee2_choice, fee2_code);
returned $all_array;
}//end of function

list($fee2_choice, $fee2_code) = $all_array;
?>
[/php]



Propose change
[php]
//PROCESS
<php>

$fee2_code = strip_tags(trim($_POST['fee2_code']));

$fee2_choice = new array($fee2_choice);

$fee2_code = array_filter($fee2_code);


</php>
[/php]
Last edited by assgar; Oct 31st, 2006 at 12:36 am. Reason: missing code
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Solved Threads: 35
Moderator
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: The mysterious errors of PHP

 
0
  #6
Oct 31st, 2006
A TD can't be an array. Only form elements like INPUT.
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: The mysterious errors of PHP

 
0
  #7
Oct 31st, 2006
Originally Posted by stymiee View Post
A TD can't be an array. Only form elements like INPUT.
That is correct. My mistake. This is the longer version of the code in the form function that dynamically create 20 to 49 rows.

[php]
echo"<tr height=\"10\">
<td width=\"4%\" bgcolor=\"#eeeee0\" align=\"center\"><input
type=\"checkbox\" name=\"fee2_choice[$i]\"
value=\"$code_id\"></td>
<td width=\"7%\" bgcolor=\"#eeeee0\" ><span class=\"style20
\"><strong>$fee2_code</strong></span></td>
<td width=\"3%\" bgcolor=\"#fff8dc\" height=\"10\">
<input type=\"text\" name=\"fee2_unit[$i]\" size=\"1\"
maxlength=\"2\" value =\"$fee_unit\"/></td>
<td width=\"79%\" bgcolor=\"#fff8dc\" class=\"style20\">
$description </td>
<td width=\"6%\" align=\"left\" >$p_fee</td>\n";
echo"</tr>\n";
[/php]
Last edited by assgar; Oct 31st, 2006 at 12:46 am. Reason: spelling
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: The mysterious errors of PHP

 
1
  #8
Oct 31st, 2006
In your code, try dumping the cotents of the array before you use array_filter().
EG:

[PHP]var_dump($fee2_code);
$fee2_code = array_filter($fee2_code); //line 155[/PHP]

The error is because it isn't an array. Even though your HTML looks ok and it seem it should be an array.



It is difficult to understand what the callback is can you explain better than the PHP manual?

Note: I was looking at Example 2. array_filter() without callback.
The callback is a function that you define, that will be executed in another function.

If you were to implement callback functions in your own code, it could look something like:

[PHP]

/**
* Example of function using a callback. (same as array_filter except it will filter out empty strings also)
* @param array The array to filter
* @param function The callback function
*/
function my_array_filter($array, $callback = false) {

if (!$callback) {
$callback = 'trim'; // default callback function: trim()
}

$new_array = array(); // filtered array
foreach($array as $index=>$value) {
// execute the callback function passing it the argument: $value
if (call_user_func($callback, $value)) {
$new_array[$index] = $value;
}
}
return $new_array;
}

/**
* Example of my_array_filter()
*/

$arr = array('hey', false, true, '0', ' ', '');

var_dump(my_array_filter($arr));
echo '<br />';
var_dump(array_filter($arr));


[/PHP]


If you look at the function, my_array_filter, there is a line:

[PHP]call_user_func($callback, $value)[/PHP]

call_user_function will execute a user defined function. Since this function name is passed as an argument in to a function, then it is called a callback.
It just means its a chosen function to use inside another function...

Actually that explanation came out weird.

A simple example:

[PHP]


function my_callback_implement($callback, $argument) {

// some useful code here

$result = $callback($argument);

}

// example use


my_callback_implement('echo', 'This was echoed with echo() where echo is the callback function in my_callback_implement().');

[/PHP]

Here the argument you suply is executed as the callback function on teh line:
$result = $callback($argument);

as you can see its just the variable you supply, being called as a function. A callback function..


Anyways, back to the array_filter.

If you dont define the second argument in array_filter then it uses a callback function that just returns the value of the argument supplied.
So if the argument is false, it will be filtered out.

Edit:

Note:

[php]


$callback = 'echo';
$argument = 'Hello callback world';
$callback($argument);

[/php]

looks exactly like:

[php]

echo('Hello callback world');

[/php]

to php.

However we call 'echo' the callback function since it is user defined, and called at runtime.
Last edited by digital-ether; Oct 31st, 2006 at 10:35 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: The mysterious errors of PHP

 
0
  #9
Oct 31st, 2006
Wow. Great explaination.
Thanks for the time and effort I will give it a shot.

"The best part of what I do are the problems. I then learn about the problem and how to solve it."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC