PHP: nl2br() on array variables

Discussion in 'Web Design and Development (archive)' started by Rower_CPU, Mar 4, 2004.

  1. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #1
    I'm working on new projects with a more strict php.ini and running into some weird behavior.

    For instance, I'm pulling info from a db and trying to run good old nl2br to display new lines as <br> in the HTML. But, if I try to access the array value by name and put it into a new variable I get errors - it works fine if I echo it out though.

    If I access the variable by it's position, it works fine.
    PHP:
    $variable $row[variable]; // Doesn't work
    $variable $row[1];  // Works
    Thoughts?
     
  2. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #2
    sounds like your array isn't being created with associative keys. what's the line you're using to create the array?

    try one of the following:
    PHP:
    $array mysql_fetch_array($resultMYSQL_ASSOC);
    or
    PHP:
    $array mysql_fetch_assoc($result);
    see if forcing it to use associative indices has any effect. (both lines do the same thing.)

    edit: sorry, should have looked closer. i take it, since you're using an array variable called $row, that you're using mysql_fetch_row()? if that's the case, then this is definitely the culprit. mysql_fetch_row() only returns a numeric array. use one of the lines above if you need associative indices. or keep it as is, if calling an array variable by offset is ok with you.
     
  3. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #3
    I can access the variables by name when I echo them out, though (should've made that clearer).

    Would the associative array make a difference between ability to echo and manipulate the strings?
     
  4. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #4
    can you give me an example of the code you're using to echo out your variables?
     
  5. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #5
    Sure thing...
    PHP:
    while ($row mysql_fetch_array($data)) {
          if (
    $alternate == "1") {
             
    $class "yellow";
                
    $alternate "2";
        } else {
            
    $class "orange";
            
    $alternate "1";
        }
        echo 
    "<tr class=\"$class\">\n";
        
    $machine nl2br($row[2]);
        
    $comment nl2br($row[3]);
        echo 
    "<td><a href=\"http://$row[IP]\" target=\"blank\">$row[IP]</td>
            <td>
    $machine</td>
            <td>
    $comment</td>
            <td><a href=\"ip.php?ip=
    $row[IP]\">edit</a></td>\n";
    }
    You can see I'm echoing $row[IP] just fine, but for the nl2br, I have to go with position numbers.

    I'm sure there are other optimizations that can be made, too...feel free to point them out.
     
  6. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #6
    Try

    $row["IP"] instead
     
  7. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #7
    Well, $row[IP] works fine for echoing...

    Are you saying I need the quotes for tossing the value into another variable???
     
  8. macrumors 65816

    mrjamin

    Joined:
    Feb 6, 2003
    Location:
    Strongbadia
    #8
    what errors are you actually getting? can you put up a phpinfo file for us to look at?
     
  9. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #9
    Actually, it's only a notice:
    The notice is if I try something like:
    PHP:
    $machine $row[machine];
    phpinfo()

    kingjr3-
    The quotes thing seems to work. Makes no sense to me, though...
     
  10. macrumors 65816

    mrjamin

    Joined:
    Feb 6, 2003
    Location:
    Strongbadia
    #10
    thats all down to the way PHP had it error handling set! thats a very poorly set up server if you ask me...

    put an .htaccess file in the root folder of your site, containing the following:
    Code:
    <IfModule mod_php4.c>
        php_value error_reporting 247
    </IfModule>
    
    We've just got to hope that the apache config of the server is set to allow htaccess files to override stuff!

    So yeh, that'll get rid of the notices. Its not great programming practise to call stuff by $array[key_name] (hence php kicking up a warning), i always use $array['key_name'] where possible.

    Displaying notices can expose all kinds of vulnerabilities in your code for even a novice hacker to exploit, so best turn them off anyway

    edit: removed register_globals stuff, already turned off :D
     
  11. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #11
    Well, it's my personal box at work...;)

    Errors were completely off, but for troubleshooting purposes I turned them on. I can edit php.ini to turn them back off.

    So can you explain why $array[key_name] is fine in echo statements but not elsewhere? That's what I'm stumped on.
     
  12. macrumors 65816

    mrjamin

    Joined:
    Feb 6, 2003
    Location:
    Strongbadia
    #12
    No, I can't! It is an odd one and got me stumped too :rolleyes:
     
  13. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #13
    I can't explain why the echo works, but I do know that an associative array (aka hashtable) uses strings as the key. When you use $row[ID], PHP is looking for a constant called ID, which doesn't exist. $row["ID"] is asking for the element in the array with the key="ID" (the string). ALWAYS use quotes when addressing elements in a hashtable.

    Glad it worked.
     
  14. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #14
  15. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #15
    Thanks for the pointer to the docs, kingjr3.

    Thanks for the info and help everyone!
     
  16. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #16
    Rower,
    are you familiar with the ternary operator: (condition ? if true : if false) here's an example of how i use it to set alternating row styles:
    PHP:
    $result mysql_query($query) or die(mysql_error());
    $i 1;
    echo 
    '<table>';
    while(
    $array mysql_fetch_assoc($result))
    {
        
    $style = ($i 'odd' 'even');
        echo 
    '<tr class="'.$style.'"><td>'.$array["key"].'</td></tr>';
        
    $i++;
    }
    echo 
    '</table>';
    the important part is:
    PHP:
    $i 1;
    $style = ($i 'odd' 'even');
    $i++;
    which iterates a simple counter variable and checks at each pass if it's odd or even by comparing it bitwise to 1. you can get the same effect using the modulo operator:
    PHP:
    $style = ($i 'odd' 'even');
    but i like to use the bitwise function, because binary is so punk rock.

    the bitwise comparator (the & above) takes two variables and compares their binary equivalents, beginning with the least significant digit (the 1's place.) so whatever $i is, it turns it into binary and compares it to, in this case, the numeral 1. and since there's only one digit to be compared to, it never looks past the right-most digit -- and that's the digit that tells us if a binary number is odd or even.
     
  17. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #17
    That's a new one for me. Thanks for pointing that out.

    That's a much nicer way to switch the class style than my 1,2 method. :)
     
  18. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #18
  19. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #19
    it's like we're one with the web design zeitgeist... could you call it the zengeist?
     
  20. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #20
    I think you could. :)
     
  21. macrumors 65816

    mrjamin

    Joined:
    Feb 6, 2003
    Location:
    Strongbadia
    #21
    zendguist may be more accurate
     
  22. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #22
    touché, sir! but may i suggest zeldgeist?
     
  23. thread starter Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #23
    Ha! I was waiting for someone to use that. :p
     
  24. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #24
    well, you heard it here first... i can't wait for MR to get blogged for being the birthplace of the phrase.
     

Share This Page