PDA

View Full Version : PHP: nl2br() on array variables


Rower_CPU
Mar 4, 2004, 03:44 PM
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.
$variable = $row[variable]; // Doesn't work
$variable = $row[1]; // Works

Thoughts?

sonofslim
Mar 4, 2004, 04:01 PM
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:
$array = mysql_fetch_array($result, MYSQL_ASSOC);
or
$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.

Rower_CPU
Mar 4, 2004, 04:05 PM
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?

sonofslim
Mar 4, 2004, 04:06 PM
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?

can you give me an example of the code you're using to echo out your variables?

Rower_CPU
Mar 4, 2004, 04:24 PM
Sure thing...
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.

jeremy.king
Mar 4, 2004, 04:44 PM
Try

$row["IP"] instead

Rower_CPU
Mar 4, 2004, 04:52 PM
Try

$row["IP"] instead

Well, $row[IP] works fine for echoing...

Are you saying I need the quotes for tossing the value into another variable???

mrjamin
Mar 4, 2004, 06:09 PM
what errors are you actually getting? can you put up a phpinfo file for us to look at?

Rower_CPU
Mar 4, 2004, 06:30 PM
what errors are you actually getting? can you put up a phpinfo file for us to look at?

Actually, it's only a notice:
Notice: Use of undefined constant machine - assumed 'machine'

The notice is if I try something like:
$machine = $row[machine];

phpinfo() (http://larctech.sdsu.edu/test.php)

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

mrjamin
Mar 4, 2004, 06:46 PM
Actually, it's only a notice:


The notice is if I try something like:
$machine = $row[machine];

phpinfo() (http://larctech.sdsu.edu/test.php)

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

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:

<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

Rower_CPU
Mar 4, 2004, 06:49 PM
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:

<IfModule mod_php4.c>
php_value error_reporting 247
# php_flag register_globals off
</IfModule>


If you're comfortable working with register_globals off, uncomment the second line.

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

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.

mrjamin
Mar 4, 2004, 07:46 PM
So can you explain why $array[key_name] is fine in echo statements but not elsewhere? That's what I'm stumped on.

No, I can't! It is an odd one and got me stumped too :rolleyes:

jeremy.king
Mar 4, 2004, 08:23 PM
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.

jeremy.king
Mar 4, 2004, 08:27 PM
The PHP Manual explains why $row[ID] works....sometimes

See http://us4.php.net/manual/en/language.types.array.php

Look for the Dos and Don'ts section for more information

Bottom Line: always use quotes.

Rower_CPU
Mar 4, 2004, 08:31 PM
Thanks for the pointer to the docs, kingjr3.

Thanks for the info and help everyone!

sonofslim
Mar 5, 2004, 09:05 AM
I'm sure there are other optimizations that can be made, too...feel free to point them out.

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:
$result = mysql_query($query) or die(mysql_error());
$i = 1;
echo '<table>';
while($array = mysql_fetch_assoc($result))
{
$style = ($i & 1 ? 'odd' : 'even');
echo '<tr class="'.$style.'"><td>'.$array["key"].'</td></tr>';
$i++;
}
echo '</table>';the important part is:$i = 1;
$style = ($i & 1 ? '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:$style = ($i % 2 ? '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.

Rower_CPU
Mar 5, 2004, 12:36 PM
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. :)

Rower_CPU
Mar 5, 2004, 01:13 PM
Oddly enough, this was just posted on A List Apart:

http://www.alistapart.com/articles/zebratables/

Topical, no?

sonofslim
Mar 5, 2004, 03:53 PM
Oddly enough, this was just posted on A List Apart:

http://www.alistapart.com/articles/zebratables/

Topical, no?

it's like we're one with the web design zeitgeist... could you call it the zengeist?

Rower_CPU
Mar 5, 2004, 06:25 PM
I think you could. :)

mrjamin
Mar 6, 2004, 11:11 AM
it's like we're one with the web design zeitgeist... could you call it the zengeist?

zendguist may be more accurate

sonofslim
Mar 6, 2004, 12:42 PM
zendguist may be more accurate
touché, sir! but may i suggest zeldgeist?

Rower_CPU
Mar 6, 2004, 12:48 PM
touché, sir! but may i suggest zeldgeist?

Ha! I was waiting for someone to use that. :p

sonofslim
Mar 6, 2004, 02:41 PM
well, you heard it here first... i can't wait for MR to get blogged for being the birthplace of the phrase.