[KLUG Members] php 4.3.10, correction

bill bill at billtron.com
Fri Jan 28 15:45:48 EST 2005


On Fri, 2005-01-28 at 02:17, Lunitix wrote:
> Sorry for the multiple postings.
> 
> 
> Also, when the scoring is calculated the new php doesn't seem to like 
> there not being anything passed in the $_POST[sub_score]
> 
> if ($_POST["add_score"]) {
> 	//calculating scores
> 	//print_r($_POST);
> 	list($key,$val)=each($_POST["add_score"]);
> 	$val=$val+$_POST["curval"];
> 	$sqlstring = "$key=$val";
> 	while (list($key,$val)=each($_POST[sub_score])) {  <-here

I finally read the full code and realized my mistake.  I hadn't seen
where you marked the error.  The problem is not with an "if" statement
but with an "each" statement.

In this case, you can't perform the line above marked "<-here" if it
isn't an array.  That will fail in all PHP versions I've ever used.

Either you must guarantee elsewhere it will get an array or check for it
here with 

if (is_array ($_POST[sub_score)) {

  while (list($key,$val)=each($_POST[sub_score])) { 
    //do stuff  
  }

} // end if

or 

if (count($_POST[sub_score])) {

  while (list($key,$val)=each($_POST[sub_score])) { 
    //do stuff  
  }

} // end if

The first one is better because the second may count the length of a
string variable.

> Yet a $_POST[add_score] can have nothing passed to it and all seems well.
>   if ($_POST["add_score"]) {

That's because $_POST["add_score"] is being used in an "if" statement
(which can contain 0, null, or nothing and evaluate correctly as
"false").  An "each" statement, however, requires an array (if it
doesn't get an array it errors).

Hope I've fixed up any confusion I've caused.

kind regards,

bill



More information about the Members mailing list