+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Small PHP Issue

  1. #1
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Small PHP Issue

    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 Code:
    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 by Twinkie; 12-22-2009 at 11:48 PM.

  2. #2
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Small PHP Issue

    Quote Originally Posted by Twinkie View Post
    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 Code:
    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 by Gouri; 12-23-2009 at 12:19 AM.
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  3. #3
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Small PHP Issue

    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 by Twinkie; 12-23-2009 at 01:15 AM.

  4. #4
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Small PHP Issue

    Hi Twinkie,

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

    PHP Code:
    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"); 
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  5. #5
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Small PHP Issue

    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?

  6. #6
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: Small PHP Issue

    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
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  7. #7
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Small PHP Issue

    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 Code:
    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"); 

  8. #8
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: Small PHP Issue

    print_r($flags); // ?

  9. #9
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Small PHP Issue

    $flags isn't declared global in feedback(). In any case, globals are (often) evil.

    Quote Originally Posted by gsonline View Post
    PS: remove "\n" and place <BR>
    <br> isn't semantic and is mostly abused. If you need line separation in the rendered view, use elements that naturally separate, such as <p> for paragraphs.
    Last edited by misson; 12-23-2009 at 04:45 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  10. #10
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: Small PHP Issue

    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 by Twinkie; 12-23-2009 at 12:55 PM.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. PEAR PHP installation/configuration issue
    By emailtoarpit in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 12-01-2009, 11:21 PM
  2. PHP Session writing issue (starka)
    By ah-blabla in forum Free Hosting
    Replies: 2
    Last Post: 10-20-2009, 09:45 AM
  3. PHP mail issue
    By adamfek in forum Free Hosting
    Replies: 5
    Last Post: 10-19-2009, 11:33 AM
  4. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  5. new PHP issue with wordpress
    By karanits in forum Free Hosting
    Replies: 3
    Last Post: 08-23-2008, 12:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers