| | |
The mysterious errors of PHP
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
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:
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:
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!
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
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.
In that case you need to declare the variable you pass to it an array before passing it to that function.
PHP Syntax (Toggle Plain Text)
$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!
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
•
•
•
•
In that case you need to declare the variable you pass to it an array before passing it to that function.
PHP Syntax (Toggle Plain Text)
$fee2_choice = new array();
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
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!
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
•
•
Join Date: Oct 2006
Posts: 82
Reputation:
Solved Threads: 0
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]
[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
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.
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.
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.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: PHP parsing error, unexpected '<'
- Next Thread: interactive maps? is this possible mambo/php
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend






