Forum Moderators: coopster

Message Too Old, No Replies

Loop through a result array and apply => htmlspecialchars

Please help me I'm having a brain cramp...

         

mipapage

2:59 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey all,

I think that this could be easy, but my brain cramps up part way and I can't seem to get it right (I'm in the middle of a marathon coding/programming session).

I have a result set:

$query = "SELECT * FROM mytable WHERE code = 'topsecret!'";  
$result = mysql_query($query);

Now I want to loop thru the resulting $row's and apply the htmlspecialchars() function. My brain cramps right about at the point where I start to build the foreach. Can someone walk me through this?

while ($row = mysql_fetch_array($result)) {  
foreach ($row as $key => $val) {
CRAMP!
}

mep00

3:14 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



After you aply
htmlspecialchars()
, what do you want to do with the result? What does you table structre look like? Also, your
while
is missing a closing brace.

Without some more info your question is too open ended to give an answer.

mipapage

3:22 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the quick reply!

missing a closing brace

Thanks, just a bad copy and paste...

Details..

Basically, I end up with 7 values '$row[1] = string -> $row[7] = string' for each result.

Right now I had been plugging the $row[x] results into a while loop containing the markup to present search results.

Unfortunately there are a few special characters in the db (out of my control) that I need to be careful of...

So, what I would like to do is clean up the 'bad' entities before passing the values to the html-creating while loop...

willybfriendly

3:31 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you consider array_walk()?

WBF

mipapage

3:34 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ooooh - I am now! Thanks! Very Useful [es2.php.net]. Well, I'll let you know how it goes...

willybfriendly

3:53 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. I have a couple of standard functions I use, pre_store() and pre_display, that I can walk $_POST through before storing to the DB and then walk $row through before outputting to the page. These are the places to put trim(), mysql_real_escape(), etc.

I find it really cuts down on the work.

WBF

willybfriendly

3:56 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No edit button today! That should read pre_display().

WBF

mipapage

4:10 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



willybfriendly,

I'm melting here. I'm at the stage where I can see that array_walk should work, but damn if I can build the right function to walk my array thru...

Ha!
I knew that by typing up this message I'd get it to work. I Think I faked out the 'powers that be', so to speak.

Anyway, why the '&' in

function test_alter(&$item1, $key, $prefix) 
{
$item1 = "$prefix: $item1";
}

from the example in the link I posted?
does this question expose me as the rookie I am?

mipapage

4:14 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting, I'm starting to get enough of these little helper functions that I can see where using a class would be beneficial to me...

Oh yeah, thanks WBF!

mep00

4:35 pm on Feb 15, 2004 (gmt 0)

10+ Year Member



Anyway, why the '&'
By default php uses call by value. By using the amperstand you call by referrence.

using a class would be beneficial
Once you get used to it, you begin to realise that classes are a neat way to group functions together.

willybfriendly

7:12 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why the &?

Think variable scope. In PHP variables within a function are not accessble outsie of that function. So,

function($data)
{
do something with $data
}

contains the variable $data. You can call it via

function($id);

and internally it will treat $id as $data.

To get that $data back you will either need to return it (i.e. "return $data;"), pass a variable that was declared global, i.e.

$id = "asdf";
global $id;
function($id);

or, pass the variable by reference. To do that you put the ampersand in front of the variable in the fucntion declaration.

function(&$data);

Now the function will operate on the variable stored in memory rather than the variable created when the function is called.

Clear as mud?

WBF

mipapage

7:47 pm on Feb 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Clear as mud?

Uhhh-huh. Yep. No, really, I was reading those Zend 'rock solid php' (or something like that) and they go into variable variables, I believe. So that may have helped me to understand what you're spelling out. Thanks!

And thanks to both of you - in the end a very nice solution. There are so many functions in PHP that I just don't know...

mep00

6:42 am on Feb 16, 2004 (gmt 0)

10+ Year Member



Why the &?
Think variable scope. In PHP variables within a function are not accessble outsie of that function.
Close, but not exactly. While issues of variable scope are relavent, the amperstand has no effect on scope. With it, two different variable can access the same memory location--not just the same value. Variable scope deals with variable names and where they are valid. Two variables can have the same name, but because of scope, are two compleatly different variables, each with it's own value and memory location. With the amperstand you have two names for the same variable.

internally it will treat $id as $data.
Not at all. It mearly copies the value of $id and stores it in $data. Any changes to $data are lost when $data is destroyed upon function termination.

To get that $data back you will either need to return it (i.e. "return $data;"), pass a variable that was declared global,
All a
return
does is copy an evaluated expression (thereby "returning" it) and terminate the function pass control back to the caller. A global variable is one with global scope--it can be referrenced anywhere. By passing a global as a paramater to a function you are making a copy of it's value and assigning it to the local variable in the same possion in the function's declaired argument list. (I'm ignoring the feature of variable length argument lists to keep things simple.) Unless you are calling by reference (with the amperstand), there is no way to get
$data
back; it will be destroyed upon function termination. What you can do is return a copy of it's value. The difference is very significant.

function(&$data);

Now the function will operate on the variable stored in memory rather than the variable created when the function is called.

Close enough. It's not exactly correct, but the difference is splitting hairs.

Clear as mud?
Sorry WBF, but here you are 100% correct.

Clear as mud?

Uhhh-huh. Yep. No,
Well put.

variable variables..may have helped me to understand what you're spelling out.
I was thinking along the same lines. In case there is confusion about variable variables:

$color = "red";
$apple = "color";
echo $$apple; // Will display: [red]red[/red]

Most definitly a cool feature.

mipapage

11:11 am on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks mep00,

After some sleep this is starting to make sense. This one's off to the bookmark list for me for sure!

willybfriendly

4:05 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mep00 - looks like we are explaining the same thing from two different perspectives - technical vs. working.

With it, two different variable can access the same memory location--not just the same value.

Yes, it effectively sets a pointer to that memory location and operations are then conducted on the value stored in that location.

Variable scope deals with variable names and where they are valid. Two variables can have the same name, but because of scope, are two compleatly different variables, each with it's own value and memory location. With the amperstand you have two names for the same variable.

Or, two variable names pointing to the same memory location, where the value under consideration is stored.

Any changes to $data are lost when $data is destroyed upon function termination.

Which is why one must have a way to "retrieve" $data from the function if one wants to continue to use its value. Perhaps a better way of saying this is one must "preserve" the value of $data for later use.

All a return does is copy an evaluated expression (thereby "returning" it) and terminate the function pass control back to the caller.

True enough. My intent was to suggest that one could use the syntax "$id = function($id);" as a way to change/preserve the value of $id.

A global variable is one with global scope--it can be referrenced anywhere. By passing a global as a paramater to a function you are making a copy of it's value and assigning it to the local variable in the same possion in the function's declaired argument list.

But, if one declares a variable global within a function. it can then be referenced outside of that function. Another way to preserve the value generated within the function.

Unless you are calling by reference (with the amperstand), there is no way to get $data back; it will be destroyed upon function termination. What you can do is return a copy of it's value. The difference is very significant.

But, if what one wants is to preserve the new value of $data, there are three ways to do so. 1) Pass by reference, 2) assign via a return statement, or 3) declare the variable as global.

but the difference is splitting hairs.

Well said indeed!

Variable scope has cost countless hours of headscratching and fist pounding. My goal was simply to explain that one needs to understand that what takes place in a function is not going to show up outside of that function unless one takes steps to make it so.

WBF

mipapage

4:25 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Variable scope has cost countless hours of headscratching and fist pounding.

Well, this time, you both saved me from that fate. Thanks!