Small PHP Issue

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
There is a variable called flags created throughout my script that this function responds to. For some reason, with the variable set as dumped below, it does not print anything. Why??? *growls*
PHP:
function feedback($type) {
    if (count($flags[$type]) > 1) {
        echo "<div id=\"statusbox_" . $type . "\"><ul>\n";
        foreach ($flags[$type] as $value) {
            echo "<li>" . $value . "</li>\n";
        }
        echo "</ul></div>\n";
    } else if (count($flags[$type]) == 1) {
        echo "<div id=\"statusbox_" . $type . "\">" . $flags[$type][0] . "</div>\n";
    }
}

feedback("success");
feedback("failure");
Code:
Array ( [failure] => Array  (  [0] => WARNING: There are no valid services available.  )

As an afterthought, it is amazing that no matter how experienced, or how large your applications are, it is always the little things that get you. If you think I am an idiot for asking this question, then you have not coded enough.
 
Last edited:

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
There is a variable called flags created throughout my script that this function responds to. For some reason, with the variable set as dumped below, it does not print anything. Why??? *growls*
PHP:
function feedback($type) {
    if (count($flags[$type]) > 1) {
        echo "<div id=\"statusbox_" . $type . "\"><ul>\n";
        foreach ($flags[$type] as $value) {
            echo "<li>" . $value . "</li>\n";
        }
        echo "</ul></div>\n";
    } else if (count($flags[$type]) == 1) {
        echo "<div id=\"statusbox_" . $type . "\">" . $flags[$type][0] . "</div>\n";
    }
}

feedback("success");
feedback("failure");
Code:
Array ( [failure] => Array  (  [0] => WARNING: There are no valid services available.  )

As an afterthought, it is amazing that no matter how experienced, or how large your applications are, it is always the little things that get you. If you think I am an idiot for asking this question, then you have not coded enough.

Is "success" and "failure" are only elements or another arrays inside the "flags" array

If they are just elements then count (array[value]) is the problem.

If not they are arrays then it has to return the number of elements of inner arrays.

To count number of success and failures then you have to do manually i think.

PS: remove "\n" and place <BR> :)
 
Last edited:

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Is "success" and "failure" are only elements or another arrays inside the "flags" array
If they are just elements then count (array[value]) is the problem.
I included the var_dump so you can see what was in the array at the time of the problem. In this case, to my knowledge, the 'count($flags[$type])' should have been counting the number of elements in the array of strings @ the key 'failure' within the multidimensional flags array. There is only one element there, which means that it should have printed a box with one string in it, but instead, the 'count($flags[$type])' statement returns zero, and focus slips out of that if statement altogether. WHy doesn't it return 1?

remove "\n" and place <BR>
Thats just there so I can read the HTML output. I hate sources that have no line breaks. But thanks for the tip.
 
Last edited:

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
Hi Twinkie,

PHP:
$count =array_count_values($flags);
$count[$type]      //  This will give you the number of success or failures.

So total your code will become.

PHP:
function feedback($type) { 
    $count =array_count_values($flags);
    if ($count[$type]) > 1) { 
        echo "<div id=\"statusbox_" . $type . "\"><ul>"; 
        foreach ($flags[$type] as $value) { 
            echo "<li>" . $value . "</li>\n"; 
        } 
        echo "</ul></div>\n"; 
    } else if ($count[$type] == 1) { 
        echo "<div id=\"statusbox_" . $type . "\">" . $flags[$type][0] . "</div>"; 
    }
} 

feedback("success"); 
feedback("failure");
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
With that code, I get this error: array_count_values() [<a href='function.array-count-values'>function.array-count-values</a>]: The argument should be an array

I don't understand why I should divert from count(), or why it isn't working?
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
That means if $flags holds only one element, It is not an array, It is just an element, that is the error in both the case count and array_count_values
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
To my knowledge, if an array holds only one element, then it is still an array and it is accessed by the [0] key. However, I tried this code anyway and it produced the same results:
PHP:
function feedback($type) {
	if (is_array($flags[$type])) {
		echo "<div id=\"statusbox_" . $type . "\"><ul>\n";
		foreach ($flags[$type] as $value) {
			echo "<li>" . $value . "</li>\n";
		}
		echo "</ul></div>\n";
	} else if (isset($flags[$type])) {
		echo "<div id=\"statusbox_" . $type . "\">" . $flags[$type] . "</div>\n";
	}
}

feedback("success");
feedback("failure");
 

Twinkie

Banned
Messages
1,389
Reaction score
12
Points
0
Ahhhhhhhhhh! I always forget that, and it is always the longest time before I realize my mistake. Thank you!

Unless we are working OOP, globals are a necessary evil.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
You can minimize globals by passing additional arguments. If there are only 1 or 2 arguments, this works well. With 4 or more, it becomes unwieldy.

On the subject of OOP, you could create a class (e.g. Options or Config) that has the globally available data as a static member. Then you don't pollute the global namespace as much, though coupling is still too high.
 
Top