Check through output in array
Results 1 to 12 of 12

Thread: Check through output in array

  1. #1
    Senior Member
    Join Date
    May 2006
    Posts
    183

    Check through output in array

    Hi,

    I have code so that users can make comments on a page, they have a choice to have their name displayed or be anonymous. I want to order the comments by the date they were made, and display the name or 'anonymous poster' if they selected anonymous.

    So I have this code which grabs everything into an array, but the if statement within the array is not working, in testing, when I select anonymous the page still displays the name of the user. What is the best way around this?

    The following code is where I am stuck:

    [php]
    echo "<h3>Comments for this article:</h3>";
    while($fetch=mysql_fetch_array($query)) {
    $anonymous=$fetch['anonymous'];
    if ($anonymous===true){
    echo "<p>".$fetch['comment']."<br/><sub><b>Comment by: </b>'Anonymous Poster'</sub><hr /><p>";
    }
    else{
    echo "<p>".$fetch['comment']."<br/><sub><b>Comment by: </b>".$fetch['userid']."</sub><hr /><p>";
    }
    }

    Thanks,


    G

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    Well, there's the issue that you're still using the MySQL extension.

    But have you checked to see what $fetch['anonymous'] contains? Is it a boolean? Is it true? The type matters because of the comparison operator you're using.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Senior Member
    Join Date
    May 2006
    Posts
    183
    Thanks, where it is true, the value is true.

    G

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    Quote Originally Posted by genista
    Thanks, where it is true, the value is true.
    But have you checked?
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Senior Member
    Join Date
    May 2006
    Posts
    183
    I have checked, some are true where anonymous has been set.

  6. #6
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    What do you get for var_dump($fetch['anonymous'])?
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  7. #7
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by genista View Post
    Thanks, where it is true, the value is true.
    ...what I'm wondering (and I wouldn't be surprised if this is also what Weedpacket is wondering) is if it is the boolean true, or the string "true".

  8. #8
    Senior Member
    Join Date
    May 2006
    Posts
    183
    Ok, I see where your going and yes you are correct. I have now changed ===true to ="true" and we have success! Feel a bit stupid though....


    Thanks Weedpacket!

  9. #9
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by genista View Post
    Ok, I see where your going and yes you are correct. I have now changed ===true to ="true" and we have success! Feel a bit stupid though....


    Thanks Weedpacket!
    mm...

    PHP Code:
    if( $var true # will _always_evaluate_as_TRUE_ ( assignment )

    if( $var == true # this is *probably* the one you want ( type-insensitive comparison )

    if( $var === true # only true if $var is true _and_ a boolean ( type-sensitive comparison ) 
    However, realize that, depending on how you're storing this info in the DB, you might still have problems. For example:
    • NULL (a missing value) will compare false
    • "False" (a string) will compare true
    • "" (an empty string) will compare false
    • "0" (the character zero) will compare false
    • . . .
    Last edited by traq; 02-11-2013 at 10:18 AM.

  10. #10
    Senior Member
    Join Date
    May 2006
    Posts
    183
    Thanks!

  11. #11
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    sorry; see my edits above

  12. #12
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,716
    Quote Originally Posted by genista View Post
    Feel a bit stupid though....
    Unfortunately, PHP can do that to ya ... generally at least a couple times a week, here.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •